1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?php
namespace WPGraphQL\Type\TermObject;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Types;
class TermObjectQuery {
private static $root_query;
public static function root_query( $taxonomy_object ) {
if ( null === self::$root_query ) {
self::$root_query = [];
}
if ( ! empty( $taxonomy_object->name ) && empty( self::$root_query[ $taxonomy_object->name ] ) ) :
self::$root_query[ $taxonomy_object->name ] = [
'type' => Types::term_object( $taxonomy_object->name ),
'description' => sprintf( __( 'A % object', 'wp-graphql' ), $taxonomy_object->graphql_single_name ),
'args' => [
'id' => Types::non_null( Types::id() ),
],
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $taxonomy_object ) {
$id_components = Relay::fromGlobalId( $args['id'] );
return DataSource::resolve_term_object( $id_components['id'], $taxonomy_object->name );
},
];
return self::$root_query[ $taxonomy_object->name ];
endif;
}
}