gpt4 book ai didi

javascript - Graphql参数传递

转载 作者:行者123 更新时间:2023-12-03 05:06:48 24 4
gpt4 key购买 nike

嘿伙计们,我刚刚开始学习如何使用react/graphql 进行编码,并且我很难理解参数传递的工作原理。下面的代码示例取自 https://github.com/graphql/swapi-graphql ,我不知道解析函数何时填充参数“edge”和“conn”。有人能给我一些见解吗?

export function connectionFromUrls(
name: string,
prop: string,
type: GraphQLOutputType
): GraphQLFieldConfig<*, *> {
const {connectionType} = connectionDefinitions({
name,
nodeType: type,
resolveNode: edge => getObjectFromUrl(edge.node),
connectionFields: () => ({
totalCount: {
type: GraphQLInt,
resolve: conn => conn.totalCount,
description:
`A count of the total number of objects in this connection, ignoring pagination.
This allows a client to fetch the first five objects by passing "5" as the
argument to "first", then fetch the total count so it could display "5 of 83",
for example.`
},
[prop]: {
type: new GraphQLList(type),
resolve: conn => conn.edges.map(edge => getObjectFromUrl(edge.node)),
description:
`A list of all of the objects returned in the connection. This is a convenience
field provided for quickly exploring the API; rather than querying for
"{ edges { node } }" when no edge data is needed, this field can be be used
instead. Note that when clients like Relay need to fetch the "cursor" field on
the edge to enable efficient pagination, this shortcut cannot be used, and the
full "{ edges { node } }" version should be used instead.`
}
})
});
return {
type: connectionType,
args: connectionArgs,
resolve: (obj, args) => {
const array = obj[prop] || [];
return {
...connectionFromArray(array, args),
totalCount: array.length
};
},
};
}

最佳答案

GraphQL 执行器在处理查询时调用 resolve 函数。作为开发人员,您不必管理执行器的行为;您只需管理执行器的行为即可。您唯一的目标是指定(在 resolve 函数中)字段返回的内容。关于执行器,您需要了解的只是它递归地调用每个字段,直到到达查询树的所有分支,并将解析的对象沿层次结构传递。

当您定义 GraphQL 类型时,您可以指定它们具有哪些字段,每个字段返回什么类型(例如,User 类型有一个名为 address 的字段,该字段可以是Address 等),以及将执行以响应查询的 resolve 函数。 resolve 函数的第一个参数始终是父对象;在这种情况下,conn。 (实际上,edge 被传递给 resolveNode,这是处理连接边缘的自定义方法,但这超出了本问题的范围。)

关于javascript - Graphql参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41982138/

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