APIs for WordPress

Gato GraphQL vs WP REST API

Comparison between Gato GraphQL and the WP REST API

Gato GraphQL vs WP REST API
plus image
Gato GraphQL vs WP REST API

The differences between REST and GraphQL APIs are generally valid when contrasting the WP REST API against Gato GraphQL.

With GraphQL you can execute a tailored GraphQL query against an endpoint, indicating what specific data you need, and fetching only that data within a single request.

For instance, the following GraphQL query will fetch the required data for a specific post, including data from its relationships (author, categories and tags), all within a single request:

query {
  post(by: { id: 1 }) {
    title
    content
    url
    date
    author {
      id
      name
    }
    categories {
      id
      name
    }
    tags {
      id
      name
    }
  }
}

To fetch the same data with REST, you may need to first execute a request to retrieve the post data, and a subsequent request for each of its relationships (author, categories and tags) to fetch their data.

These architectural differences between REST and GraphQL have been widely covered elsewhere, so from now on, we won't repeat them here.

Below, let's do a more specific comparison between Gato GraphQL + all extensions, and the WP REST API.

Summary table

FeatureGato GraphQLWP REST API
Data FetchingExecutes tailored GraphQL queries against an endpoint, fetching specific data within a single request.Requires multiple requests to different endpoints to fetch related data (e.g., post data, author data, categories, and tags).
EndpointsSupports Persisted Queries, which are endpoints with pre-defined data created using the GraphQL language via a user interface within the wp-admin, without needing to deploy code.Exposes data via REST endpoints, each with its own URL and pre-defined data, created via PHP code and deployed within a theme or plugin.
Access ControlProvides flexibility with field-level access control based on rules (e.g., user roles, capabilities, IP range).Restricts data based on the context parameter (e.g., view for unauthenticated users, edit for authenticated users with permissions).
Bulk OperationsSupports Multiple Query Execution, where a single GraphQL document can execute multiple operations that can share state via the @export directive, improving upon batch requests.Allows batch requests, where multiple requests are satisfied internally within a single HTTP request.
Site ManagementCan fetch data, modify it, and store it back within a single GraphQL document via user interfaces, enabling automation and management of WordPress sites. Can perform tasks of multiple plugins like duplicating, automating, backing up, search/replace, webhooks, and translating. Is an API and more.Primarily an API for accessing and manipulating WordPress data.
GeneralityA generic tool to manage WordPress sites, capable of data mutation and integration with third-party services via GraphQL queries.Focused on providing an API; requires additional plugins or custom code to achieve similar functionalities.

Summary of key points:

  • Data Fetching: Gato GraphQL uses tailored queries, while WP REST API uses multiple endpoint requests.
  • Endpoints: Gato GraphQL uses persisted queries created via UI, while WP REST API uses PHP-coded REST endpoints.
  • Access Control: Gato GraphQL offers field-level control, while WP REST API uses the context parameter.
  • Bulk Operations: Gato GraphQL uses Multiple Query Execution, and WP REST API uses batch requests.
  • Site Management: Gato GraphQL is a comprehensive tool, and WP REST API is primarily an API.

Accessing predefined data

With the WP REST API, you expose data via REST endpoints. Each endpoint has its own URL, and its data is pre-defined (for the corresponding resources, such as posts or users).

Similar to REST endpoints, Gato GraphQL supports Persisted Queries, which are also endpoints with pre-defined data. Requesting a persisted query via GET will execute the stored GraphQL query, and produce the expected JSON response:

Executing a persisted in the browser
Executing a persisted in the browser

The difference between them is that while REST API endpoints are created via PHP code, and must be deployed within a theme or plugin, Gato GraphQL persisted queries are created using the GraphQL language, and are published via a user interface (powered by the WordPress editor) within the wp-admin, without the need to deploy any code.

Persisted query editor
Persisted query editor

The same caching mechanisms can be applied to REST endpoints and GraphQL persisted queries. Since the persisted query is accessed under its own endpoint, its response can be cached using standard HTTP caching (PRO).

Access control

Restricting data in the WP REST API depends on the context parameter. Passing ?context=view produces data for unauthenticated users, and ?context=edit includes additional data for authenticated users (with the right permissions), such as the post's content.raw field.

Gato GraphQL provides much more flexibility, with every single field being either accessible or not based on Access Control rules. You can validate that only logged-in users, or users with a certain role or capability, or visitors from a certain IP range, can access a specific field (PRO).

Access Control List editor
Access Control List editor

Bulk operations

The WP REST API allows to execute batch requests, where multiple requests are satisfied internally within a single HTTP request.

Gato GraphQL provides Multiple Query Execution, where a single GraphQL document can execute multiple operations.

Multiple Query Execution is an improvement over batch requests, as the operations can share state with one another via the @export directive.

For instance, to duplicate a post, we have a query operation fetch the post data, and pass this data to a mutation operation that creates a new post with it:

query GetPostAndExportData($postId: ID!)
{
  post(by: { id: $postId }, status: any) {
    author {
      id @export(as: "authorID")
    }
    categories {
      id @export(as: "categoryIDs", type: LIST)
    }
    rawContent @export(as: "rawContent")
    rawExcerpt @export(as: "excerpt")
    featuredImage {
      id @export(as: "featuredImageID")
    }
    tags {
      id @export(as: "tagIDs", type: LIST)
    }
    rawTitle @export(as: "title")
  }
}
 
mutation DuplicatePost
  @depends(on: "GetPostAndExportData")
{
  createPost(input: {
    status: draft,
    authorBy: {
      id: $authorID
    },
    categoriesBy: {
      ids: $categoryIDs
    },
    contentAs: {
      html: $rawContent
    },
    excerpt: $excerpt
    featuredImageBy: {
      id: $featuredImageID
    },
    tagsBy: {
      ids: $tagIDs
    },
    title: $title
  }) {
    postID
  }
}

Managing the WordPress site

Gato GraphQL allows us to fetch data from the database, modify it as required, and store it back, all of it within a single GraphQL document.

This is achieved via user interfaces, to compose and publish the GraphQL queries, configure the endpoints as needed, and automate the execution of a query when some event happens.

This all means that Gato GraphQL is a generic tool to manage our WordPress sites, satisfying those use cases where data (whether from the WordPress site, or provided by 3rd party services) must be mutated, as this can be accomplished by executing some GraphQL query.

Please notice how Gato GraphQL can deliver the functionality of multiple plugins:

The WP REST API is just that, an API.

Gato GraphQL is also an API, but also a lot more.

Subscribe to our newsletter

Stay in the loop on all new things concerning Gato GraphQL.