Created
December 4, 2018 13:23
-
-
Save jkhaui/3d15f01e6c0924e1c51102e1e13afbc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
php | |
class SubscriptionType extends \WPGraphQL\Type\WPObjectType { | |
private static $fields; | |
public function __construct() { | |
$config = [ | |
'name' => 'SubscriptionType', | |
'fields' => self::fields(), | |
'description' => __( 'A list of users\' transaction history and whether or not they have an active subscription.', 'meprSub' ), | |
]; | |
parent::__construct( $config ); | |
} | |
protected static function fields() { | |
if ( null === self::$fields ) { | |
self::$fields = function() { | |
$fields = [ | |
'membershipStatus' => [ | |
'type' => 'string', | |
'description' => __( 'Shows whether or not the user has an active member subscription.', 'meprSub' ), | |
// Help with resolving status field from MemberPress field in MEPR custom table | |
'resolve' => function( \WP_User $user, $args, $context, $info ) { | |
return ! empty( $user->ID, $status ) ? esc_html( $user->$status ) : ''; | |
} | |
], | |
'totalSpent' => [ | |
'type' => 'float', | |
'resolve' => function( $book, $args, $context, $info ) { | |
// Help with resolving status field from MemberPress field in MEPR custom table | |
'description' => __( 'The total amount of money the user has spent.', 'meprSub' ), | |
return ! empty( $user->pages ) ? absint( $user->pages ) : 0; | |
} | |
], | |
]; | |
// Pass the field through the filters, etc provided by WPObjectType | |
return self::prepare_fields( $fields, 'SubscriptionType' ); | |
}; | |
} | |
return ! empty( self::$fields ) ? self::$fields : null; | |
} | |
} | |
// custom class | |
class MeprGraphQLClass { | |
public $foo; | |
public $bar; | |
public function __construct($a,$b) { | |
$this->foo = $a; | |
$this->bar = $b; | |
} | |
} | |
add_action( 'graphql_root_query', 'wp_graphql_custom_table_root_query' ); | |
function wp_graphql_custom_table_root_query( $fields ) { | |
$fields['userActions'] = [ | |
'type' => \WPGraphQL\Types::string(), | |
'description' => __( 'Returns a random Dad joke', 'wp-graphql' ), | |
'resolve' => function() { | |
return []; | |
}, | |
]; | |
return $fields; | |
} | |
register_graphql_object_type( | |
'Subscriptions', | |
[ | |
'description' => __( 'A list of users\' transaction history and whether or not they have an active subscription.' ), | |
'isPrivate' => false, | |
'fields' => [ | |
'membershipStatus' => [ | |
'type' => 'string', | |
'description' => __( 'Shows whether or not the user has an active member subscription.' ), | |
'resolve' => function( $book, $args, $context, $info ) { | |
return ! empty( $book->language ) ? esc_html( $book->language ) : ''; | |
} | |
], | |
'totalSpent' => [ | |
'type' => 'float', | |
'description' => __( 'The total amount of money the user has spent.' ), | |
'resolve' => function( $book, $args, $context, $info ) { | |
return ! empty( $book->pages ) ? absint( $book->pages ) : 0; | |
} | |
], | |
], | |
] | |
); | |
add_action( 'graphql_init', function() { | |
require_once( 'path/to/your/file.php' ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment