Thuta Learning
GraphQL
BasicWeb Developmentbeginner

Arguments and Variables

What you'll walk away with

  • Explain the core ideas behind Arguments and Variables
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

Every GraphQL field can accept arguments — syntax like `tutorial(id: "42")` parameterizes a single field. Instead of hardcoding values in the query string, declare a variable such as `$id: ID!` and send its value separately as a variables JSON object per request; this lets client code reuse the same query string and reduces injection risk. Default values (`$includeAuthor: Boolean = true`) and the `@include`/`@skip` directives let you conditionally include fields.

Connect it to a real scenario

The Tutorial Platform frontend reuses a single `TutorialById` query string across many ids by changing only the variables. When a mobile client does not need the author bio, it sends `$includeAuthor: false`, and the `@include(if: $includeAuthor)` directive skips the author sub-selection.

Try the working example

graphql
query TutorialById($id: ID!, $includeAuthor: Boolean = true) {
  tutorial(id: $id) {
    title
    summary
    author @include(if: $includeAuthor) {
      name
    }
  }
}

# variables: { "id": "42", "includeAuthor": false }
You should see
You can write fields with arguments and a reusable, variable-based query.

5-minute try-it

Write a `tutorials(difficulty: $difficulty)` query using a `$difficulty: Difficulty` variable, and run it with two different variable values.

One important caution

Do not concatenate values directly into the query string — user input there can create injection and caching problems. Use variables instead.

GraphQL — Queries and MutationsGraphQL

Easy traps

  • Do not concatenate values directly into the query string — user input there can create injection and caching problems. Use variables instead.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Write a `tutorials(difficulty: $difficulty)` query using a `$difficulty: Difficulty` variable, and run it with two different variable values.

You'll know it worked when: You can write fields with arguments and a reusable, variable-based query.

Arguments and Variables | Thuta Learning