Gato GraphQL vs WP REST API
Comparison between Gato GraphQL and the 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:
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.
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:
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.
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).
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:
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:
- It is not a duplicator plugin, however you can duplicate posts with it.
- It is not an automator plugin, but you can automate your tasks, without any restriction.
- It is not a backup plugin, but you can import and export posts.
- It is not a search and replace plugin, but you can modify your posts in bulk.
- It is not webhook plugin or HTTP client, but you can both send a request to any API, and receive and process incoming requests from any service.
- It is not a translation plugin, but you can translate any content, including the properties inside Gutenberg blocks.
The WP REST API is just that, an API.
Gato GraphQL is also an API, but also a lot more.