Reputation: 1
Hey guys I just started learning how to code using react/graphql, and I have a very hard time understanding how parameter passing works. In the code example below taken from https://github.com/graphql/swapi-graphql , I don't know when the resolve function populates the arguments "edge" and "conn". Could someone give me some insight?
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
};
},
};
}
Upvotes: 0
Views: 373
Reputation: 8751
The GraphQL executor calls the resolve
function when it processes a query. You as a developer don't have to manage how the executor behaves; your only goal is to specify (in the resolve
function) what the field returns. All you need to know about the executor is that it calls each field, recursively, until it reaches all branches of the query tree, passing the resolved objects down the hierarchy.
When you define GraphQL types, you specify what fields they have, what type each field returns (e.g. a type User
has a field called address
which can be of type Address
and so on), and a resolve
function that will be executed in response to a query. The first parameter to the resolve
function is always the parent object; in this case, conn
. (Actually, edge
is passed to resolveNode
which is a custom method for handling connection edges but that's beyond the scope of this question.)
Upvotes: 1