Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL error "fields must be an object with field names as keys or a function which returns such an object."

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)
    }
like image 439
Jazzy Avatar asked Sep 08 '16 23:09

Jazzy


2 Answers

EntityType is already defined as a GraphQLObjectType. So, there's no need to wrap GraphQLObjectType around EntityType again.

Change the entity field of PersonType as below:

    entity: {
        type: EntityType, // <-- Duh!
        resolve: (person) => getEntityByName(person.name)
    }
like image 52
Jazzy Avatar answered Sep 27 '22 19:09

Jazzy


In my case it was an empty type which caused it.

Changing

type SomeType {
}

to

type SomeType {
  # Structs can't be empty but we have no useful fields to include so here we are.
  placeholder: String
}

Fixed the issue for me.

like image 25
M-WaJeEh Avatar answered Sep 27 '22 19:09

M-WaJeEh