thefold / graphql-wp
0.8.5
2020-06-29 09:44 UTC
Requires
- php: >=7
- danielstjules/stringy: 2.4.0
- ivome/graphql-relay-php: 0.5.0
- mohiohio/wordpress-lib: 0.1.5
- ramsey/uuid: 4.0.1
- rbdwllr/reallysimplejwt: 3.0.2
- webonyx/graphql-php: 0.13.8
Requires (Dev)
- analog/analog: ^1.0
- php-stubs/wordpress-stubs: 5.4.0
- dev-master
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.2
- 0.3.1
- 0.3.0.x-dev
- 0.3.0
- 0.2.x-dev
- 0.2.0
- 0.1.3
- 0.1.2
- 0.1.2-b.1
- 0.1.2b
- 0.1.2a
- 0.1.0
- 0.0.1
- dev-renovate/webonyx-graphql-php-14.x
- dev-renovate/php-stubs-wordpress-stubs-5.x
- dev-renovate/rbdwllr-reallysimplejwt-4.x
- dev-renovate/rbdwllr-reallysimplejwt-3.x
- dev-renovate/ivome-graphql-relay-php-0.x
- dev-renovate/pin-dependencies
- dev-renovate/ramsey-uuid-4.x
- dev-renovate/webonyx-graphql-php-0.x
- dev-renovate/danielstjules-stringy-3.x
- dev-development
- dev-mohiohio
- dev-relay
This package is auto-updated.
Last update: 2021-05-17 02:15:47 UTC
README
一个易于定制的WordPress GraphQL端点。
这是一个WordPress插件,在/graphql处公开GraphQL端点。
使用这个出色的graphql-php库。
支持Relay连接。
安装
composer require mohiohio/graphql-wp
如果你不熟悉使用Composer与WordPress,我建议使用bedrock之类的设置。否则,至少需要require autoload.php以便正常工作。
使用
安装后,最好的方法是访问/graphiql
以探索/开发。这将显示可用的端点和参数。请注意,这仅在您是登录管理员用户时才有效。
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() ]); });