gpt4 book ai didi

javascript - GraphQL 解析 GraphQLObjectType

转载 作者:行者123 更新时间:2023-11-29 23:47:22 25 4
gpt4 key购买 nike

我正在使用 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 方法永远不会为 sentreceived 字段调用。上面的查询返回:

{data: {invitations: {sent: null, received: null}}}

有没有什么方法可以解析嵌套字段(sentreceived)而不用在父级(邀请) 字段?

最佳答案

这对我有用!根据GraphQL Documentation , 如果 resolve 方法返回非标量,则执行将继续。所以下面的代码有效:

// Root schema
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
invitations: {
type: InvitationType,
resolve: () => ({}) // Resolve returns an object.
}
}
})
});

希望这对您有所帮助。干杯!

关于javascript - GraphQL 解析 GraphQLObjectType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com