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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
<?php
namespace WPGraphQL\Type\TermObject\Connection;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Types;
class TermObjectConnectionDefinition {
private static $connection;
public static function connection( $taxonomy_object ) {
if ( null === self::$connection ) {
self::$connection = [];
}
if ( empty( self::$connection[ $taxonomy_object->name ] ) ) :
$connection = Relay::connectionDefinitions( [
'nodeType' => Types::term_object( $taxonomy_object->name ),
'name' => ucfirst( $taxonomy_object->graphql_plural_name ),
'connectionFields' => function() use ( $taxonomy_object ) {
return [
'taxonomyInfo' => [
'type' => Types::taxonomy(),
'description' => __( 'Information about the type of content being queried', 'wp-graphql' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $taxonomy_object ) {
return $taxonomy_object;
},
],
'nodes' => [
'type' => Types::list_of( Types::term_object( $taxonomy_object->name ) ),
'description' => __( 'The nodes of the connection, without the edges', 'wp-graphql' ),
'resolve' => function( $source, $args, $context, $info ) {
return ! empty( $source['nodes'] ) ? $source['nodes'] : [];
},
],
];
},
] );
$args = [
'where' => [
'name' => 'where',
'type' => Types::term_object_query_args(),
],
];
self::$connection[ $taxonomy_object->name ] = [
'type' => $connection['connectionType'],
'description' => sprintf( __( 'A collection of %s objects', 'wp-graphql' ), $taxonomy_object->graphql_plural_name ),
'args' => array_merge( Relay::connectionArgs(), $args ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $taxonomy_object ) {
return DataSource::resolve_term_objects_connection( $source, $args, $context, $info, $taxonomy_object->name );
},
];
endif;
return self::$connection[ $taxonomy_object->name ];
}
}