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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<?php
namespace WPGraphQL\Type\PostObject\Connection;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Types;
class PostObjectConnectionDefinition {
private static $connection;
protected static $added_args;
public static function connection( $post_type_object ) {
if ( null === self::$connection ) {
self::$connection = [];
}
if ( empty( self::$connection[ $post_type_object->name ] ) ) :
$connection = Relay::connectionDefinitions( [
'nodeType' => Types::post_object( $post_type_object->name ),
'name' => ucfirst( $post_type_object->graphql_plural_name ),
'connectionFields' => function() use ( $post_type_object ) {
return [
'postTypeInfo' => [
'type' => Types::post_type(),
'description' => __( 'Information about the type of content being queried', 'wp-graphql' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $post_type_object ) {
return $post_type_object;
},
],
'nodes' => [
'type' => Types::list_of( Types::post_object( $post_type_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'] : [];
},
],
];
},
] );
self::$connection[ $post_type_object->name ] = [
'type' => $connection['connectionType'],
'description' => sprintf( __( 'A collection of %s objects', 'wp-graphql' ), $post_type_object->graphql_plural_name ),
'args' => array_merge( Relay::connectionArgs(), self::added_args() ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $post_type_object ) {
return DataSource::resolve_post_objects_connection( $source, $args, $context, $info, $post_type_object->name );
},
];
endif;
return self::$connection[ $post_type_object->name ];
}
protected static function added_args() {
if ( null === self::$added_args ) {
self::$added_args = [
'where' => [
'name' => 'where',
'description' => __( '', 'wp-graphql' ),
'type' => Types::post_object_query_args(),
],
];
}
return self::$added_args;
}
}