Overview
  • Namespace
  • Class

Namespaces

  • WPGraphQL
    • Data
    • Type
      • Avatar
      • Comment
        • Connection
      • CommentAuthor
      • EditLock
      • Enum
      • MediaItem
        • Mutation
      • Plugin
        • Connection
      • PostObject
        • Connection
        • Mutation
      • PostType
      • Setting
      • Taxonomy
      • TermObject
        • Connection
        • Mutation
      • Theme
        • Connection
      • Union
      • User
        • Connection
        • Mutation
    • Utils

Classes

  • WPGraphQL\AppContext
  • WPGraphQL\Data\Config
  • WPGraphQL\Data\ConnectionResolver
  • WPGraphQL\Data\DataSource
  • WPGraphQL\Data\Loader
  • WPGraphQL\Router
  • WPGraphQL\Type\Avatar\AvatarType
  • WPGraphQL\Type\Comment\CommentQuery
  • WPGraphQL\Type\Comment\CommentType
  • WPGraphQL\Type\Comment\Connection\CommentConnectionArgs
  • WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition
  • WPGraphQL\Type\Comment\Connection\CommentConnectionResolver
  • WPGraphQL\Type\CommentAuthor\CommentAuthorQuery
  • WPGraphQL\Type\CommentAuthor\CommentAuthorType
  • WPGraphQL\Type\EditLock\EditLockType
  • WPGraphQL\Type\Enum\MediaItemStatusEnumType
  • WPGraphQL\Type\Enum\MimeTypeEnumType
  • WPGraphQL\Type\Enum\PostObjectFieldFormatEnumType
  • WPGraphQL\Type\Enum\PostStatusEnumType
  • WPGraphQL\Type\Enum\PostTypeEnumType
  • WPGraphQL\Type\Enum\RelationEnumType
  • WPGraphQL\Type\Enum\TaxonomyEnumType
  • WPGraphQL\Type\MediaItem\MediaItemType
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemCreate
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemDelete
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemMutation
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemUpdate
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionDefinition
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionResolver
  • WPGraphQL\Type\Plugin\PluginQuery
  • WPGraphQL\Type\Plugin\PluginType
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgs
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgsDateQuery
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionResolver
  • WPGraphQL\Type\PostObject\Mutation\PostObjectCreate
  • WPGraphQL\Type\PostObject\Mutation\PostObjectDelete
  • WPGraphQL\Type\PostObject\Mutation\PostObjectMutation
  • WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate
  • WPGraphQL\Type\PostObject\Mutation\TermObjectDelete
  • WPGraphQL\Type\PostObject\PostObjectQuery
  • WPGraphQL\Type\PostObject\PostObjectType
  • WPGraphQL\Type\PostType\PostTypeType
  • WPGraphQL\Type\RootMutationType
  • WPGraphQL\Type\RootQueryType
  • WPGraphQL\Type\Setting\SettingQuery
  • WPGraphQL\Type\Setting\SettingType
  • WPGraphQL\Type\Taxonomy\TaxonomyType
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionArgs
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionResolver
  • WPGraphQL\Type\TermObject\Mutation\TermObjectCreate
  • WPGraphQL\Type\TermObject\Mutation\TermObjectMutation
  • WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate
  • WPGraphQL\Type\TermObject\TermObjectQuery
  • WPGraphQL\Type\TermObject\TermObjectType
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionDefinition
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionResolver
  • WPGraphQL\Type\Theme\ThemeType
  • WPGraphQL\Type\Union\CommentAuthorUnionType
  • WPGraphQL\Type\Union\PostObjectUnionType
  • WPGraphQL\Type\Union\TermObjectUnionType
  • WPGraphQL\Type\User\Connection\UserConnectionArgs
  • WPGraphQL\Type\User\Connection\UserConnectionDefinition
  • WPGraphQL\Type\User\Connection\UserConnectionResolver
  • WPGraphQL\Type\User\Mutation\UserCreate
  • WPGraphQL\Type\User\Mutation\UserDelete
  • WPGraphQL\Type\User\Mutation\UserMutation
  • WPGraphQL\Type\User\Mutation\UserUpdate
  • WPGraphQL\Type\User\UserQuery
  • WPGraphQL\Type\User\UserType
  • WPGraphQL\Type\WPEnumType
  • WPGraphQL\Type\WPInputObjectType
  • WPGraphQL\Type\WPObjectType
  • WPGraphQL\Types
  • WPGraphQL\Utils\InstrumentSchema
  • WPGraphQL\WPSchema

