thefold/graphql-wp

此包已被废弃,不再维护。作者建议使用mohiohio/graphql-wp包代替。

WordPress的GraphQL端点

安装: 219

依赖项: 0

建议者: 0

安全性: 0

星星: 304

关注者: 16

分支: 28

开放问题: 3

类型:wordpress-muplugin


README

一个易于定制的WordPress GraphQL端点。

这是一个WordPress插件,在/graphql处公开GraphQL端点。

使用这个出色的graphql-php库。

支持Relay连接。

安装

composer require mohiohio/graphql-wp

如果你不熟悉使用Composer与WordPress,我建议使用bedrock之类的设置。否则,至少需要require autoload.php以便正常工作。

使用

安装后,最好的方法是访问/graphiql以探索/开发。这将显示可用的端点和参数。请注意,这仅在您是登录管理员用户时才有效。

https://github.com/tim-field/graphql-wp/raw/master/.readme.md/graphiql.png

wp_query

此设计旨在遵循WordPress现有的WP Query函数。因此,作为规则,您可以传递与WP Query*相同的参数。

*实际上,您可以向WP_Query传递很多参数,而我只实现了到目前为止需要的那些。但添加更多是微不足道的,因为参数直接传递到get_posts函数,所以只需在模式中定义它们即可。

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

将提供

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

文章

当然,您还可以获取单个文章

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

自定义字段

任何元字段都可用,如下所示

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

如果您想定义自己的解析器/类型,可以像这样扩展文章类型的字段模式。

// 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;
});

自定义文章类型

您可以这样向客户端特定插件添加自定义文章类型(包括自定义字段)。

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()
    ]);
});

在野外

http://www.page1management.com/

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