PHP
Clone or download
tim-field Update README.md
Updated examples to close #33
Latest commit 5f6fcb2 Aug 9, 2018
Permalink
Failed to load latest commit information.
.readme.md
Mutations
Type/Definition
.gitignore
LICENSE
README.md
Schema.php
WPType.php
composer.json
index.php

README.md

graphql-wp

A GraphQL endpoint for WordPress

This is a WordPress Plugin that exposes a GraphQL endpoint at /graphql

Uses this excellent graphql-php library.

Supports Relay Connections.

Should I use this ? 🤔

You are likely to come across extra functionality that you will want to add to this, as long as you are comfortable with PHP that should be easy to do. I've been working less and less with WordPress and PHP of late so if you want to jump in and take over maintaing this library that would be ideal. I think all the hard stuff is done 😄

Install

composer require mohiohio/graphql-wp

Assuming you have something like this in your composer.json file ( so it knows to install it in your plugin directory )

    "extra" : {
        "wordpress-install-dir": "public/wp",
        "installer-paths": {
            "public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
            "public/wp-content/themes/{$name}/": ["type:wordpress-theme"]
        }
    },

If your aren't familar with using composer with WordPress I'd recommend using a setup like bedrock. Otherwise you will at the least need to require autoload.php for this to work.

Using

The best way to explore / develop with this is by using a tool such as ChromeiQL That will show you the endpoints and arguments that are available.

https://raw.githubusercontent.com/balintsera/graphql-wp/fix/no-response/.readme.md/graphiql-query.png

wp_query

This is designed to follow WordPress' existing WP Query functions. So as a rule you can pass the same parameters as your can to WP Query*.

*In reality there are a lot of params you can pass to WP_Query, and I've only implemented the ones that I've needed so far. But adding more is trivial as the arguments are just passed directly to the get_posts function, so its just a matter of defining them in the schema.

query example {
 wp_query {
   posts(first: 10) {
     edges {
       node {
         title
         name
         terms(taxonomy: "category") {
           name
           slug
         }
       }
     }
   }
 }
}

Will give you

{
  "data": {
    "wp_query": {
      "posts": {
        "edges": [
          {
            "node": {
              "title": "Dashboard",
              "name": "hello-world",
              "terms": [
                {
                  "name": "Uncategorized",
                  "slug": "uncategorized"
                }
              ]
            }
          }
        ]
      }
    }
  }
}

Post

And of course you can get an individual post

query example {
  wp_post(ID: 9) {
    title
    content
    status
  }
}

Custom Fields

Any meta fields are available like so

query example {
  wp_post(ID: 9) {
    title
    foo: meta_value(key: "foo")
    bar: meta_value(key: "bar")
  }
}

If you want to define your own resolver / type you can extend the field schema for a post type like so.

// There is a get_{post_type}_schema call available for each post type
add_filter('graphql-wp/get_post_schema', function($schema) {

    $schema['fields'] = function() use ($schema) {
               // Note call to "parent" function here
        return $schema['fields']() + [
            'foo' => [
                'type' => Type::string(),
                'resolve' => function($post) {
                    return get_post_meta($post->ID, 'foo' ,true);
                }
            ],
            'bar' => [
                'type' => Type::string(),
                'resolve' => function($post) {
                    return get_post_meta($post->ID, 'bar' ,true);
                }
            ]
        ];
    };
    return $schema;
});

Custom Post Types

This is how you can add custom post types ( which have custom fields ) to a client specific plugin. graphql-wp/get_post_types is a good hook for this. Where $types is a hash of the schema we are working with, so just add new items into this and you are good to go.

use GraphQL\Type\Definition\Type;
use Mohiohio\GraphQLWP\Type\Definition\Post;
use Mohiohio\GraphQLWP\Type\Definition\Attachment;

class Foo extends Post {

    static function getDescription() {
        return "A custom post type example, for post type `foo`";
    }

    static function getFieldSchema() {
        return parent::getFieldSchema() + [
            'website' => [
                'type' => Type::string(),
                'resolve' => function($post) {
                    return get_post_meta($post->ID,'website',true);
                },
            ],
            'image' => [
                'type' => Attachment::getInstance(),
                'resolve' => function($post) {
                    $attachment_id = get_post_meta($post->ID,'image',true);
                    return $attachment_id ? get_post($attachment_id) : null;
                },
            ]
        ];
    }
}

add_filter('graphql-wp/schema-types', function($types){
    return array_merge($types, [
        Foo::getInstance()
    ]);
});

In the wild

http://www.page1management.com/

https://www.wokexpress.co.nz/menu