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
});
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});
}
},
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With