All Projects → tim-field → Graphql Wp

tim-field / Graphql Wp

Licence: gpl-3.0
GraphQL endpoint for WordPress

Projects that are alternatives of or similar to Graphql Wp

Pop
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder
Stars: ✭ 160 (-47.19%)
Mutual labels:  graphql, wordpress, headless-cms
Nextjs Headless Wordpress
🔥 Nextjs Headless WordPress
Stars: ✭ 110 (-63.7%)
Mutual labels:  graphql, wordpress, headless-cms
Gatsby Wpgraphql Blog Example
Demo showing how to use WPGraphQL as the source for Gatsby Sites
Stars: ✭ 152 (-49.83%)
Mutual labels:  graphql, wordpress
Payload
Headless CMS and Application Framework built with Node.js, React and MongoDB
Stars: ✭ 154 (-49.17%)
Mutual labels:  graphql, headless-cms
Jss
Official repo of Sitecore JavaScript Services
Stars: ✭ 173 (-42.9%)
Mutual labels:  graphql, headless-cms
Wp Graphql Yoast Seo
This is an extension to the WPGraphQL plugin for Yoast SEO
Stars: ✭ 120 (-60.4%)
Mutual labels:  graphql, wordpress
Directus
Open-Source Data Platform 🐰 — Directus wraps any SQL database with a real-time GraphQL+REST API and an intuitive app for non-technical users.
Stars: ✭ 13,190 (+4253.14%)
Mutual labels:  graphql, headless-cms
Wp Graphql Gutenberg
Query gutenberg blocks with wp-graphql
Stars: ✭ 158 (-47.85%)
Mutual labels:  graphql, wordpress
Docker
Directus Docker — The Official Docker Container for the Directus Suite
Stars: ✭ 93 (-69.31%)
Mutual labels:  graphql, headless-cms
Unite Cms
Really flexible headless CMS, built on top of Symfony and GraphQL.
Stars: ✭ 242 (-20.13%)
Mutual labels:  graphql, headless-cms
Wp Decoupled
Next.js app with WPGraphQL and WordPress at the backend.
Stars: ✭ 197 (-34.98%)
Mutual labels:  graphql, wordpress
Strapi Sdk Javascript
🔌 Official JavaScript SDK for APIs built with Strapi.
Stars: ✭ 247 (-18.48%)
Mutual labels:  graphql, headless-cms
Shio
✨ :dna: Shio CMS - Model Content, Use GraphQL and Create Site using Javascript with Native Cache and Search.
Stars: ✭ 119 (-60.73%)
Mutual labels:  graphql, headless-cms
Graphql Api For Wp
[READ ONLY] GraphQL API for WordPress
Stars: ✭ 136 (-55.12%)
Mutual labels:  graphql, wordpress
Strapi
🚀 Open source Node.js Headless CMS to easily build customisable APIs
Stars: ✭ 41,786 (+13690.76%)
Mutual labels:  graphql, headless-cms
Wp Graphql
🚀 GraphQL API for WordPress
Stars: ✭ 3,097 (+922.11%)
Mutual labels:  graphql, wordpress
Daptin
Daptin - Backend As A Service - GraphQL/JSON-API Headless CMS
Stars: ✭ 1,195 (+294.39%)
Mutual labels:  graphql, headless-cms
Wooshop
A Wocommerce Based React Native App for IOS and Android over GraphQL
Stars: ✭ 92 (-69.64%)
Mutual labels:  graphql, wordpress
Tipe
🎉 Next Generation API-first CMS for developers. Generate an API-first CMS from a GraphQL schema with offline prototyping and an inline editor
Stars: ✭ 2,157 (+611.88%)
Mutual labels:  graphql, headless-cms
Wordexpress Starter Vue
WordPress with Vue, GraphQL, and Node
Stars: ✭ 252 (-16.83%)
Mutual labels:  graphql, wordpress

graphql-wp

A GraphQL endpoint for WordPress that's easy to customize.

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

Uses this excellent graphql-php library.

Supports Relay Connections.

Install

composer require mohiohio/graphql-wp

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.

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.

https://github.com/tim-field/graphql-wp/raw/master/.readme.md/graphiql.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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].