Using GraphQL and WordPress with the WPGraphQL Plugin

Using GraphQL and WordPress with the WPGraphQL Plugin

Adam Morgan

What is GraphQL?

For those of you unfamiliar with GraphQL, it is actually a specific language. GraphQL’s query language is super easy to pick up. For most people, this language will click immediately, and you don’t have to worry about understanding a lengthy explanation. Thanks to the type system GraphQl offers, learning GraphQl really isn’t all that difficult. Also, the many advantages it offers over traditional REST API’s in WordPress will be obvious immediately. With GraphQL, one of the easiest ways to think of this language is a structured way of making REST calls. The queries will be specific to an application, not the WordPress database that holds all of your data.

With GraphQL, it will only expose the capabilities that are required by your application to run and controls the access to these capabilities tightly. In fact, GraphQL serves are often far easier to secure than traditional RESTful APIs. Also, you will find it super easy to build your own GraphQL server. To create your own server, you’ll need to define a schema and resolve any functions.

GraphQL Schema and Resolve Functions

It’s important to understand that your GraphQL server will have two main components: resolve functions and a schema. The schema is a model of any data that you can fetch through your GraphQL server. This will define what queries clients can make and what type of data can be fetched from your server. It also determines the relationship between these data types. There is one critical piece of information that the schema does not contain and that is where the data for your GraphQL server comes from.

This is where resolve functions come into play. Resolve functions are like little mini routers. They will specify how the fields and types in your scheme connect. One of the greatest features of the GraphQL server is that it will hide any of the backend complexity of your WordPress website from clients. Regardless of how many different backend your WordPress site uses, all your clients will see is a single endpoint.

Using WPGraphQL Plugin for Your WordPress Site

With the WPGraphQL plugin, you’ll easily be able to install this on your WordPress site like any other WordPress plugin. This specific plugin brings the impressive power of GraphQL to WordPress. Where REST will require multiple HTTP requests for related data, using GraphQL means that any related data will automatically be queried in a single request.

Before you can merge anything into your WPGraphQL code base it will have to pass a number of tests to ensure it has 100% code coverage. Also, to set up your own tests, you’ll need MySQL setup locally. Your test suite will also require two different databases for testing.

With the WPGraphQL plugin, some of the main concepts and code are based on WordPress Rest API, so users who are not familiar with GraphQL should still be able to understand the basic concepts of GraphQL language with ease.

Already using a WPGraphQL plugin? Let us know your thoughts on this plugin option in the comments section below.