Arguments
Pass arguments to GraphQL fields to choose, filter and limit data, with defaults, required arguments, enum and ID arguments, and how the validator checks them.
Arguments
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Any field can declare arguments in the schema, and the client passes values in parentheses after the field name: books(genre: SCIENCE, limit: 1).
- Arguments are named and unordered; an argument with a default can be omitted, and one typed with ! and no default is required.
- Arguments can be passed to any field at any depth, not only to root fields, which is how you filter a nested list.
- The validator checks argument names and types before execution: unknown arguments, missing required ones, and wrong literal types are all rejected.
- Arguments are the inputs of a field's resolver, so the client's choice of values reaches your database query.
Fields as functions
In the previous lesson a field was something you pick, like a column. That works for title, but a root field like book needs more: which book? A field in GraphQL is closer to a function. It can take arguments that change what it returns.
You declare them in the schema, in parentheses after the field name:
type Query {
book(id: ID!): Book
books(genre: Genre, limit: Int = 10): [Book!]!
}
and the client supplies values the same way:
{ books(genre: SCIENCE, limit: 1) { title } }
I ran that on graphql-js 17.1.0 against a three-book shelf and got [{"title":"Cosmos"}]. Arguments are named, so order does not matter, and you can leave out any that are optional.
Required, optional, default
Three states, controlled by the type and the =:
| Declaration | Meaning |
|---|---|
id: ID! |
required; leaving it out is a validation error |
genre: Genre |
optional; the resolver sees it as undefined if absent |
limit: Int = 10 |
optional; the resolver sees 10 if absent |
Trying each of those:
{ books(limit: 1) { title } }returned the first book.genrewas left off and all genres were included.{ books { title } }returned all three, becauselimitdefaulted to 10.{ book { title } }was rejected: “Argument “Query.book(id:)” of type “ID!” is required, but it was not provided.”{ book(id: "10", x: 1) { title } }was rejected: “Unknown argument “x” on field “Query.book”.”
All four came back from the validator, before any resolver ran. A typo in an argument name cannot silently do nothing. It is an error.
Types of values
An argument can be any input type: a scalar, an enum, a list, or an input object (input types). Literals follow JSON-like rules, with some differences:
- Strings use double quotes:
"12". - Enum values are bare:
SCIENCE. IDaccepts a string or an integer;book(id: 10)worked andidarrived as"10".- Lists use brackets:
ids: [1, 2]. A single value where a list is expected is wrapped for you. - Objects use braces and unquoted keys:
{ genre: "sf" }.
Passing the wrong kind of literal is an error. Int rejects 1.5 and "3", as shown in scalar types.
Arguments at any depth
Any field can take arguments, not only root fields. If Author.books declared limit, you could write:
{
author(id: "1") {
name
books(limit: 2) { title }
}
}
A real use is filtering a nested list without a separate endpoint. In REST you add query parameters to /authors/1/books. In GraphQL the nested field takes them. This is also where the performance risk lives: a client can put a large limit on a field at every level. Servers cap and paginate those lists, which is the subject of pagination.
Where arguments land in your code
In graphql-js, a resolver receives its field’s arguments as an object (the first argument of an object-style resolver, or the second of a function-style resolver). With the object-style root value I used in this group:
const rootValue = {
books: ({ genre, limit }) =>
books.filter((b) => !genre || b.genre === genre).slice(0, limit),
}
Defaults are already applied by the time the resolver runs. An absent optional argument with no default arrives as undefined. Context and resolver arguments lists exactly what each resolver parameter holds.
Two cautions
Arguments are user input. Anything a client sends can be hostile. If genre ends up in SQL, parameterise it. If limit has no ceiling, someone will ask for a million. An enum argument at least restricts the input to defined names, which is one more reason to use enums.
Do not smuggle literals into the query string. The next lessons cover variables: instead of writing book(id: "12") into the query text, you declare $id and send the value separately. For anything beyond exploration in GraphiQL, that is how clients should send arguments.
What the video covers
This is a clip of My Digital Diaries’ “Anatomy of a GraphQL Query Explained” (EP02, October 2026): 13:13 to 17:10, the chapter its description labels “Arguments”. It is a short slice of a 25-minute episode and I judged it from the chapter list only, with no transcript. The surrounding chapters (fields, selection sets, aliases) are what the neighbouring lessons use. I did not find a better dedicated video: the others I saw were older, in a different stack, or from channels I could not vouch for, so deeper is empty.
Where this fits
Arguments let you ask for the same field with different values. That raises a puzzle: what if you want two of the same field in one query? Next: aliases.
Check yourself
How does a client pass two arguments to one field?
Inside the parentheses, separated by commas or whitespace, as name: value pairs in any order, such as books(genre: SCIENCE, limit: 1).
What happens if you omit a required argument?
The validator rejects the query with a message such as Argument Query.book(id:) of type ID! is required, but it was not provided.
What value does a resolver see for an argument with a default that the client omitted?
The default declared in the schema, since the executor applies it before calling the resolver.
Can a nested field have arguments?
Yes, any field can, so a query can filter or limit a list deep inside the response.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.