Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array as argument in GraphQL

I have started working on GraphQL.My schema contains one list item too.

Following is the code of my schema:

var userType = new graphql.GraphQLObjectType({  
 name: 'user',
 fields: function () {
  return {
    _id: {
    type: graphql.GraphQLID
  },
  name: {
    type: graphql.GraphQLString
  },
  age: {
    type: graphql.GraphQLString
  },
  degrees:[
  {type:graphql.GraphQLList}
  ]
}
  }
   });

AND the query is as follows:

  var QueryType = new graphql.GraphQLObjectType({  
  name: 'Query',
  fields: () => ({
    userArr: {
      type: new graphql.GraphQLList(userType),
      args:{
         degrees:{type:new graphql.GraphQLList(userType)}
      },
     resolve: function(source, args) {
        console.log(args);
        resolve(args);
      }
    }
})
})

I got this error. enter image description here

Basically i need to post the array from client graphql query and have to define query accordingly which i am unable to achieve. Any suggestions because i can't find any help over this issue..

like image 521
N.A Avatar asked Nov 16 '16 08:11

N.A


People also ask

How do you pass an argument in a GraphQL query?

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.

How do you pass two arguments in GraphQL query?

Multiple arguments can be used together in the same query. For example, you can use the where argument to filter the results and then use the order_by argument to sort them.


1 Answers

GraphQLObjectType is not a valid input type.

See Mutations and Input Types

"Input types can't have fields that are other objects, only basic scalar types, list types, and other input types."

You can use the suggestion above because GraphQLString is a scalar

degrees:{
    type:new graphql.GraphQLList(graphql.GraphQLString)
}

Otherwise, you would need to define a GraphQLInputObjectType

const userInputType = new GraphQLInputObjectType({
    name: 'userInput',
    fields: { /* put your fields here */ }
});
/* some code in between */

degrees:{
    type:new graphql.GraphQLList(userInputType)
}
like image 192
Lucas Blancas Avatar answered Oct 05 '22 14:10

Lucas Blancas