Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL error - argument type must be Input Type but got: undefined

Tags:

I have

export const getPicture = {
    type: GraphPicture,
    args: {
        type: new GraphQLNonNull(GraphQLInt)
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

export const getPictureList = {
    type: new GraphQLList(GraphPicture),
    resolve(_, __, session) {
        return Picture.findAll({where: {owner: session.userId}});
    }
};

and

const query = new GraphQLObjectType({
    name: 'Queries',
    fields: () => {
        return {
            getPictureList: getPictureList,
            getPicture: getPicture
        }
    }
});

const schema = new GraphQLSchema({
    query: query,
    mutation: mutation
});

Which throws:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined.

getPictureList works fine, getPicture fails.

I have tried making getPicture a GraphQLLIst, doesn't help.

I have tried making the args type an Input Type, doesn't help.

Any ideas about what is going on here

The stack trace is:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined.
at invariant (/home/jrootham/dev/trytheseon/node_modules/graphql/jsutils/invariant.js:19:11)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:307:33
at Array.map (native)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:304:52
at Array.forEach (native)
at defineFieldMap (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:293:14)
at GraphQLObjectType.getFields (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:250:46)
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:224:27
at typeMapReducer (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:236:7)
at Array.reduce (native)
at new GraphQLSchema (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:104:34)
at Object.<anonymous> (/home/jrootham/dev/trytheseon/server/graphQL.js:45:16)
at Module._compile (module.js:399:26)
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:173:7)
at Module.load (module.js:345:32)
at Function.Module._load (module.js:302:12)
at Module.require (module.js:355:17)
at require (internal/module.js:13:17)
at Object.<anonymous> (/home/jrootham/dev/trytheseon/devServer.js:14:44)
at Module._compile (module.js:399:26)
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5)
like image 867
Jim Rootham Avatar asked Jun 13 '16 18:06

Jim Rootham


People also ask

How to define an input type in GraphQL?

To define an input type, use the input keyword followed by the name and curly braces ( {} ). Inside the curly braces, we list the fields and types as usual. Note that fields of an input type can be only a scalar, an enum, or another input type. checkInDate: String!

What is GraphQLObjectType?

class GraphQLObjectType. An object type within GraphQL that contains fields. class GraphQLInterfaceType. An interface type within GraphQL that defines fields implementations will contain.

Is it possible to use inheritance with GraphQL input types?

No, the spec does not allow input types to implement interfaces. And GraphQL type system in general does not define any form of inheritance (the extends keyword adds fields to an existing type, and isn't for inheritance).


2 Answers

Just for reference, I got here searching the same error just not with undefined, but with my own Product type:

... argument * must be Input Type but got Product

Turned out that in GraphQL what you pass to a Mutation should be either scalar type (String, Integer, ...) or user defined input type, not type type.

http://graphql.org/graphql-js/mutations-and-input-types/

input ProductInput {
  id: ID
  name: String
}

I was trying to pass in my Product type:

type Product {
  id: ID
  name: String
}

This feels a bit redundant, but guess, I'll be glad for having it separated later.

like image 105
kub1x Avatar answered Oct 02 '22 18:10

kub1x


The problem isn't the return type of the query but the type you have specified to your args.

export const getPicture = {
    type: GraphPicture,
    args: {
        type: new GraphQLNonNull(GraphQLInt)
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

Assuming you meant to have an argument named "type" it should be:

export const getPicture = {
    type: GraphPicture,
    args: {
        type: {
             type: new GraphQLNonNull(GraphQLInt)
        }
    },
    resolve(_, args) {
        return Picture.findByPrimary(args.id);
    }
};

here is an example from the graphql-js repo:

hero: {
  type: characterInterface,
  args: {
    episode: {
      description: 'If omitted, returns the hero of the whole saga. If ' +
                   'provided, returns the hero of that particular episode.',
      type: episodeEnum
    }
  },
  resolve: (root, { episode }) => getHero(episode),
},

See the full schema from the graphql-js repo here: https://github.com/graphql/graphql-js/blob/master/src/tests/starWarsSchema.js

like image 31
Brad Decker Avatar answered Oct 02 '22 17:10

Brad Decker