6

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..

2 Answers 2

4

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)
}
Sign up to request clarification or add additional context in comments.

Comments

2

I did something very similar recently. Your input arguments don't need to hold to the same type system as the when it's formatting the output data to send back. So your arg just needs to simply accept a list of strings or objects, or whatever standard type you want to send in.

In this case, I updated it to accept a list(array) of strings.

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

Also, I noticed on your user type, you have degrees surrounded with array brackets. Similar to your degrees input argument, you'll be outputting an array of strings. Try something like this:

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

Happy coding!

6 Comments

I have tried this too now it is giving this error. user.degrees field type must be Output Type but got: undefined.
can u please confirm that the code in userType is correct or not?
Gotcha, could you post up your userType? just edit your original post and post a github gist code snippit if it's large
my question already contains the userType.u can see the first code block.
Wow, not sure how I missed that O_O. Anywho, I updated my answer, looks like you just needed to format things differently to handle lists. Let me know if that works.
|

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.