Lesson 1.1. What WPGraphQL Is

A home page in seven requests

A Next.js frontend shows the site name, the three latest posts, their authors and categories on the home page. With the standard REST routes it looked like this: /wp-json/ for the site name, /wp/v2/posts, then /wp/v2/users/{id} for each author and /wp/v2/categories?include=…. Every response had dozens of fields, and the card showed three of them. The page waited on a chain of requests, and half the traffic went to data nobody ever saw.

With WPGraphQL it's one query. And the response contains only what you asked for.

Goal

In 10 minutes you will:

  • explain what WPGraphQL is and where its endpoint lives;
  • write your first query and get a response of the same shape;
  • add related data (the author) to the same query;
  • remove unneeded fields from a card.

Key concepts

A GraphQL layer on top of WordPress

WPGraphQL is a free open source plugin (GPL-3.0) that adds an extendable GraphQL API to WordPress. Post types, taxonomies, users, media and custom fields become one typed schema behind one endpoint — /graphql.

flowchart LR
  F["Frontend<br/>Next.js · app · widget"] --> E["/graphql<br/>one endpoint"]
  E --> S["Schema<br/>Post · User · Term · Media"]
  S --> W["WordPress<br/>queries · DB"]
REST APIWPGraphQL
many routes: /wp/v2/posts, /wp/v2/users…one endpoint /graphql
the server decides which fields to returnthe client lists the fields it needs
related data — separate requests or _embedrelated data — a branch of the same query
documentation — a list of routesdocumentation — the schema itself

Query shape = response shape

GRAPHQL
query {
  generalSettings {
    title
    url
  }
}
JSON
{
  "data": {
    "generalSettings": {
      "title": "4WP Training Blog",
      "url": "https://training.4wp.dev"
    }
  }
}

You don't get description, language or timezone, because you didn't ask for them. That is the core idea of GraphQL.

Lists are connections

GRAPHQL
query {
  posts(first: 3) {
    nodes {
      title
      uri
      author {
        node {
          name
        }
      }
    }
  }
}

posts is a connection: the first argument limits the count (10 without it, 100 max by default), and the items live in nodes. The author is an edge to a single object: author { node { … } }. More on this in lesson 1.2.

GraphiQL and the terminal

Once WPGraphQL is installed, the admin gets a GraphiQL IDE: a query editor with autocomplete and a schema explorer. In this course we work in a GraphQL terminal right inside the lesson: same logic, a training schema with WPGraphQL type names and test data from the "4WP Training Blog". Nothing to install, and no live site to touch.

Does a regular blog need WPGraphQL?

Only if something other than your theme reads the content: a headless frontend, a mobile app, another site. A blog rendered by a theme doesn't need WPGraphQL.

Code / requests

On a real site with WPGraphQL you can send the same query with curl:

BASH
curl -s https://site.test/graphql \
  -H 'Content-Type: application/json' \
  -d '{"query":"{ generalSettings { title url } }"}'

# Public reads also work over GET
curl -s 'https://site.test/graphql?query=%7BgeneralSettings%7Btitle%7D%7D'

Common mistakes

SymptomCauseFix
/graphql → 404WPGraphQL is inactive or permalinks are "plain"activate the plugin; on plain permalinks use /?graphql
The response is as heavy as RESTa "just in case" query with content in a listonly the fields the component shows
10 posts came back, 3 were neededno firstposts(first: 3)
author { name } throws an errorauthor is an edge, not a Userauthor { node { name } }

Practice

Open the GraphQL terminal and complete four steps:

  1. The site title and description from generalSettings.
  2. The three latest posts: title and uri.
  3. Add the author's name to each post.
  4. The list card shows only title and excerpt — remove content.

<!– GraphQL terminal: graphql/lesson-1-1-what-is-wpgraphql.json (fence “graphql-terminal“ after LMS4WP integration) –>

Interview Questions

An open source plugin that adds a GraphQL API to WordPress: one typed schema of posts, taxonomies, users, media and custom fields behind one endpoint, /graphql.

In REST, the server defines the response shape for each route. In GraphQL, the client lists the fields, and the response comes back in exactly that shape, with related data in the same request.

10 by default. The default maximum is 100, and the graphql_connection_max_query_amount filter changes it.

No. It's needed when another client reads the content: a headless frontend, an app, another site.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.