-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Double Stringify() for AWS AppSync AWSJSON attributes #807
Comments
Hey @QAnders 👋 What type is that EDIT: I'm willing to bet this is more a property of string interpolation in JavaScript and not so much about mutation createOutgoingInvoice{
outgoinginvoice(
input: {
invoice_data: {
currency: "USD",
references: {"some": "field", "value": 2}
},
org_nr: "SE123"
}
) {
id
}
} Note how its set to the object, and again, this is because it returns an unquoted string. Since your GraphQL server expects a stringified JSON object, this is where your 2nd While you can use interpolation to add variables to your query, I'd recommend that you use GraphQL mutation createOutgoingInvoice($currency: String, $references: AWSJSON) {
outgoinginvoice(
input: {
invoice_data: {
currency: $currency,
references: $references
},
org_nr: "SE123"
}
) {
id
}
} Then in your request (not sure what library you're using to fetch, but here is the raw import { print } from 'graphql';
const mutation = gql`
mutation createOutgoingInvoice {
# ...
}
`
fetch('/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
// NOTE: If you're using apollo or another library, this print is usually done for you
query: print(mutation),
variables: {
currency: 'USD',
references: JSON.stringify(references)
}
})
) See if you have better luck with this approach! |
Thanks for getting back to me! Much appreciated! I did a lot (I mean a lot) of testing and various attempts in getting it working and I have too tried with AppSync then returns an error like:
The only way (I found) to get it to accept anything in I guess I just have to live with the double stringify() and hopefully this issue can help some other poor sod struggling to get request with JSON into AppSync... I added some logging to AppSync, and here we can clearly see that your point, @jerelmiller , is totally accurate on a working query:
Trying the same with
Using only one stringify on the variables gives me the expected "string" in the logs, but I still get the
So, I can conclude, that something in AppSync and how it move the values from variables to the mutation query is messing it up and the only solution I can find is to have the JSON objects doubly stringify'd and in the mutation query itself... |
graphql-tag
has served us well, and it's a great module, but I've been trying to figure this one out, and it seems related tographql-tag
...We are running AWS AppSync and it's a bit finnicky with JSON data as input, so it could be some AWS thing as well...
We have an invoice object, on which we run mutations (e.g. create the invoice in the DB), and inside the invoice object we have other attributes that are JSON.
In the AppSync schema the attributes are defined as
AWSJSON
which will take a stringify'd JSON as input.In the code though, calling the mutation I have to stringify it twice to get it working, like the below sample:
The mutation works and the invoice is created, but there's something weird going on... Has anyone else come across this using AppSync?
The text was updated successfully, but these errors were encountered: