Fluidbyte
Fluidbyte

Reputation: 5210

GraphQL Query.resolve must be an object

I'm new to GraphQL and trying to get a basic query setup against a mock data source that just resolves a promise with a filter to pull the record by id. I have the following:

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLList
} = require('graphql')

const db = require('../db')

const getUserById = (id) => db.read(id)

const UserType = new GraphQLObjectType({
  name: 'User',
  description: 'User Type',
  fields: () => ({
    first_name: {
      type: GraphQLString
    },
    last_name: {
      type: GraphQLString
    },
    email: {
      type: GraphQLString
    },
    friends: {
      type: new GraphQLList(UserType),
      resolve: (user) => user.friends.map(getUserById)
    }
  })
})

const QueryType = new GraphQLObjectType({
  name: 'Query',
  description: 'User Query',
  fields: () => ({
    user: {
      type: UserType,
      args: {
        id: { type: GraphQLString }
      }
    },
    resolve: (root, args) => getUserById(args.id)
  })
})

const schema = new GraphQLSchema({
  query: QueryType
})


module.exports = schema

When I try to run this with graphQLHTTP I get the following:

Error: Query.resolve field config must be an object

I've been following Zero to GraphQL in 30 Minutes and can't figure out what I'm doing wrong.

Upvotes: 0

Views: 1002

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84667

You've accidentally made the resolver for the user query one of the fields on the Query type. Move it inside the user field and you should be good.

Upvotes: 1

Related Questions