GraphQL错误“字段必须是字段名称作为键的对象或返回这样的对象的函数。

为什么我从模式文件中得到这个错误?

错误:

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. 

错误发生在PersonTypeentity prop上。 味精指示Entity上的每个字段都应该是一个对象,但是我没有看到任何地方的例子。

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

我已经看过types系统文档,但没有看到答案。 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) } 

EntityType已经被定义为一个GraphQLObjectType 。 因此,不需要再次在EntityType包装GraphQLObjectType

更改PersonTypeentity字段如下:

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