Skip to content

Commit

Permalink
Merge pull request #2 from jasonbahl/feature/v0.0.15-compatibility
Browse files Browse the repository at this point in the history
Update to be compatabile with WPGraphQL v0.0.15
  • Loading branch information
jasonbahl authored Aug 3, 2017
2 parents cc61bee + f561884 commit 07e4f47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 47 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#WPGraphQL Meta Query
# WPGraphQL Meta Query
This plugin adds Meta_Query support to the WP GraphQL Plugin for postObject query args.

This plugin adds Meta_Query support to the WP GraphQL Plugin for postObject query args.

## Why is this an extension and not part of WPGraphQL?

Meta Queries _can_ be expensive and have been known to actually take sites down, which is why they are not
part of the core WPGraphQL plugin.

If you need meta queries for your WPGraphQL system, this plugin enables them, but use with caution. It might be better
to hook into WPGraphQL and define specific meta queries that you _know_ you need and are not going to take your system
down instead of allowing just any meta_query via this plugin, but you could use this plugin as an example of how
to hook into WPGraphQL to add inputs and map those inputs to the WP_Query that gets executed.

## Pre-req's
Using this plugin requires having the <a href="https://github.com/wp-graphql/wp-graphql" target="_blank">WPGraphQL plugin</a> installed
and activated.
and activated. Requires WPGraphQL version 0.0.15 or newer.

## Activating / Using
Activate the plugin like you would any other WordPress plugin.
Expand Down
88 changes: 48 additions & 40 deletions src/Type/MetaQueryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
namespace WPGraphQL\MetaQuery\Type;

use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\InputObjectType;
use WPGraphQL\Type\WPEnumType;
use WPGraphQL\Type\WPInputObjectType;
use WPGraphQL\Types;

/**
Expand All @@ -12,10 +13,11 @@
*
* @package WPGraphQL\Type
*/
class MetaQueryType extends InputObjectType {
class MetaQueryType extends WPInputObjectType {

private static $meta_compare_enum;
private static $meta_type;
protected static $fields;

/**
* MetaQueryType constructor.
Expand All @@ -27,44 +29,50 @@ class MetaQueryType extends InputObjectType {
public function __construct() {
$config = [
'name' => 'metaQuery',
'fields' => function() {
$fields = [
'relation' => [
'type' => Types::relation_enum(),
],
'metaArray' => Types::list_of(
new InputObjectType( [
'name' => 'metaArray',
'fields' => function() {
$fields = [
'key' => [
'type' => Types::string(),
'description' => __( 'Custom field key', 'wp-graphql' ),
],
'value' => [
'type' => Types::string(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
'compare' => [
'type' => self::meta_compare_enum(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
'type' => [
'type' => self::meta_type_enum(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
];
return $fields;
},
] )
),
];
return $fields;
},
'fields' => self::fields(),
];
parent::__construct( $config );
}

protected static function fields() {

if ( null === self::$fields ) :
self::$fields = [
'relation' => [
'type' => Types::relation_enum(),
],
'metaArray' => Types::list_of(
new WPInputObjectType( [
'name' => 'metaArray',
'fields' => function() {
$fields = [
'key' => [
'type' => Types::string(),
'description' => __( 'Custom field key', 'wp-graphql' ),
],
'value' => [
'type' => Types::string(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
'compare' => [
'type' => self::meta_compare_enum(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
'type' => [
'type' => self::meta_type_enum(),
'description' => __( 'Custom field value', 'wp-graphql' ),
],
];
return $fields;
},
] )
),
];
endif;
return ! empty( self::$fields ) ? self::$fields : null;

}

/**
* meta_compare_enum
*
Expand All @@ -75,7 +83,7 @@ public function __construct() {
*/
private static function meta_compare_enum() {
if ( null === self::$meta_compare_enum ) {
self::$meta_compare_enum = new EnumType( [
self::$meta_compare_enum = new WPEnumType( [
'name' => 'metaCompare',
'values' => [
[
Expand Down Expand Up @@ -136,7 +144,7 @@ private static function meta_compare_enum() {
],
],
] );
}
} // End if().
return ! empty( self::$meta_compare_enum ) ? self::$meta_compare_enum : null;
}

Expand All @@ -150,7 +158,7 @@ private static function meta_compare_enum() {
*/
private static function meta_type_enum() {
if ( null === self::$meta_type ) {
self::$meta_type = new EnumType( [
self::$meta_type = new WPEnumType( [
'name' => 'metaType',
'values' => [
[
Expand Down Expand Up @@ -191,7 +199,7 @@ private static function meta_type_enum() {
],
],
] );
}
} // End if().
return ! empty( self::$meta_type ) ? self::$meta_type : null;
}

Expand Down
8 changes: 4 additions & 4 deletions wp-graphql-meta-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/**
* Plugin Name: WP GraphQL Meta Query
* Plugin URI: https://github.com/wp-graphql/wp-graphql-meta-query
* Description: Meta_Query support for the WPGraphQL plugin
* Description: Meta_Query support for the WPGraphQL plugin. Requires WPGraphQL version 0.0.15 or newer.
* Author: Digital First Media, Jason Bahl
* Author URI: http://www.wpgraphql.com
* Version: 0.0.1
* Version: 0.0.2
* Text Domain: wp-graphql-meta-query
* Requires at least: 4.7.0
* Tested up to: 4.7.1
Expand Down Expand Up @@ -57,7 +57,7 @@ public function __construct() {
* Filter the query_args for the PostObjectQueryArgsType
* @since 0.0.1
*/
add_filter( 'graphql_wp_query_input_fields', [ $this, 'add_input_fields' ], 10, 1 );
add_filter( 'graphql_queryArgs_fields', [ $this, 'add_input_fields' ], 10, 1 );

/**
* Filter the $allowed_custom_args for the PostObjectsConnectionResolver to map the
Expand All @@ -79,7 +79,7 @@ private function setup_constants() {

// Plugin version.
if ( ! defined( 'WPGRAPHQL_METAQUERY_VERSION' ) ) {
define( 'WPGRAPHQL_METAQUERY_VERSION', '0.0.1' );
define( 'WPGRAPHQL_METAQUERY_VERSION', '0.0.2' );
}

// Plugin Folder Path.
Expand Down

0 comments on commit 07e4f47

Please sign in to comment.