Interfaces

  • WPGraphQL\Data\ConnectionResolverInterface
  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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 
<?php

namespace WPGraphQL\Type\PostObject;

use GraphQL\Deferred;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;

use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Data\Loader;
use WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition;
use WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition;
use WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Types;

/**
 * Class PostObjectType
 *
 * This sets up the base PostObjectType. Custom Post Types that are set to "show_in_graphql" automatically
 * use the PostObjectType and inherit the fields that are defined here. The fields get passed through a
 * filter unique to each type, so each post_type can modify it's type schema via field filters.
 *
 * NOTE: In some cases the shape of a Custom Post Type's schema is so drastically different from the standard
 * PostObjectType shape it might make more sense for the custom post type to register a different type
 * altogether instead of utilizing the PostObjectType.
 *
 * @package WPGraphQL\Type
 * @since   0.0.5
 */
class PostObjectType extends WPObjectType {

    /**
     * Holds the $fields definition for the PostObjectType
     *
     * @var $fields
     */
    private static $fields;

    /**
     * Holds the post_type_object
     *
     * @var object $post_type_object
     */
    private static $post_type_object;

    /**
     * PostObjectType constructor.
     *
     * @param string $post_type The post_type name
     *
     * @since 0.0.5
     */
    public function __construct( $post_type ) {

        /**
         * Get the post_type_object from the post_type and store it
         * for later use
         *
         * @since 0.0.5
         */
        self::$post_type_object = get_post_type_object( $post_type );

        $config = [
            'name'        => ucfirst( self::$post_type_object->graphql_single_name ),
            // translators: the placeholder is the post_type of the object
            'description' => sprintf( __( 'The %s object type', 'wp-graphql' ), self::$post_type_object->graphql_single_name ),
            'fields'      => self::fields( self::$post_type_object ),
            'interfaces'  => [ self::node_interface() ],
        ];

        parent::__construct( $config );
    }

