3 ms·
The easy use case is to fetch data from a WordPress backend, and render your site using some JS library (React, Vue, Next.js). But it can do much more than tha
by leoloso 4y ago
The easy use case is to fetch data from a WordPress backend, and render your site using some JS library (React, Vue, Next.js).
But it can do much more than that (indeed, I like to think of GraphQL as the single tool to deal with any content-related task). These are some interesting use cases:
- Use a WordPress backend as an upstream server to manage all your data, and feed it to downstream servers/applications/websites
- Use GraphQL to extract information from 1 or more sources (such as users from the WordPress sites and the newsletter contact data from Mailchimp) and combine the data (and analyze it all together as a single dataset)
- Execute operations using GraphQL to adapt the content on your site, either as a one-off when installing a new site, or regularly. Eg: replace all "https://myoldsite.com https://myoldsite.com" to "https://mynewsite.com https://mynewsite.com" in the content after changing the domain or doing a migration, and execute queries to replace any "http:// http://" to "https:// https://" when a writer publishes a new blog post
- ETL (Extract, Transform, Load) of anything that involves content, whether stored in WordPress or external sources
- Interact with 3rd party services. Eg: Use GraphQL to connect to the Google Translate API and translate all the blog posts to a different language, or send a tweet after a blog post is published
- Send notifications (by email, Slack, etc) after something happened (a new post, comment, etc)
It can also be used with pipelines, and it can complement WP-CLI whenever it falls short of being able to do something. An example of this:
I myself use GraphQL via my plugin to connect to the GitHub API and fetch data from my releases, which can then be downloaded automatically and installed on my testing sites before running integration tests. I tried using WP-CLI directly for this and it fails, because the GraphQL API gives the .zip download file URL via a header, and this data needs to be extracted first. So I extract the header via GraphQL, and then inject it into WP-CLI, and the whole process is automated.
Many of these tasks, you can do them with dedicated plugins (such as for content migration). The beauty of GraphQL is that it's like a Swiss-army knife: you can write your own GraphQL query to execute many of these tasks, customizing it for your own needs, and avoiding to buy other plugins.