Skip to content

dobbit/graphql-wp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-wp

A GraphQL endpoint for WordPress

Exposes a graph ql endpoint at /graphql

This is a work in progress / in active development, but already pretty useful. Read only access currently.

Uses this excellent graphql-php library.

##Install composer require thefold/graphql-wp

##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.

###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":"{ 
	wp_query { 
		posts(paged: 1 posts_per_page: 10)  { 
			title 
			name 
			terms (taxonomy:\"category\") { 
				name 
				slug 
			}
		}
	}
}"}

Will give you

{
  "data": {
    "wp_query": {
      "posts": [
       {
          "title": "Much better than REST",
          "name": "so-easy-yes"
          "terms": [
          {
              "name": "Example Category ",
              "slug": "example-category"
          }
          ]
       } ...

Also available on wp_query menu

{"query":
    "{ wp_query 
	    { menu(name: \"Main Menu\")  { 
		    title 
		    url
		}
	}
}"}

Will give you

{
  "data": {
    "wp_query": {
      "menu": [
        {
          "title": "Home",
          "url": "http://graphqlwordpress.dev/"
        }
      ]
    }
  }
}

###Post

And of course you can get an individual post ( but most of the time you'll probably use wp_query as your main entry point )

{"query":"{wp_post(ID:\"1\") { title, content, status }}"}

###Custom Post Types

This is how I'm adding custom post types ( which have custom fields ) to my client specific plugin.
graphql-wp/get_post_types is a good hook for this ( there are other hooks, check the source, I'll endeavour to document them soon )

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.

add_filter('graphql-wp/get_post_types', function($types, $WPSchema) {

    $types[self::TYPE] = [
        'name' => 'Artist',
        'description' => 'A custom post type example',
        'fields' => [
            'website' => [
                'type' => Type::string(),
                'resolve' => function($post) {
                    return get_field('website',$post->ID);
                },
            ],
            'image' => [
                'type' => new ACFImage([
                    'name'=>'image'
                ]),
                'resolve' => function($post) {
                    return get_field('image',$post->ID);
                },
            ],
            'news' => [
                'type' => function() use ($WPSchema) {
                    return new ListOfType($WPSchema->getType('post'));
                },
                'resolve' => function($post) {
                    return get_posts([
                        'connected_type' => self::CONNECTION_NEWS,
                        'connected_items' => $post,
                        'nopaging' => true,
                        'suppress_filters' => false
                    ]) ?: [];
                }
            ],
            'downloads' => [
                'type' => new ListOfType( new ObjectType([
                    'name' => 'Downloads',
                    'fields' => [
                        'file' => [
                            'type' => new ACFFile(['name'=>'File']),
                            'resolve' => function($fieldset) {
                                return $fieldset['file'];
                            }
                        ]
                    ]
                ])),
                'resolve' => function($post) {
                    return get_field('downloads',$post->ID) ?: [];
                }
            ])),
                'resolve' => function($post) {
                    return static::getTourDates($post);
                }
            ]
        ]
    ];
    return $types;
}

},10, 2);

About

GraphQL endpoint for WordPress

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%