GraphQL 解析 GraphQLObjectType
Posted
技术标签:
【中文标题】GraphQL 解析 GraphQLObjectType【英文标题】:GraphQL resolve GraphQLObjectType 【发布时间】:2017-09-19 13:52:55 【问题描述】:我正在使用express-graphql 以及以下查询:
invitations
sent
received
架构定义(简化)如下:
const InvitationType = new GraphQLObjectType(
name: 'InvitationType',
description: 'Friends invitations',
fields:
sent:
type: new GraphQLList(GraphQLString),
description: 'Invitations sent to friends.',
resolve()
return ['sentA'];
,
received:
type: new GraphQLList(GraphQLString),
description: 'Invitations received from friends.',
resolve()
return ['receivedA', 'receivedB'];
);
// Root schema
const schema = new GraphQLSchema(
query: new GraphQLObjectType(
name: 'RootQueryType',
fields:
invitations:
type: InvitationType // no resolve() method here.
)
);
但是,resolve
方法永远不会为 sent
和 received
字段调用。上面的查询返回:
data: invitations: sent: null, received: null
有没有办法解决嵌套字段(sent
和 received
)而不在父字段(invitations
)上定义 resolve()
方法?
【问题讨论】:
【参考方案1】:这对我有用!根据GraphQL Documentation,如果resolve方法返回一个非标量,执行将继续。所以下面的代码有效:
// Root schema
const schema = new GraphQLSchema(
query: new GraphQLObjectType(
name: 'RootQueryType',
fields:
invitations:
type: InvitationType,
resolve: () => () // Resolve returns an object.
)
);
希望这会有所帮助。干杯!
【讨论】:
以上是关于GraphQL 解析 GraphQLObjectType的主要内容,如果未能解决你的问题,请参考以下文章