Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
240 lines (181 sloc) 7.88 KB
title description
Interacting with WPGraphQL
Learn how to interact with the WPGraphQL API

import GraphiQL from '../../src/components/GraphiQL' import Note from '../../src/components/Note'

On this page, you will find details on the various ways you can interact with your WPGraphQL API.

On this page you will see examples of GraphQL queries. Don't worry if it looks foreign right now. We'll go over what it all means in the{' '} Intro to GraphQL Guide

GraphQL IDEs and Tools

One of the most recognizable tools for interacting with GraphQL APIs is GraphiQL.

GraphiQL is a React component that acts as an IDE for interacting with GraphQL APIs. While it might be the most recognizable (and is maintained by Facebook), it's just one of many tools you can use to interact with your WPGraphQL API.

Below is an example of an _interactive_ embedded version of GraphiQL.

Some instances will even have a Documentation explorer, so you can explore all the possibilities that exist in the Schema.

  • Go ahead, click the play button to execute the query against a live WPGraphQL API!
  • Next, click the < Docs at the top right to explore the docs and search around to see what's available.

Below is a non-comprehensive list of helpful tools to interact with your WPGraphQL API:

  • WPGraphiQL – GraphQL IDE right in your WordPress dashboard
  • GraphQL Playground – GraphQL IDE that supports multi-column schema docs, tabs, query history, configuration of HTTP headers and GraphQL Subscriptions.
  • – A light, Electron-based wrapper around GraphiQL
  • GraphQL Network – A chrome dev-tools extension for debugging GraphQL network requests.
  • GraphQL IDE – An extensive IDE for exploring GraphQL API’s
  • Altair GraphQL Client – A beautiful feature-rich GraphQL Client for all platforms
  • Insomnia – An full-featured API client with first-party GraphQL query editor
Feel free to use any of the tools mentioned, explore and discover others that aren’t mentioned, or even build your own!

Just note that guides and documentation on this site will reference WPGraphiQL and and will also make use of inline instances of the GraphiQL React component to show examples.

HTTP POST Requests

WPGraphQL adds a /graphql endpoint to your WordPress install which can be queried by sending an HTTP Post request specifying at minimum a query as part of the request.

Components of a Request

When making an HTTP request to a WPGraphQL server, a few things need to be included in the request:

  • Content-Type header: The Content-Type should be set to application/json in the Headers of the request.
  • Method: The method of the request should be set to POST
  • Body: The body of the request should contain the following:
    • query (required): The string for the query (or mutation) to execute
    • variables (optional): JSON object of variables for use in the query
    • operationName (optional): The name of the operation to use for the response if multiple operations were sent in the query string

Browser Console

One of the easiest ways to test a WPGraphQL server is to make a fetch request from the browser console.

You can open up Chrome Dev Tools and paste the following into your console to fetch data from WPGraphQL. (Of course, change the URL to the API you want to fetch from.)

fetch('', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  body: JSON.stringify({
    query: `
            generalSettings {
  .then(res => res.json())
  .then(res => console.log(

GIF showing an example of fetching GraphQL data using fetch in the browser console

GIF Screenshot showing the request being made


While Curl is not necessarily the most convenient way to query a GraphQL API, (especially more complex queries that can get quite lengthy), it's certainly possible.

Here's an example of querying a GraphQL API using CURL. You can open up tour Terminal and paste in the following:

curl \\
  -X POST \\
  -H "Content-Type: application/json" \\
  --data '{ "query": "{generalSettings{ url }}" }' \\

WP Remote Post

Here's an example of making a request to a GraphQL API from WordPress theme or plugin code in PHP using the native WordPress wp_remote_post function.

$request = wp_remote_post( '', [
    'headers' => [
      'Content-Type' => 'application/json',
    'body' => wp_json_encode([
        'query' => '
              generalSettings {

If you inspected the results of the request like so:

// NOTE: the `true` returns the data as an associative
// array instead of an object
$decoded_response = json_decode( $request['body'], true );
print_r( $decoded_response['data'] );

You should see something like the following:

    [generalSettings] => Array
            [url] =>


Apollo Client

When building rich JavaScript applications that rely on data from a GraphQL API, Apollo Client is one of the best options for managing the fetching and caching of GraphQL data.

Apollo client supports many of the popular JavaScript and Native frameworks such as:

GraphQL in WordPress PHP

While HTTP POST requests are the most common way to interact with GraphQL, it's possible, and becoming more common, to interact with WPGraphQL directly from within your Theme / Plugin PHP code.

WPGraphQL exposes a graphql() function that allows you to execute a GraphQL query. You can use this to populate shortcodes, page templates, Gutenberg blocks, or anything else you could think of that needs data from your WordPress site to render or make decisions on in PHP.

You can use the graphql() method like so:

$graphql = graphql([
  'query' => ' {
    generalSettings {

If you inspected the results of the request like so:

print_r( $graphql['data'] );

You should see something like the following:

    [generalSettings] => Array
            [url] =>


HTTP GET Requests

It's typically recommended to use HTTP POST requests with WPGraphQL, but GET requests have limited support.

For example, just paste the following url in your browser to execute an HTTP GET request on a WPGraphQL server:{generalSettings{url}}
You can’t perform that action at this time.