Express-graphql: ReferenceError: ...Type is not defined when defining GraphqlQueryType recursively

Created on 6 Aug 2017  路  2Comments  路  Source: graphql/express-graphql

Dear all

I'm trying to define PersonType which including a friends field as GraphqlList of PersonType

friends: {
    type: new GraphQLList(PersonType),
}

Unfortunately I got error message

ReferenceError: PersonType is not defined

My entire schema definition is shown below

const PersonType = new GraphQLObjectType({
    name: 'Person',
    fields: {
        firstName: {
            type: GraphQLString,
            resolve(person) {
                return person.firstName
            }
        },
        lastName: {
            type: GraphQLString,
            resolve(person) {
                return person.lastName
            }
        },
        friends: {
            type: new GraphQLList(PersonType),
            resolve() {
                ...
            }
        }
    }
})

const QueryRootType = new GraphQLObjectType({
    name: 'Query',
    fields: {
        person: {
            type: PersonType,
            args: {
                id: { type: GraphQLInt, defaultValue: 10001 }
            },
            resolve(root, args) {
                ...
            }
        }
    }
})

Please guide how to fix this error
Thanks

Most helpful comment

Use thunk functions for defining your fields property:

const PersonType = new GraphQLObjectType({
  name: 'Person',
  fields: () => ({ // << lambda function
    // Define your fields
  })
});

All 2 comments

Use thunk functions for defining your fields property:

const PersonType = new GraphQLObjectType({
  name: 'Person',
  fields: () => ({ // << lambda function
    // Define your fields
  })
});

@VojtechStep thanks
it's working now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arunoda picture arunoda  路  4Comments

mkermani144 picture mkermani144  路  5Comments

justinmchase picture justinmchase  路  4Comments

dhurlburtusa picture dhurlburtusa  路  4Comments

KieronWiltshire picture KieronWiltshire  路  3Comments