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

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\ConnectionResolver;
use WPGraphQL\Types;

/**
 * Class UserConnectionResolver
 *
 * @package WPGraphQL\Data\Resolvers
 * @since 0.5.0
 */
class UserConnectionResolver extends ConnectionResolver {

    /**
     * This runs the query and returns the repsonse
     *
     * @param $query_args
     *
     * @return \WP_User_Query
     */
    public static function get_query( $query_args ) {
        $query = new \WP_User_Query( $query_args );
        return $query;
    }

    /**
     * This returns the $query_args that should be used when querying for posts in the postObjectConnectionResolver.
     * This checks what input $args are part of the query, combines them with various filters, etc and returns an
     * array of $query_args to be used in the \WP_Query call
     *
     * @param mixed       $source    The query source being passed down to the resolver
     * @param array       $args      The arguments that were provided to the query
     * @param AppContext  $context   Object containing app context that gets passed down the resolve tree
     * @param ResolveInfo $info      Info about fields passed down the resolve tree
     *
     * @return array
     * @throws \Exception
     */
    public static function get_query_args( $source, array $args, AppContext $context, ResolveInfo $info ) {

        /**
         * Set the $query_args based on various defaults and primary input $args
         */
        $query_args['count_total'] = false;
        $query_args['offset'] = self::get_offset( $args );
        $query_args['order'] = ! empty( $args['last'] ) ? 'ASC' : 'DESC';

        /**
         * If "pageInfo" is in the fieldSelection, we need to calculate the pagination details, so
         * we need to run the query with count_total set to true.
         */
        $field_selection = $info->getFieldSelection( 2 );
        if ( ! empty( $field_selection['pageInfo'] ) ) {
            $query_args['count_total'] = true;
        }

        /**
         * Set the number, ensuring it doesn't exceed the amount set as the $max_query_amount
         */
        $query_args['number'] = self::get_query_amount( $source, $args, $context, $info );

        /**
         * Take any of the input $args (under the "where" input) that were part of the GraphQL query and map and
         * sanitize their GraphQL input to apply to the WP_Query
         */
        $input_fields = [];
        if ( ! empty( $args['where'] ) ) {
            $input_fields = self::sanitize_input_fields( $args['where'], $source, $args, $context, $info );
        }

        /**
         * Merge the default $query_args with the $args that were entered in the query.
         *
         * @since 0.0.5
         */
        if ( ! empty( $input_fields ) ) {
            $query_args = array_merge( $query_args, $input_fields );
        }

        /**
         * Filter the query_args that should be applied to the query. This filter is applied AFTER the input args from
         * the GraphQL Query have been applied and has the potential to override the GraphQL Query Input Args.
         *
         * @param array       $query_args array of query_args being passed to the
         * @param mixed       $source     source passed down from the resolve tree
         * @param array       $args       array of arguments input in the field as part of the GraphQL query
         * @param AppContext  $context    object passed down zthe resolve tree
         * @param ResolveInfo $info       info about fields passed down the resolve tree
         *
         * @since 0.0.6
         */
        $query_args = apply_filters( 'graphql_user_connection_query_args', $query_args, $source, $args, $context, $info );

        return $query_args;

    }

    /**
     * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_User_Query
     * friendly keys.
     *
     * There's probably a cleaner/more dynamic way to approach this, but this was quick. I'd be
     * down to explore more dynamic ways to map this, but for now this gets the job done.
     *
     * @param array       $args     The query "where" args
     * @param mixed       $source   The query results of the query calling this relation
     * @param array       $all_args Array of all the query args (not just the "where" args)
     * @param AppContext  $context  The AppContext object
     * @param ResolveInfo $info     The ResolveInfo object
     *
     * @since  0.0.5
     * @return array
     * @access private
     */
    public static function sanitize_input_fields( array $args, $source, array $all_args, AppContext $context, ResolveInfo $info ) {

        $arg_mapping = [
            'roleIn'            => 'role__in',
            'roleNotIn'         => 'role__not_in',
            'searchColumns'     => 'search_columns',
            'hasPublishedPosts' => 'has_published_posts',
            'nicenameIn'        => 'nicename__in',
            'nicenameNotIn'     => 'nicename__not_in',
            'loginIn'           => 'login__in',
            'loginNotIn'        => 'login__not_in',
        ];

        /**
         * Map and sanitize the input args to the WP_User_Query compatible args
         */
        $query_args = Types::map_input( $args, $arg_mapping );

        /**
         * Filter the input fields
         *
         * This allows plugins/themes to hook in and alter what $args should be allowed to be passed
         * from a GraphQL Query to the get_terms query
         *
         * @param array       $query_args The mapped query args
         * @param array       $args       The query "where" args
         * @param mixed       $source     The query results of the query calling this relation
         * @param array       $all_args   Array of all the query args (not just the "where" args)
         * @param AppContext  $context    The AppContext object
         * @param ResolveInfo $info       The ResolveInfo object
         *
         * @since 0.0.5
         * @return array
         */
        $query_args = apply_filters( 'graphql_map_input_fields_to_wp_comment_query', $query_args, $args, $source, $all_args, $context, $info );

        return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];

    }

}
API documentation generated by ApiGen