    /**
     * fields
     * This defines the fields for PostObjectTypes
     *
     * @param $post_type_object
     *
     * @return \GraphQL\Type\Definition\FieldDefinition|mixed|null
     * @since 0.0.5
     */
    private static function fields( $post_type_object ) {

        /**
         * Get the $single_name out of the post_type_object
         *
         * @since 0.0.5
         */
        $single_name = self::$post_type_object->graphql_single_name;

        /**
         * If no fields have been defined for this type already,
         * make sure the $fields var is an empty array
         *
         * @since 0.0.5
         */
        if ( null === self::$fields ) {
            self::$fields = [];
        }

        /**
         * If the $fields haven't already been defined for this type,
         * define the fields
         *
         * @since 0.0.5
         */
        if ( empty( self::$fields[ $single_name ] ) ) :
            /**
             * Get the taxonomies that are allowed in WPGraphQL
             *
             * @since 0.0.5
             */
            $allowed_taxonomies = \WPGraphQL::$allowed_taxonomies;

            /**
             * Define the fields for the post_type
             *
             * @return mixed
             * @since 0.0.5
             */
            self::$fields[ $single_name ] = function() use ( $single_name, $post_type_object, $allowed_taxonomies ) {
                $fields = [
                    'id'                => [
                        'type'        => Types::non_null( Types::id() ),
                        'description' => __( 'The globally unique ID for the object', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ( ! empty( $post->post_type ) && ! empty( $post->ID ) ) ? Relay::toGlobalId( $post->post_type, $post->ID ) : null;
                        },
                    ],
                    $single_name . 'Id' => [
                        'type'        => Types::non_null( Types::int() ),
                        'description' => __( 'The id field matches the WP_Post->ID field.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return absint( $post->ID );
                        },
                    ],
                    'ancestors'         => [
                        'type'        => Types::list_of( Types::post_object_union() ),
                        'description' => esc_html__( 'Ancestors of the object', 'wp-graphql' ),
                        'args'        => [
                            'types' => [
                                'type'        => Types::list_of( Types::post_type_enum() ),
                                'description' => __( 'The types of ancestors to check for. Defaults to the same type as the current object', 'wp-graphql' ),
                            ],
                        ],
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            $ancestors    = [];
                            $types        = ! empty( $args['types'] ) ? $args['types'] : [ $post->post_type ];
                            $ancestor_ids = get_ancestors( $post->ID, $post->post_type );
                            if ( ! empty( $ancestor_ids ) ) {
                                foreach ( $ancestor_ids as $ancestor_id ) {
                                    $ancestor_obj = get_post( $ancestor_id );
                                    if ( in_array( $ancestor_obj->post_type, $types, true ) ) {
                                        $ancestors[] = $ancestor_obj;
                                    }
                                }
                            }

                            return ! empty( $ancestors ) ? $ancestors : null;
                        },
                    ],
                    'author'            => [
                        'type'        => Types::user(),
                        'description' => __( "The author field will return a queryable User type matching the post's author.", 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return DataSource::resolve_user( $post->post_author );
                        },
                    ],
                    'date'              => [
                        'type'        => Types::string(),
                        'description' => __( 'Post publishing date.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_date ) ? $post->post_date : null;
                        },
                    ],
                    'dateGmt'           => [
                        'type'        => Types::string(),
                        'description' => __( 'The publishing date set in GMT.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_date_gmt ) ? $post->post_date_gmt : null;
                        },
                    ],
                    'content'           => [
                        'type'        => Types::string(),
                        'description' => __( 'The content of the post.', 'wp-graphql' ),
                        'args'        => [
                            'format' => self::post_object_format_arg(),
                        ],
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {

                            $content = ! empty( $post->post_content ) ? $post->post_content : null;

                            // If the raw format is requested, don't apply any filters.
                            if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
                                return $content;
                            }

                            return apply_filters( 'the_content', $content );
                        },
                    ],
                    'title'             => [
                        'type'        => Types::string(),
                        'description' => __( 'The title of the post. This is currently just the raw title. An amendment to support rendered title needs to be made.', 'wp-graphql' ),
                        'args'        => [
                            'format' => self::post_object_format_arg(),
                        ],
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {

                            $title = ! empty( $post->post_title ) ? $post->post_title : null;

                            // If the raw format is requested, don't apply any filters.
                            if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
                                return $title;
                            }

                            return apply_filters( 'the_title', $title );
                        },
                    ],
                    'excerpt'           => [
                        'type'        => Types::string(),
                        'description' => __( 'The excerpt of the post. This is currently just the raw excerpt. An amendment to support rendered excerpts needs to be made.', 'wp-graphql' ),
                        'args'        => [
                            'format' => self::post_object_format_arg(),
                        ],
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {

                            $excerpt = ! empty( $post->post_excerpt ) ? $post->post_excerpt : null;

                            // If the raw format is requested, don't apply any filters.
                            if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
                                return $excerpt;
                            }

                            $excerpt = apply_filters( 'get_the_excerpt', $excerpt, $post );
                            return apply_filters( 'the_excerpt', $excerpt );
                        },
                    ],
                    'status'            => [
                        'type'        => Types::string(),
                        'description' => __( 'The current status of the object', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_status ) ? $post->post_status : null;
                        },
                    ],
                    'commentStatus'     => array(
                        'type'        => Types::string(),
                        'description' => __( 'Whether the comments are open or closed for this particular post.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->comment_status ) ? $post->comment_status : null;
                        },
                    ),
                    'pingStatus'        => [
                        'type'        => Types::string(),
                        'description' => __( 'Whether the pings are open or closed for this particular post.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->ping_status ) ? $post->ping_status : null;
                        },
                    ],
                    'slug'              => [
                        'type'        => Types::string(),
                        'description' => __( 'The uri slug for the post. This is equivalent to the WP_Post->post_name field and the post_name column in the database for the `post_objects` table.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_name ) ? $post->post_name : null;
                        },
                    ],
                    'toPing'            => [
                        'type'        => Types::list_of( Types::string() ),
                        'description' => __( 'URLs queued to be pinged.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->to_ping ) ? implode( ',', $post->to_ping ) : null;
                        },
                    ],
                    'pinged'            => [
                        'type'        => Types::list_of( Types::string() ),
                        'description' => __( 'URLs that have been pinged.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->pinged ) ? implode( ',', $post->pinged ) : null;
                        },
                    ],
                    'modified'          => [
                        'type'        => Types::string(),
                        'description' => __( 'The local modified time for a post. If a post was recently updated the modified field will change to match the corresponding time.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_modified ) ? $post->post_modified : null;
                        },
                    ],
                    'modifiedGmt'       => [
                        'type'        => Types::string(),
                        'description' => __( 'The GMT modified time for a post. If a post was recently updated the modified field will change to match the corresponding time in GMT.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_modified_gmt ) ? $post->post_modified_gmt : null;
                        },
                    ],
                    'parent'            => [
                        'type'        => Types::post_object_union(),
                        'description' => __( 'The parent of the object. The parent object can be of various types', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, array $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->post_parent ) ? get_post( $post->post_parent ) : null;
                        },
                    ],
                    'editLast'          => [
                        'type'        => Types::user(),
                        'description' => __( 'The user that most recently edited the object', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, array $args, AppContext $context, ResolveInfo $info ) {
                            $edit_last = get_post_meta( $post->ID, '_edit_last', true );

                            return ! empty( $edit_last ) ? DataSource::resolve_user( absint( $edit_last ) ) : null;
                        },
                    ],
                    'editLock'          => [
                        'type'        => Types::edit_lock(),
                        'description' => __( 'If a user has edited the object within the past 15 seconds, this will return the user and the time they last edited. Null if the edit lock doesn\'t exist or is greater than 15 seconds', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, array $args, AppContext $context, ResolveInfo $info ) {
                            $edit_lock       = get_post_meta( $post->ID, '_edit_lock', true );
                            $edit_lock_parts = explode( ':', $edit_lock );

                            return ! empty( $edit_lock_parts ) ? $edit_lock_parts : null;
                        },
                    ],
                    'enclosure'         => [
                        'type'        => Types::string(),
                        'description' => __( 'The RSS enclosure for the object', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, array $args, AppContext $context, ResolveInfo $info ) {
                            $enclosure = get_post_meta( $post->ID, 'enclosure', true );

                            return ! empty( $enclosure ) ? $enclosure : null;
                        },
                    ],
                    'guid'              => [
                        'type'        => Types::string(),
                        'description' => __( 'The global unique identifier for this post. This currently matches the value stored in WP_Post->guid and the guid column in the `post_objects` database table.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->guid ) ? $post->guid : null;
                        },
                    ],
                    'menuOrder'         => [
                        'type'        => Types::int(),
                        'description' => __( 'A field used for ordering posts. This is typically used with nav menu items or for special ordering of hierarchical content types.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->menu_order ) ? absint( $post->menu_order ) : null;
                        },
                    ],
                    'desiredSlug'       => [
                        'type'        => Types::string(),
                        'description' => __( 'The desired slug of the post', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            $desired_slug = get_post_meta( $post->ID, '_wp_desired_post_slug', true );

                            return ! empty( $desired_slug ) ? $desired_slug : null;
                        },
                    ],
                    'link'              => [
                        'type'        => Types::string(),
                        'description' => __( 'The permalink of the post', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            $link = get_permalink( $post->ID );

                            return ! empty( $link ) ? $link : null;
                        },
                    ],
                    'uri'               => [
                        'type'        => Types::string(),
                        'description' => __( 'URI path for the resource', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            $uri = get_page_uri( $post->ID );

                            return ! empty( $uri ) ? $uri : null;
                        },
                    ],
                    'terms'             => [
                        'type'        => Types::list_of( Types::term_object_union() ),
                        'args'        => [
                            'taxonomy' => [
                                'type'         => Types::list_of( Types::taxonomy_enum() ),
                                'description'  => __( 'Select which taxonomies to limit the results to', 'wp-graphql' ),
                            ],
                        ],
                        // Translators: placeholder is the name of the post_type
                        'description' => sprintf( __( 'Terms connected to the %1$s', 'wp-graphql' ), $single_name ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) use ( $allowed_taxonomies ) {

                            $terms = [];
                            /**
                             * If the $arg for taxonomies is populated, use it as the $allowed_taxonomies
                             * otherwise use the default $allowed_taxonomies passed down
                             */
                            $allowed_taxonomies = ! empty( $args['taxonomy'] ) ? [ $args['taxonomy'] ] : $allowed_taxonomies;
                            if ( ! empty( $allowed_taxonomies ) && is_array( $allowed_taxonomies ) ) {
                                foreach ( $allowed_taxonomies as $taxonomy ) {
                                    $tax_terms = get_the_terms( $post->ID, $taxonomy );
                                    if ( ! empty( $tax_terms ) && is_array( $tax_terms ) ) {
                                        $terms = array_merge( $terms, $tax_terms );
                                    }
                                }
                            }

                            return ! empty( $terms ) ? $terms : null;
                        },
                    ],
                    'termNames'         => [
                        'type'        => Types::list_of( Types::string() ),
                        'args'        => [
                            'taxonomy' => [
                                'type'         => Types::taxonomy_enum(),
                                'description'  => __( 'Select which taxonomies to limit the results to', 'wp-graphql' ),
                            ],
                        ],
                        // Translators: placeholder is the name of the post_type
                        'description' => sprintf( __( 'Terms connected to the %1$s', 'wp-graphql' ), $single_name ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) use ( $allowed_taxonomies ) {

                            $terms = [];
                            /**
                             * If the $arg for taxonomies is populated, use it as the $allowed_taxonomies
                             * otherwise use the default $allowed_taxonomies passed down
                             */
                            $allowed_taxonomies = ! empty( $args['taxonomy'] ) ? [ $args['taxonomy'] ] : $allowed_taxonomies;
                            if ( ! empty( $allowed_taxonomies ) && is_array( $allowed_taxonomies ) ) {
                                foreach ( $allowed_taxonomies as $taxonomy ) {
                                    $tax_terms = get_the_terms( $post->ID, $taxonomy );
                                    if ( ! empty( $tax_terms ) && is_array( $tax_terms ) ) {
                                        $terms = array_merge( $terms, $tax_terms );
                                    }
                                }
                            }
                            $term_names = wp_list_pluck( $terms, 'name' );

                            return ! empty( $term_names ) ? $term_names : null;
                        },
                    ],
                ];

                /**
                 * Add comment fields to the schema if the post_type supports "comments"
                 *
                 * @since 0.0.5
                 */
                if ( post_type_supports( $post_type_object->name, 'comments' ) ) {
                    $fields['comments']     = CommentConnectionDefinition::connection();
                    $fields['commentCount'] = [
                        'type'        => Types::int(),
                        'description' => __( 'The number of comments. Even though WPGraphQL denotes this field as an integer, in WordPress this field should be saved as a numeric string for compatability.', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            return ! empty( $post->comment_count ) ? absint( $post->comment_count ) : null;
                        },
                    ];
                }

                /**
                 * If the post_type is Hierarchical, there should be a children field
                 */
                if ( true === $post_type_object->hierarchical ) {
                    $fields[ 'child' . ucfirst( $post_type_object->graphql_plural_name ) ] = PostObjectConnectionDefinition::connection( $post_type_object );
                }

                /**
                 * Add term connections based on the allowed taxonomies that are also
                 * registered to the post_type
                 *
                 * @since 0.0.5
                 */
                if ( ! empty( $allowed_taxonomies ) && is_array( $allowed_taxonomies ) ) {
                    foreach ( $allowed_taxonomies as $taxonomy ) {
                        // If the taxonomy is in the array of taxonomies registered to the post_type
                        if ( in_array( $taxonomy, get_object_taxonomies( $post_type_object->name ), true ) ) {
                            $tax_object                                 = get_taxonomy( $taxonomy );
                            $fields[ $tax_object->graphql_plural_name ] = TermObjectConnectionDefinition::connection( $tax_object );
                        }
                    }
                }

                if ( post_type_supports( $post_type_object->name, 'thumbnail' ) ) {
                    $fields['featuredImage'] = [
                        'type'        => Types::post_object( 'attachment' ),
                        'description' => __( 'The featured image for the object', 'wp-graphql' ),
                        'resolve'     => function( \WP_Post $post, $args, AppContext $context, ResolveInfo $info ) {
                            $thumbnail_id = get_post_thumbnail_id( $post->ID );

                            return ! empty( $thumbnail_id ) ? get_post( absint( $thumbnail_id ) ) : null;
                        },
                    ];
                }

                /**
                 * This prepares the fields by sorting them and applying a filter for adjusting the schema.
                 * Because these fields are implemented via a closure the prepare_fields needs to be applied
                 * to the fields directly instead of being applied to all objects extending
                 * the WPObjectType class.
                 *
                 * @since 0.0.5
                 */
                return self::prepare_fields( $fields, $single_name );
            };
        endif;

        return ! empty( self::$fields[ $single_name ] ) ? self::$fields[ $single_name ] : null;
    }

    /**
     * Define the args to be used by post object fields.
     *
     * @return mixed
     */
    public static function post_object_format_arg() {
        return [
            'type'        => Types::post_object_field_format_enum(),
            'description' => __( 'Format of the field output', 'wp-graphql' ),
        ];
    }
}
API documentation generated by ApiGen