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 
<?php
namespace WPGraphQL\Type\PostObject;

use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Type\WPInputObjectType;
use WPGraphQL\Types;

/**
 * Class PostObjectQuery
 * @package WPGraphQL\Type\PostObject
 * @Since 0.0.5
 */
class PostObjectQuery {

    /**
     * Holds the root_query field definition
     * @var array $root_query
     * @since 0.0.5
     */
    private static $root_query;

    /**
     * Holds the definition of the $post_object_by field
     * @var array $post_object_by
     */
    private static $post_object_by;

    /**
     * Holds the definition for the args that can be input on the $post_object_by field
     * @var array $post_object_by_args
     */
    private static $post_object_by_args;

    /**
     * Method that returns the root query field definition for the post object type
     *
     * @param object $post_type_object
     * @return array
     * @since 0.0.5
     */
    public static function root_query( $post_type_object ) {

        if ( null === self::$root_query ) {
            self::$root_query = [];
        }

        if ( ! empty( $post_type_object->name ) && empty( self::$root_query[ $post_type_object->name ] ) ) :
            self::$root_query[ $post_type_object->name ] = [
                'type' => Types::post_object( $post_type_object->name ),
                'description' => sprintf( __( 'A % object', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                'args' => [
                    'id' => Types::non_null( Types::id() ),
                ],
                'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $post_type_object ) {
                    $id_components = Relay::fromGlobalId( $args['id'] );
                    return DataSource::resolve_post_object( $id_components['id'], $post_type_object->name );
                },
            ];
        endif;

        return ! empty( self::$root_query[ $post_type_object->name ] ) ? self::$root_query[ $post_type_object->name ] : null;
    }
    
    /**
     * Method that returns the "post_object_by" field definition to get a post object by id, postId or slug.
     *
     * @param \WP_Post_Type $post_type_object
     * @return array
     */
    public static function post_object_by( \WP_Post_Type $post_type_object ) {

        if ( null === self::$post_object_by ) {
            self::$post_object_by = [];
        }

        if ( ! empty( $post_type_object->name ) && empty( self::$post_object_by[ $post_type_object->name ] ) ) :
            self::$post_object_by[ $post_type_object->name ] = [
                'type' => Types::post_object( $post_type_object->name ),
                'description' => sprintf( __( 'A %s object', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                'args' => self::post_object_by_args( $post_type_object ),
                'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $post_type_object ) {

                    $post_object = null;
            
                    if ( ! empty( $args['id'] ) ) {
                        $id_components = Relay::fromGlobalId( $args['id'] );
                        if ( empty( $id_components['id'] ) || empty( $id_components[ 'type' ] ) ) {
                            throw new UserError( __( 'The "id" is invalid', 'wp-graphql' ) );
                        }
                        $post_object = DataSource::resolve_post_object( absint( $id_components['id'] ), $post_type_object->name );
                    } elseif ( ! empty( $args[ $post_type_object->graphql_single_name . 'Id' ] ) ) {
                        $id = $args[ $post_type_object->graphql_single_name . 'Id' ];
                        $post_object =  DataSource::resolve_post_object( $id, $post_type_object->name );
                    } elseif ( ! empty( $args['uri'] ) ) {
                        $uri = esc_html( $args['uri'] );
                        $post_object = DataSource::get_post_object_by_uri( $uri, 'OBJECT', $post_type_object->name );
                    } elseif ( ! empty( $args['slug'] ) ) {
                        $slug = esc_html( $args['slug'] );
                        $post_object = DataSource::get_post_object_by_uri( $slug, 'OBJECT', $post_type_object->name );
                    }

                    if ( empty( $post_object ) || is_wp_error( $post_object ) ) {
                        throw new UserError( __( 'No resource could by found', 'wp-graphql' ) );
                    }

                    if ( ! $post_object instanceof \WP_Post ) {
                        throw new UserError( __( 'The queried resource is not valid', 'wp-graphql' ) );
                    }

                    if ( $post_type_object->name !== $post_object->post_type ) {
                        throw new UserError( __( 'The queried resource is not the correct type', 'wp-graphql' ) );
                    }

                    return $post_object;

                },
            ];
        endif;
        return ! empty( self::$post_object_by[ $post_type_object->name ] ) ? self::$post_object_by[ $post_type_object->name ] : null;
    }

    /**
     * Define the args to be used by the $postObject.By field
     * @param \WP_Post_Type $post_type_object
     *
     * @return mixed
     */
    public static function post_object_by_args( \WP_Post_Type $post_type_object ) {

        if ( null === self::$post_object_by_args ) {
            self::$post_object_by_args = [];
        }

        if ( empty( self::$post_object_by_args[ ucfirst( $post_type_object->name ) . 'ByArgs' ] ) ) {

            $args = [
                'id' => [
                    'type'        => Types::string(),
                    'description' => sprintf( __( 'Get the object by it\'s global ID', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                ],
                $post_type_object->graphql_single_name . 'Id' => [
                    'type'        => Types::int(),
                    'description' => sprintf( __( 'Get the %s by it\'s database ID', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                ],
                'uri' => [
                    'type'        => Types::string(),
                    'description' => sprintf( __( 'Get the %s by it\'s uri', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                ]
            ];

            if ( false === $post_type_object->hierarchical ) {
                $args['slug'] = [
                    'type' => Types::string(),
                    'description' => sprintf( __( 'Get the %s by it\'s slug (only available for non-hierarchical types)', 'wp-graphql' ), $post_type_object->graphql_single_name ),
                ];
            }

            self::$post_object_by_args[ $post_type_object->name . 'ByArgs' ] = WPInputObjectType::prepare_fields( $args, ucfirst( $post_type_object->name . 'ByArgs' ) );

        }

        return self::$post_object_by_args[ $post_type_object->name . 'ByArgs' ];

    }

}
API documentation generated by ApiGen