Technical Details

GraphQL vs REST API: Which is the Best Choice for Headless WordPress?

โ† Technical Details
2023-01-24 ยท 4 min read
GraphQL vs REST API: Which is the Best Choice for Headless WordPress?
Discuss this article with your AI
Copy page

๐Ÿ’ก Quick Summary (TL;DR):

  • WordPress REST API: Supported out-of-the-box. Great for simple setups, but prone to over-fetching (large payloads) and under-fetching (requiring multiple round-trips for nested data like tags, categories, or featured images).
  • WPGraphQL (GraphQL): Requires the WPGraphQL plugin. Solves REST's efficiency issues by allowing developers to fetch posts, authors, and media in a single query with a customized shape.
  • Verdict: For complex Headless WordPress sites (using React, Next.js, or Gatsby), GraphQL is highly recommended to improve performance and developer experience. For simple integrations, the built-in REST API is sufficient.

When it comes to building headless WordPress applications, developers have two main options for interacting with the WordPress backend: the native REST API or GraphQL. Both options have their own advantages and disadvantages, and the choice between them will depend on the specific needs of your project.


The REST API Approach

REST API (Representational State Transfer) is a widely used standard for building web services. In WordPress, it is supported out-of-the-box under the /wp-json/wp/v2/ namespace.

With the REST API, developers retrieve data by making HTTP requests to specific endpoints. For example, to fetch a list of posts, you send a GET request:

GET /wp-json/wp/v2/posts?_embed

While simple and familiar, the REST API suffers from two major limitations:

  1. Over-fetching: The server returns a massive JSON payload containing hundreds of fields (such as comment statuses, pingbacks, and formats) even if you only need the post title and content.
  2. Under-fetching: To get nested relational data like the author's profile picture or featured images, you often have to make additional HTTP requests, causing latency.

The GraphQL Approach

GraphQL is a modern query language developed by Facebook. Unlike the REST API, it is not built into WordPress out-of-the-box. To use it, you must install the WPGraphQL plugin, which exposes a single /graphql endpoint.

With GraphQL, developers can write declarations asking for exactly what they need. For instance, to get posts with their titles, excerpts, and featured images, you write a single query:

query GetPosts {
  posts {
    nodes {
      title
      excerpt
      featuredImage {
        node {
          sourceUrl
        }
      }
    }
  }
}

This single query fetches the posts and their associated media relationships in one round-trip, significantly reducing network payload and loading times.


REST API vs. GraphQL in Headless WordPress

FeatureWordPress REST APIWPGraphQL (GraphQL)
Out-of-the-Box SupportYes (available at /wp-json/wp/v2/).No (requires installing the WPGraphQL plugin).
Data Fetching EfficiencyOver-fetches data (returns many unused fields by default).Under-fetching/Over-fetching solved (only returns requested fields).
Nested Data RelationshipsProne to N+1 requests (needs custom fields or _embed queries).Fetches posts, authors, categories, and media in one query.
Schema & ToolingJSON schema (less interactive).Strongly typed schema with GraphiQL IDE integrated in WP admin.
Ecosystem FitStandard HTTP libraries.Fits perfectly with Apollo Client, Urql, or modern static site generators.

Frequently Asked Questions

What is the WordPress REST API?

The WordPress REST API is an interface that allows developers to interact with the WordPress core databases remotely via HTTP requests. It is fully integrated into the WordPress core, making it available without any plugins.

What is WPGraphQL?

WPGraphQL is a free, open-source WordPress plugin that provides a customizable GraphQL schema and API endpoint for any WordPress site. It is the de facto standard for building GraphQL-based headless WordPress sites.

Why is GraphQL preferred for headless WordPress?

GraphQL is preferred because it solves the performance bottlenecks of the REST API. It allows static site generators (like Gatsby or Next.js) and single-page apps to query custom fields, post relationships, and media elements in a single, lightweight HTTP request.

What are the main disadvantages of the REST API?

The main disadvantages are performance overhead due to over-fetching and the necessity of managing multiple endpoints. Handling deeply nested custom fields (such as ACF) in REST API often requires writing custom PHP endpoint filters or executing multiple sequential HTTP calls.

Can I use both REST API and GraphQL together?

Yes. Since WPGraphQL is plugin-based, it can run alongside the native REST API. You can use GraphQL for rendering your frontend pages and REST API for webhook handlers, form submissions, or third-party CRM integrations.