嘿伙计们我刚开始学习如何使用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
};
},
};
}
答案 0 :(得分:1)
GraphQL执行程序在处理查询时调用resolve
函数。作为开发人员,您不必管理执行者的行为方式;您唯一的目标是指定(在resolve
函数中)字段返回的内容。您需要了解的有关执行程序的全部内容是它以递归方式调用每个字段,直到它到达查询树的所有分支,并将已解析的对象传递给层次结构。
当您定义GraphQL类型时,您可以指定它们具有哪些字段,每个字段返回的类型(例如,类型User
有一个名为address
的字段,其类型可以是Address
和所以)和一个resolve
函数将被执行以响应查询。 resolve
函数的第一个参数始终是父对象;在这种情况下,conn
。 (实际上,edge
会传递给resolveNode
,这是一种处理连接边缘的自定义方法,但这超出了此问题的范围。)