Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: RootQueryType.resolve field config must be an object

im new to GraphQl, just created my first schema to see this error

Error: RootQueryType.resolve field config must be an object
    at invariant (/Applications/Node/users/node_modules/graphql/jsutils/invariant.js:19:11)
    at /Applications/Node/users/node_modules/graphql/type/definition.js:360:56
    at Array.forEach (native)
    at defineFieldMap (/Applications/Node/users/node_modules/graphql/type/definition.js:357:14)
    at GraphQLObjectType.getFields (/Applications/Node/users/node_modules/graphql/type/definition.js:311:44)
    at typeMapReducer (/Applications/Node/users/node_modules/graphql/type/schema.js:209:25)
    at Array.reduce (native)
    at new GraphQLSchema (/Applications/Node/users/node_modules/graphql/type/schema.js:98:34)
    at Object.<anonymous> (/Applications/Node/users/schema/schema.js:39:18)
    at Module._compile (module.js:569:30)

this is my schema

const graphql = require('graphql');
const _ = require('lodash');
const{
    GraphQLObjectType,
    GraphQLInt, 
    GraphQLString,
    GraphQLSchema
} = graphql;

const users = [
    {id:'23', firstName:'Bill', age:20},
    {id:'47', firstName:'Samantha', age:21}
];

const UserType = new GraphQLObjectType({
    name: 'User',
    fields:{
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age:{type: GraphQLInt}
    }
});

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields:{
        user: {
            type: UserType,
            args:{
               id: {type: GraphQLString}
            }
        },
        resolve(parentValue, args){
            return _.find(users, {id: args.id});
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQuery
});
like image 500
Dike Jude Avatar asked Jul 26 '17 14:07

Dike Jude


1 Answers

Your GraphQL schema is invalid because you misplace the resolve function in your RootQuery object.

Try this:

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields:{
        user: {
            type: UserType,
            args:{
               id: {type: GraphQLString}
            },
            resolve(parentValue, args){ // move the resolve function to here
                return _.find(users, {id: args.id});
            }
        },

    }
});
like image 148
Wing-On Yuen Avatar answered Nov 15 '22 23:11

Wing-On Yuen