GraphQL 错误 "fields must be an object with field names as keys or a function which returns such an object."
GraphQL error "fields must be an object with field names as keys or a function which returns such an object."
为什么我的架构文件会出现此错误?
错误:
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/
这是节点上的代码 运行 并提供给 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
。因此,没有必要再次将 GraphQLObjectType
环绕在 EntityType
周围。
更改 PersonType
的 entity
字段如下:
entity: {
type: EntityType, // <-- Duh!
resolve: (person) => getEntityByName(person.name)
}
在我的例子中,这是一个空类型导致的。
改变
type SomeType {
}
至
type SomeType {
# Structs can't be empty but we have no useful fields to include so here we are.
placeholder: String
}
为我解决了这个问题。
我遇到了同样的错误,在我的情况下,这是由于未正确声明 fields
属性(由拼写错误引起)引起的。 fields
属性 需要一个 GraphQLObjectFields
的列表,该类型应该具有但由于错字而在我的情况下丢失了。
导致我出错的问题:
const PropertyType = new GraphQLObjectType({
name: 'Property',
feilds: () => ({ // Typo in the 'fields' property
id: { type: GraphQLID },
name: { type: GraphQLString },
area: { type: GraphQLString }
})
});
与上述问题相关的错误:
Error: Property fields must be an object with field names as keys or a function which returns such an object.
为什么我的架构文件会出现此错误?
错误:
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/
这是节点上的代码 运行 并提供给 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
。因此,没有必要再次将 GraphQLObjectType
环绕在 EntityType
周围。
更改 PersonType
的 entity
字段如下:
entity: {
type: EntityType, // <-- Duh!
resolve: (person) => getEntityByName(person.name)
}
在我的例子中,这是一个空类型导致的。
改变
type SomeType {
}
至
type SomeType {
# Structs can't be empty but we have no useful fields to include so here we are.
placeholder: String
}
为我解决了这个问题。
我遇到了同样的错误,在我的情况下,这是由于未正确声明 fields
属性(由拼写错误引起)引起的。 fields
属性 需要一个 GraphQLObjectFields
的列表,该类型应该具有但由于错字而在我的情况下丢失了。
导致我出错的问题:
const PropertyType = new GraphQLObjectType({
name: 'Property',
feilds: () => ({ // Typo in the 'fields' property
id: { type: GraphQLID },
name: { type: GraphQLString },
area: { type: GraphQLString }
})
});
与上述问题相关的错误:
Error: Property fields must be an object with field names as keys or a function which returns such an object.