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
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
Most helpful comment
Use thunk functions for defining your
fieldsproperty: