I want to use an angular object as an input variable to a graphql mutation. I've been researching for hours and have tried seemingly every possible combination per examples including using variables like this, which throws an error that theres a variable mismatch.
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}
});
Ultimately, the query submitted to the server needs to look like this(with values of course):
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
}
}
}
`,
});
The above query works, however I CANNOT figure out how to convert an angular object to the graphql string form that I need for the query. After HOURS of research the only solution I have is to covert the object to a json representation, then back again and pass license as a string. This is obviously a serious hack!
JSON.parse(JSON.stringify(mylicense))
I'm looking for either an example of how to use variables with angular/Apollo OR a cleaner way of converting angular objects into strings I can use in Graphql.