如何使用 axios 将数组传递给 graphql 突变
我创建了一个接受对象数组的突变。我用阿波罗客户端测试了它,它在我的阿波罗客户端查询中工作正常,如下所示。
mutation {
createStudent(createStudentInput: [{
name:"Jhone Doe"
email:"[email protected]"
dob:"1996-01-21"
},
{
name:"Jhone Doe1"
email:"[email protected]"
dob:"2000-01-22"
}
]) {
id,
name
dob
}
}
但是,当我尝试使用 Axios 执行突变时,它会抛出错误 Request failed with status code 400
。这是我传递给 Axios 的 qraphql 查询。我不明白我在这里做错了什么。 的对象数组
const graphqlQuery = {
query: `
mutation {
createStudent(createStudentInput:${records}) {
id,
name
dob
}
}`,
variables: {
},
};
请注意,records
是我的Axios请求
try {
const results = await axios({
url: endpoint,
method: 'post',
headers: headers,
data: graphqlQuery,
});
return results;
} catch (error) {
console.log(`error occured:${error}`);
}
}
I have created a mutation that accepts an array of objects. I tested it with an apollo client and it's working fine in my apollo client query is looks like this.
mutation {
createStudent(createStudentInput: [{
name:"Jhone Doe"
email:"[email protected]"
dob:"1996-01-21"
},
{
name:"Jhone Doe1"
email:"[email protected]"
dob:"2000-01-22"
}
]) {
id,
name
dob
}
}
But when I try to perform the mutation using Axios it's throwing an error Request failed with status code 400
.this is the qraphql query I'm passing to the Axios. I can't figure out what I am doing wrong here. Please note that records
is an array of objects
const graphqlQuery = {
query: `
mutation {
createStudent(createStudentInput:${records}) {
id,
name
dob
}
}`,
variables: {
},
};
My Axios request
try {
const results = await axios({
url: endpoint,
method: 'post',
headers: headers,
data: graphqlQuery,
});
return results;
} catch (error) {
console.log(`error occured:${error}`);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论