【发布时间】:2019-10-31 15:21:02
【问题描述】:
我想使用一个角度对象作为 graphql 突变的输入变量。我已经研究了几个小时,并且似乎尝试了每个示例的所有可能组合,包括使用这样的变量,这会引发变量不匹配的错误。
return this.apollo.mutate({
mutation: gql`
mutation saveLicense(license:License) {
saveLicense(license:$license) {
licenseId,
name,
body,
deprecated,
fsfLibre,
osiApproved,
spdxId,
orLaterLicense,
familyName,
familyNameRegex,
cleanName,
twoNameRegex,
nameAttributeList {
attribute,
index
},
attributes {
attributeType,
key
}
}
}
`,
variables {license:license}
});
最终,提交给服务器的查询需要看起来像这样(当然有值):
return this.apollo.mutate({
mutation: gql`
mutation {
saveLicense(
license:{
licenseId:"",
name:"",
licenseFamily:"",
version:"",
replacementLicenseId:"",
spdxId:"",
deprecated: false,
fsfLibre:false,
osiApproved:false,
orLaterLicense:false,
familyName:"",
familyNameRegex:"",
cleanName:"",
twoNameRegex:"",
body:"",
description:""
}
){
licenseId,
name,
body,
deprecated,
fsfLibre,
osiApproved,
spdxId,
orLaterLicense,
familyName,
familyNameRegex,
cleanName,
twoNameRegex,
nameAttributeList {
attribute,
index
},
attributes {
attributeType,
key
}
}
}
`,
});
上述查询有效,但我无法弄清楚如何将 angular 对象转换为查询所需的 graphql 字符串形式。经过数小时的研究,我唯一的解决方案是将对象转换为 json 表示,然后再返回并将许可证作为字符串传递。这显然是一个严重的黑客攻击!
JSON.parse(JSON.stringify(mylicense))
我正在寻找如何将变量与 angular/Apollo 一起使用的示例,或者寻找一种将 angular 对象转换为可以在 Graphql 中使用的字符串的更简洁的方法。
【问题讨论】: