嘿,伙计们,我刚刚开始学习如何使用react/graphql编程,我很难理解参数传递是如何工作的。在下面取自https://github.com/graphql/swapi-graphql的代码示例中,我不知道resolve函数何时填充参数"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
};
},
};
}发布于 2018-01-09 00:23:18
GraphQL执行器在处理查询时调用resolve函数。作为开发人员,您不必管理executor的行为;您唯一的目标是指定(在resolve函数中)字段返回的内容。关于executor,您需要知道的就是它递归地调用每个字段,直到它到达查询树的所有分支,在层次结构中向下传递已解析的对象。
在定义GraphQL类型时,您需要指定它们具有哪些字段、每个字段返回什么类型(例如,类型User具有一个名为address的字段,该字段可以是Address类型,依此类推),以及将在响应查询时执行的resolve函数。resolve函数的第一个参数始终是父对象;在本例中为conn。(实际上,edge被传递给resolveNode,这是一个用于处理连接边缘的自定义方法,但这超出了本问题的范围。)
https://stackoverflow.com/questions/41982138
复制相似问题