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

3

7

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)
    }
Resistencia answered 8/9, 2016 at 23:46 Comment(0)
R
6

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)
    }
Resistencia answered 9/9, 2016 at 2:10 Comment(2)
this is not a very good answer only because the question does not show the block of code the answer actually refers to, making it unable to follow for others. Sadly also the top answer on google.Epiglottis
For those following along, I answered my own question. The code I am referring to in the answer is in the question. Please see update.Resistencia
B
2

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.

Bennett answered 15/12, 2018 at 8:37 Comment(0)
X
1

I got the same error which in my case was caused by not properly declared fields property (caused by a typo). The fields property requires a list of GraphQLObjectFields that the type is expected to have but was missing in my case due to the typo.

Problem that caused me the error:

const PropertyType = new GraphQLObjectType({
    name: 'Property',
    feilds: () => ({               // Typo in the 'fields' property 
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        area: { type: GraphQLString }
    }) 
});

Error related to the above problem:

Error: Property fields must be an object with field names as keys or a function which returns such an object.

Xanthic answered 5/12, 2021 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.