GraphQL is a very powerful query language that minimizes calls from a client to a server. Since WordPress is one of the most used publications tools, it can be interesting to use it with GraphQL.

Provide a GraphQL endpoint in WordPress

The principle of GraphQL is to provide an endpoint on which we will make a call to retrieve data. For WordPress to provide this, I installed wp-graphql.

To install it, download the latest version of the plugin from https://github.com/wp-graphql/wp-graphql/releases and upload the file into the WordPress dashboard.

After activating the plugin, make sure that it works by opening the URL https: // ADDRESS_DU_SITE_WORDPRESS/graphql. An error message similar to the following screenshot should then appear.

Retrieve some data

We will now try to make requests to retrieve data from the WordPress site.

For that, I use Insomnia which offers very nice features to work with GraphQL, just to mention autocompletion or  automatic indentation.

I create a new query in Insomnia and I the GraphQL endpoint as URL. I then select GraphQL as data type sent by Insomnia and I start editing the contents of the query.

Here is the example of a query that retrieves the last 10 articles with their URL and their featured images:

{
  posts (first: 10){
    nodes{
      title
      featuredImage{
        sourceURL
      }
    link
    }
  }
}

Voila, we can now use GraphQL to retrieve data from a WordPress site.