Why am I getting this error from my schema file?
Error:
graphql/jsutils/invariant.js:19
throw new Error(message);
^
Error: Entity fields must be an object with field names as keys or a function which returns such an object.
Error is around the entity
prop on PersonType
. The msg indicates that each of the fields on the Entity
should be an object, but I am not seeing any examples like this anywhere.
Basically, I am trying to get some data from the DuckDuckGo API based on a value returned from a Person query. The data returned from the API is an object with many properties, of which I am trying to use two to populate an entity
object on my Person
object.
I have taken a look at the type system docs but don't see the answer. http://graphql.org/docs/api-reference-type-system/
This is code running on Node and being served to a GraphiQL UI.
Any advice on this would be appreciated! Thanks.
Code:
const PersonType = new GraphQLObjectType({
name: 'Person',
description: '...',
fields: () => ({
name: {
type: GraphQLString,
resolve: (person) => person.name
},
url: {
type: GraphQLString,
resolve: (person) => person.url
},
films: {
type: new GraphQLList(FilmType),
resolve: (person) => person.films.map(getEntityByURL)
},
vehicles: {
type: new GraphQLList(VehicleType),
resolve: (person) => person.vehicles.map(getEntityByURL)
},
species: {
type: new GraphQLList(SpeciesType),
resolve: (person) => person.species.map(getEntityByURL)
},
entity: {
type: new GraphQLObjectType(EntityType),
resolve: (person) => getEntityByName(person.name)
}
})
});
const EntityType = new GraphQLObjectType({
name: 'Entity',
description: '...',
fields: () => ({
abstract: {
type: GraphQLString,
resolve: (entity) => entity.Abstract
},
image: {
type: GraphQLString,
resolve: (entity) => entity.Image
}
})
});
function getEntityByName(name) {
return fetch(`${DDG_URL}${name}`)
.then(res => res.json())
.then(json => json);
}
Update This is the code I am referring to that was giving the problem:
entity: {
type: EntityType, // <- no need to wrap this in GraphQLObjectType
resolve: (person) => getEntityByName(person.name)
}