0

I want to pass an object array of [{questionId1,value1},{questionId2,value2},{questionId3,value3}] of dynamic size in GraphQL Mutation with NodeJS

.........
args: {
        input: {
            type: new GraphQLNonNull(new GraphQLInputObjectType({
                name: 'AssessmentStep3Input',
                fields: {
                    questionId:{
                        name:'Question ID',
                        type: new GraphQLNonNull(GraphQLID)
                    },
                    value:{
                        name:'Question Value',
                        type: new GraphQLNonNull(GraphQLBoolean)
                    }
                }
            }))
        }
    },
.........

How can I do that with the given sample of code?

Thanks

Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39

2 Answers2

2

If you want to pass an object array with GraphQL Mutation you need to use "GraphQLList" which allows you to pass an array with dynamic size of given input.

Here is the example

........
........
args: {
        input: {
            type: new GraphQLNonNull(GraphQLList(new GraphQLInputObjectType({
                name: 'AssessmentStep3Input',
                fields: {
                    questionId:{
                        name:'Question ID',
                        type: new GraphQLNonNull(GraphQLID)
                    },
                    value:{
                        name:'Question Value',
                        type: new GraphQLNonNull(GraphQLBoolean)
                    }
                }
            }))
            )
        }
    },
........
........

Hope it helps.

Thanks

Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
  • I will just add that there should be "new" keyword before GraphQLList to get it working, otherwise it is correct :) – David Mraz Sep 03 '18 at 16:17
0

i just published the article on that, so that you can take a look if you would like to know more detail. This is the repository with the examples, where the createUsers mutation is implemented https://github.com/atherosai/express-graphql-demo/blob/feature/5-modifiers/server/graphql/users/userMutations.js. You can take a look how it is implemented, but in general the above answer is correct. You can input as many objects as you would like to in the array (if you have not implemented some number of items limiting, but it is not there by default).

David Mraz
  • 545
  • 4
  • 8