2017 © Pedro Peláez
 

wordpress-plugin graphql-wp

A GraphQL endpoint for WordPress

image

mohiohio/graphql-wp

A GraphQL endpoint for WordPress

  • Sunday, May 6, 2018
  • by timfield
  • Repository
  • 22 Watchers
  • 284 Stars
  • 1,696 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 26 Forks
  • 4 Open issues
  • 28 Versions
  • 4 % Grown

The README.md

graphql-wp

A GraphQL endpoint for WordPress that's easy to customize., (*1)

This is a WordPress Plugin that exposes a GraphQL endpoint at /graphql., (*2)

Uses this excellent graphql-php library., (*3)

Supports Relay Connections., (*4)

Install

composer require mohiohio/graphql-wp, (*5)

If your aren't familiar 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., (*6)

Using

The best way to explore / develop with this is by visiting /graphiql after installation. This will show you the endpoints and arguments that are available. Note this will only work if you are a logged in admin user., (*7)

https://github.com/tim-field/graphql-wp/raw/master/.readme.md/graphiql.png, (*8)

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*., (*9)

*_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._, (*10)

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

Will give you, (*11)

{
  "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, (*12)

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

Custom Fields

Any meta fields are available like so, (*13)

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., (*14)

// 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., (*15)

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/, (*16)

https://www.wokexpress.co.nz/menu, (*17)

The Versions

06/05 2018

dev-master

9999999-dev

A GraphQL endpoint for WordPress

  Sources   Download

GPL GPL-2.0-or-later

The Requires

 

The Development Requires

06/05 2018
30/11 2017
02/10 2017
21/09 2017
20/09 2017
28/08 2017
23/08 2017
02/02 2017
02/02 2017
30/01 2017
24/01 2017

0.4.0

0.4.0.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

24/01 2017

dev-development

dev-development

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

18/01 2017

0.3.2

0.3.2.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

18/01 2017

0.1.2-b.1

0.1.2.0-beta1

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

18/01 2017

0.1.2b

0.1.2.0-beta

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

18/01 2017

0.1.2a

0.1.2.0-alpha

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

20/09 2016

0.3.1

0.3.1.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

29/08 2016

0.3.0.x-dev

0.3.0.9999999-dev

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

29/08 2016

0.3.0

0.3.0.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

17/08 2016

0.2.x-dev

0.2.9999999.9999999-dev

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

17/08 2016

0.2.0

0.2.0.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

14/08 2016

0.1.3

0.1.3.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

19/06 2016

0.1.2

0.1.2.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

19/06 2016

dev-mohiohio

dev-mohiohio

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires

 

The Development Requires

19/06 2016
19/06 2016
17/03 2016

0.0.1

0.0.1.0

A GraphQL endpoint for WordPress

  Sources   Download

GPL

The Requires