【问题标题】:GraphQL error "fields must be an object with field names as keys or a function which returns such an object."GraphQL 错误“字段必须是以字段名称作为键的对象或返回此类对象的函数。”
【发布时间】:2017-01-17 00:34:37
【问题描述】:

为什么我的架构文件会出现此错误?

错误:

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.

错误出现在 PersonType 上的 entity 属性附近。消息表明Entity 上的每个字段都应该是一个对象,但我在任何地方都没有看到任何这样的示例。

基本上,我试图根据从 Person 查询返回的值从 DuckDuckGo API 获取一些数据。从 API 返回的数据是一个具有许多属性的对象,我尝试使用其中两个来填充我的 Person 对象上的 entity 对象。

我查看了类型系统文档,但没有看到答案。 http://graphql.org/docs/api-reference-type-system/

这是在 Node 上运行并提供给 GraphiQL UI 的代码。

对此的任何建议将不胜感激!谢谢。

代码:

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);
}

更新 这是我所指的导致问题的代码:

entity: {
        type: EntityType, // <- no need to wrap this in GraphQLObjectType
        resolve: (person) => getEntityByName(person.name)
    }

【问题讨论】:

    标签: javascript node.js graphql graphql-js


    【解决方案1】:

    EntityType 已定义为GraphQLObjectType。因此,无需再次将GraphQLObjectType 包裹在EntityType 周围。

    修改PersonTypeentity字段如下:

        entity: {
            type: EntityType, // <-- Duh!
            resolve: (person) => getEntityByName(person.name)
        }
    

    【讨论】:

    • 这不是一个很好的答案,只是因为问题没有显示答案实际引用的代码块,使其无法为其他人遵循。可悲的是,谷歌上的最佳答案也是如此。
    • 对于那些跟随的人,我回答了我自己的问题。我在答案中提到的代码在问题中。请查看更新。
    【解决方案2】:

    在我的情况下,它是一个空类型导致它。

    变化

    type SomeType {
    }
    

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

    为我解决了这个问题。

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误,在我的情况下是由未正确声明 fields 属性引起的(由错字引起)。 fields 属性需要一个 GraphQLObjectFields 列表,该类型应具有该类型,但在我的情况下由于拼写错误而丢失。

      导致我出错的问题:

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

      与上述问题相关的错误:

      错误:属性字段必须是字段名称作为键的对象或返回此类对象的函数。

      【讨论】:

        猜你喜欢
        • 2019-03-10
        • 1970-01-01
        • 2017-06-12
        • 2018-01-01
        • 2019-09-20
        • 2019-08-06
        • 1970-01-01
        • 2018-08-15
        • 2016-08-03
        相关资源
        最近更新 更多