gpt4 book ai didi

Graphqlresolver for common subfield under different parent field type(不同父字段类型下公用子字段的图形解析器)

转载 作者:bug小助手 更新时间:2023-10-25 13:40:20 27 4
gpt4 key购买 nike



I am new to graphql, I have requirement where multiple different types have the same field which I am fetching from downstream call, can you please suggest how I can write the graphqlresolver for this subfield?

我是GraphQL的新手,我有一个要求,多个不同的类型有相同的字段,我是从下游调用获取的,你能建议我如何编写这个子字段的Graphql解析器吗?


{
Type1 {
title
Field1
},
Type2 {
Field1
}
}

here Field1 value I will be getting I want to write resolver which will fetch value based on parent type also if the downstream support batch api how can i call api only once for type1 and type2 fields?

这里Field1值我将得到我想写解析器,它将根据父类型获取值,如果下游支持批处理API,我怎么能只为type1和type2字段调用一次API?


更多回答
优秀答案推荐

You can write a single resolver function for Field1 and add it to both the Type1 and Type2 types.

您可以为Field1编写单个解析器函数,并将其添加到Type1和Type2类型。


const Field1 = (parent,args,context) => …return Field1;

const Type1 = {
title: (parent,args,context) => …return the title,
Field1,
}

const Type2 = {
Field1
}

const resolvers = {...Type1,...Type2}

Beware that this function will be called with a different parent variable depending on which type is being resolved.

注意,根据要解析的类型,将使用不同的父变量调用此函数。


You can't make GraphQL call your Field1 function only once because the GraphQL server is stateless and the resolution of this field under different types happens independently of each other.

您不能让GraphQL只调用您的Field1函数一次,因为GraphQL服务器是无状态的,并且在不同类型下对该字段的解析彼此独立进行。


If you want to minimize resolution of an external API behind Field1 then you will need to cache the results of that API call.

如果您希望最小化Field1后面的外部API的解析,则需要缓存该API调用的结果。


更多回答

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