Your first query in GraphiQL

Beginner · 7 min

Start a GraphQL Yoga server, open its built-in GraphiQL explorer, read the docs panel, write a query with autocomplete, and use variables and headers.

Before thisWhat GraphQL is and why it exists

Jamie Barton · 7 min

Your first query in GraphiQL

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Jamie BartonReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • GraphiQL is a browser IDE for GraphQL; GraphQL Yoga serves it at the same URL as the API when the request asks for HTML.
  • The docs panel and autocomplete are built from the server's introspection data, so they always match the running schema.
  • A query is run against the endpoint as a normal request; nothing in GraphiQL is special beyond the editor around it.
  • Variables and request headers have their own panes, which is where you test arguments and authentication without editing the query string.
  • Typos and unknown fields are rejected by validation with a message and a line and column before any data is fetched.

Why a playground exists

A REST API is hard to explore without documentation. You have a base URL and a list of paths from somewhere, and you try them with curl or a client. A GraphQL server can describe itself, so a tool can show you what is available and check what you type against it. That tool is usually GraphiQL, a browser-based editor maintained under the GraphQL Foundation.

Think of it as a REPL for an API: type, run, read the result, adjust.

Get something to explore

Create a scratch folder and install graphql and graphql-yoga (I used 17.1.0 and 6.1.0 on Node 26). Add "type": "module" to package.json, then save this as server.mjs:

import { createServer } from 'node:http'
import { createYoga, createSchema } from 'graphql-yoga'

const books = [
  { id: '10', title: 'The Dispossessed', year: 1974, authorId: '1' },
  { id: '12', title: 'Cosmos', year: 1980, authorId: '2' },
]
const authors = [
  { id: '1', name: 'Ursula K. Le Guin' },
  { id: '2', name: 'Carl Sagan' },
]

const schema = createSchema({
  typeDefs: /* GraphQL */ `
    type Author { id: ID! name: String! books: [Book!]! }
    type Book { id: ID! title: String! year: Int author: Author! }
    type Query {
      book(id: ID!): Book
      books(limit: Int = 10): [Book!]!
    }
  `,
  resolvers: {
    Query: {
      book: (_, { id }) => books.find((b) => b.id === id),
      books: (_, { limit }) => books.slice(0, limit),
    },
    Book: { author: (b) => authors.find((a) => a.id === b.authorId) },
    Author: { books: (a) => books.filter((b) => b.authorId === a.id) },
  },
})

createServer(createYoga({ schema })).listen(4000, () =>
  console.log('http://localhost:4000/graphql'),
)

Run node server.mjs. Do not worry about the resolvers yet. They are just functions that return data; the resolvers lesson explains them.

Open the explorer

Visit http://localhost:4000/graphql in a browser. I checked from the command line that a request to that URL with Accept: text/html returns an HTML page titled “Yoga GraphiQL” (the same URL with a JSON request runs queries). I did not drive the page in a browser for this lesson, so the UI steps below follow Yoga’s and GraphiQL’s standard layout and the video’s description.

  • Docs panel. Open it and click Query. You will see book(id: ID!): Book and books(limit: Int = 10): [Book!]!. Click Book to see its fields. This panel is read from the server’s own schema. Nothing about it is written by hand, which is introspection at work (a later lesson, introspection, goes into it).
  • Editor. Type { and press Ctrl+Space for completions based on the schema. Names and arguments are suggested; wrong ones are underlined.
  • Run. The play button, or Ctrl+Enter, sends the query.

Try this:

{
  book(id: "12") {
    title
    year
    author { name books { title } }
  }
}

Against the server above the result is:

{"data":{"book":{"title":"Cosmos","year":1980,"author":{"name":"Carl Sagan","books":[{"title":"Cosmos"}]}}}}

I ran that server and sent this query with curl to confirm the output. The result panel shows the same JSON, pretty-printed.

Make a mistake on purpose

Change title to titel. GraphiQL flags it as you type, and if you run it anyway the server replies:

{"errors":[{"message":"Cannot query field \"titel\" on type \"Book\". Did you mean \"title\"?","locations":[{"line":1,"column":20}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}

That is the response from the server above, sent with curl on one line, so your line and column will differ. Note the “Did you mean” hint. No resolver ran. The server validated the query against the schema first and rejected it. This early, specific feedback is a large part of why people like GraphQL tooling.

Variables and headers

At the bottom of the editor are two small panes. Open Variables and write JSON:

{ "id": "10" }

Then change the query to use it:

query GetBook($id: ID!) {
  book(id: $id) { title }
}

This is how real client code sends requests, with the text of the query fixed and the values passed separately. The full story is in variables and operation names. The Headers pane lets you send things like Authorization: Bearer ... once authentication exists.

Other things the video mentions

Jamie Barton’s description lists exploring documentation, performing operations, tabs, prettifying, variables, and request headers. Tabs let you keep several queries open at once, and prettify reformats a query. Both are things I would try early because they make exploring a larger API less painful.

What the video covers

“Working with GraphiQL” by Jamie Barton is about seven minutes, published in May 2022. Its description names docs exploration, running operations, tabs, prettify, variables, and request headers. I could not read a transcript, so I cannot say which API it uses or how deep each part goes. The interface has been updated since 2022, so button positions may differ slightly from what you see. The runner-up is Net Ninja’s “Query Basics”, which uses Apollo Sandbox, a similar tool, rather than GraphiQL.

Where this fits

You can now send a query and read a response by hand. But what is actually going over the wire when you press run? Next: GraphQL over HTTP.

Check yourself

  1. 01Where does GraphiQL get the list of types and fields it shows in the docs panel?

    From introspection: it sends a query that asks the server to describe its schema, then renders the answer.

  2. 02What is the quickest way to find what you can ask for at the root of an API?

    Open the docs panel and look at the Query type, whose fields are the entry points, or type a field name and use autocomplete.

  3. 03Why use the variables pane rather than writing values into the query?

    It keeps the query text constant while the values change, which matches how real clients send requests.

  4. 04What do you see if you query a field that does not exist?

    An errors array with a message like Cannot query field on type and the line and column, and no data, because validation fails before execution.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark