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
<?php
namespace WPGraphQL\Type\Union;
use GraphQL\Type\Definition\UnionType;
use WPGraphQL\Types;
/**
* Class CommentAuthorUnionType
*
* In some situations, the type of term cannot be known until query time. The commentAuthorUnion allows for a
* comment author to be queried and resolved to a number of types. Currently will return a user or commentAuthor.
*
* @package WPGraphQL\Type\Union
*/
class CommentAuthorUnionType extends UnionType {
/**
* This holds an array of the possible types that can be resolved by this union
*
* @var array
*/
private static $possible_types;
/**
* CommentAuthorUnionType constructor.
*/
public function __construct() {
$config = [
'name' => 'CommentAuthorUnion',
'types' => function() {
return self::getPossibleTypes();
},
'resolveType' => function( $source ) {
if ( $source instanceof \WP_User ) {
$type = Types::user();
} else {
$type = Types::comment_author();
}
return $type;
},
];
parent::__construct( $config );
}
/**
* This defines the possible types that can be resolved by this union
*
* @return array|null An array of possible types that can be resolved by the union
*/
public function getPossibleTypes() {
if ( null === self::$possible_types ) {
self::$possible_types = [];
}
self::$possible_types = [
'user' => Types::user(),
'commentAuthor' => Types::comment_author(),
];
/**
* Filter the possible_types as it's possible some systems might set things like "parent_id" to a different
* object than a post_type, and might want to be able to hook in and add a non postObject type to the possible
* resolveTypes.
*
* @param array $possible_types An array of possible types that can be resolved for the union
*
* @since 0.0.6
*/
self::$possible_types = apply_filters( 'graphql_comment_author_union_possible_types', self::$possible_types );
return ! empty( self::$possible_types ) ? self::$possible_types : null;
}
}