4

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 Answer 1

2

I know that the question is old, but maybe someone else needs simple solution.

  1. Set type of responses as String!

    mutation submitResponse($user: ID!, $responses: String!){
      submitResponse(user: $user, responses: $responses) {
        id
      }
    }
    
  2. Then:

    yourData = [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]
    responses = JSON.stringify(yourData)
    
  3. 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'))
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.