GraphQL Args error: argument type must be an input type, but received: GraphQLObjectType (config) function {

At the start of the server ( node index.js ) I get the following error with my GraphQL NodeJS server:

Error: Query.payment (data argument type :) should be an input type, but received: function GraphQLObjectType (config) {_classCallCheck (this, GraphQLObjectType);

This error occurred when I changed the original arguments from a string

  args: { data: { type: graphQL.GraphQLString } }, 

To the type of object:

  args: { data: { type: graphQL.GraphQLObjectType } }, 

I need an object type as I need to send several fields as parameters.

GraphQL Server:

 var Query = new graphQL.GraphQLObjectType({ name: 'Query', fields: { payment: { type: graphQL.GraphQLString, args: { data: { type: graphQL.GraphQLObjectType } }, resolve: function (_, args) { // There will be more data here, // but ultimately I want to return a string return 'success!'; } } } }); 

How can I allow him to accept an object?


Frontend (if necessary, but an error occurs before I even send this message):

 var userQuery = encodeURIComponent('{ payment ( data: { user : "test" } )}'); $.get('http://localhost:4000/graphql?query=' + userQuery, function (res) { //stuff }); 
+6
source share
1 answer

If you want to use Object as an argument, you should use GraphQLInputObjectType instead of GraphQLObjectType . And keep in mind that GraphQL is strongly type-based, so you are not allowed to use the generic GraphQLObjectType type as arg, and then dynamically query args. You must explicitly define all the possible fields in this source object (and choose which one will be required and which will not)

Try using this approach:

 // your arg input object var inputType = new GraphQLInputObjectType({ name: 'paymentInput', fields: { user: { type: new GraphQLNonNull(GraphQLString) }, order: { type: GraphQLString }, ...another fields } }); var Query = new graphQL.GraphQLObjectType({ name: 'Query', fields: { payment: { type: graphQL.GraphQLString, args: { data: { type: new GraphQLNonNull(inputType) } }, resolve: function (_, args) { // There will be more data here, // but ultimately I want to return a string return 'success!'; } } } }); 
+12
source

All Articles