我正在使用React,我发送此信息:
const imageServicesClean = JSON.stringify(imageServices);
const query = `
mutation {
companyUpdate(
idCompany:${idCompany},
name:${nameClean},
imageServices:${imageServicesClean})
{
idCompany
name
imageServices {
idImageService
name
url
key
}
}
}`;
imageServicesClean以这种方式发送,但返回错误:
[{
"idImageService": 1,
"name": "Service1",
"url": "",
"key": "asdasdas"
}, {
"idImageService": 2,
"name": "Service2",
"url": "sdsads",
"key": "sddsfsds_"
}]
因为我的GraphQL服务器(Laravel)只允许没有引号的变量,所以:
[{
idImageService: 1,
name: "Service1",
url: "",
key: "sdofunc4938urcnnwikk"
}, {
idImageService: 2,
name: "Service2",
url: "sdsads",
key: "sddsfsdssss8347yuirh"
}]
因此函数JSON.stringify
不适用于GraphQL中的构建格式。如何在Javascript中将对象数组转换为GraphQL格式?
答案 0 :(得分:4)
最后这是我的解决方案:
const imageServicesClean = JSON.stringify(imageServices);
const graphQLImageServices = imageServicesClean.replace(/"([^(")"]+)":/g,"$1:");
但最后我正在使用这个库,它为我做了一切:https://github.com/atulmy/gql-query-builder