问题
How can I structure the query parameters a graphQL mutation when one of the fields is an array of objects? When I hard-code the mutation this is valid and works:
mutation{
submitResponse(user: "1234", responses: [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]) {
id
}
}
I can't for the life of me figure out how to pass in query parameters for the responses array of objects. How can I define the type of each field in the object. This is what it would like for the user id and that works fine but I can't figure out the responses piece of it.
mutation submitResponse($user: ID!){
submitResponse(user: $user, responses: [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]) {
id
}
}
Thanks!
回答1:
I know that the question is old, but maybe someone else needs simple solution.
Set type of
responses
asString!
mutation submitResponse($user: ID!, $responses: String!){ submitResponse(user: $user, responses: $responses) { id } }
Then:
yourData = [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}] responses = JSON.stringify(yourData)
In my case in BackEnd I use Python, so receive
responses
in BackEnd and then simply convert JSON (string) to Python's dict, something like:import json responses = json.loads(input.get('responses'))
来源:https://stackoverflow.com/questions/45910230/submit-an-array-of-objects-as-a-query-variable-in-graphql