HomeToolsAbout a20k

Positional Arguments

What is it

Arguments in Graphql provide a way for other node/entity connections and sub-fields to retain context from the parent node/entity

const resolvers = { Query: { user(parent, args, contextValue, info) { return users.find((user) => user.id === args.id); }, }, };

parent

The return value of the resolver for this field's parent (previous resolver in the resolver chain)

For top-level fields with no parent, this value is obtained from the rootValue function passed to Apollo server's constructor

args

An object that contains all GraphQL arguments provided for this field.

# query query { user(id: "4") } # arg passed to user resolver { "id": "4" }

context

Object shared across all resolvers executing for a particular operation.

Use this to share per-operation state

  • use this for auth information, dataloader instances, and anything else to track across resolvers

When calling a service method on a resolver field, to keep the context, you might have to pass the context explicitly as another parameter

info

Information aobut the operation's execution state.

  • field name, path to the field from the root, and etc.
© VincentVanKoh