13. The args parameter
1m

The args parameter

The args parameter contains any arguments that are passed into the field by the client. It's the second positional parameter in a resolver function.

TODO: alt text

Example:

In the GraphQL schema below, the movie field has one argument called title.

type Query {
movie(title: String!): Movie
}

Within the movie(title: String!) resolver, we can access the title field argument using the args parameter:

const resolvers = {
Query: {
movie: (parent, args) {
const movieTitle = args.title
}
}
}

Field arguments in Sandbox

In Apollo Sandbox, you can pass arguments into functions by using the Variables panel.

The Variables panel contains a JSON object of key-value pairs representing data you want to use within your GraphQL operation. For example, you can set up a variable called movieTitle using the following Variables object:

{
"movieTitle": "Singin' in the Rain"
}

Then to use the variable in your GraphQL operation, add it to the first line, after the operation name. You'll need to use a $ before the variable name. You can also pass the variable value into specific fields. In the example below, the movieTitle variable gets passed into the movie field as an argument called title.

query GetMovie($movieTitle: ID!) {
movie(title: $movieTitle) {
cast
}
}
Previous
Next