• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14605898576

22 Apr 2025 10:35PM UTC coverage: 82.671% (+0.06%) from 82.613%
14605898576

push

github

web-flow
Merge pull request #3363 from justlevine/fix/cleanup-model-resolvers

fix: cleanup Model fields for better source-of-truth and type-safety.

555 of 599 new or added lines in 27 files covered. (92.65%)

2 existing lines in 2 files now uncovered.

13916 of 16833 relevant lines covered (82.67%)

300.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.34
/src/Type/Connection/Users.php
1
<?php
2
namespace WPGraphQL\Type\Connection;
3

4
use GraphQL\Type\Definition\ResolveInfo;
5
use WPGraphQL\AppContext;
6
use WPGraphQL\Data\Connection\UserConnectionResolver;
7
use WPGraphQL\Data\DataSource;
8
use WPGraphQL\Model\Post;
9
use WPGraphQL\Utils\Utils;
10

11
/**
12
 * Class Users
13
 *
14
 * This class organizes the registration of connections to Users
15
 *
16
 * @package WPGraphQL\Type\Connection
17
 */
18
class Users {
19

20
        /**
21
         * Register connections to Users
22
         *
23
         * @return void
24
         */
25
        public static function register_connections() {
593✔
26

27
                /**
28
                 * Connection from RootQuery to Users
29
                 */
30
                register_graphql_connection(
593✔
31
                        [
593✔
32
                                'fromType'       => 'RootQuery',
593✔
33
                                'toType'         => 'User',
593✔
34
                                'fromFieldName'  => 'users',
593✔
35
                                'resolve'        => static function ( $source, $args, $context, $info ) {
593✔
36
                                        return DataSource::resolve_users_connection( $source, $args, $context, $info );
51✔
37
                                },
593✔
38
                                'connectionArgs' => self::get_connection_args(),
593✔
39
                        ]
593✔
40
                );
593✔
41

42
                register_graphql_connection(
593✔
43
                        [
593✔
44
                                'fromType'           => 'ContentNode',
593✔
45
                                'toType'             => 'User',
593✔
46
                                'connectionTypeName' => 'ContentNodeToEditLockConnection',
593✔
47
                                'edgeFields'         => [
593✔
48
                                        'lockTimestamp' => [
593✔
49
                                                'type'        => 'String',
593✔
50
                                                'description' => __( 'The timestamp for when the node was last edited', 'wp-graphql' ),
593✔
51
                                                'resolve'     => static function ( $edge ) {
593✔
52
                                                        if ( isset( $edge['source'] ) && ( $edge['source'] instanceof Post ) ) {
×
53
                                                                $edit_lock = $edge['source']->editLock;
×
54
                                                                $time      = ( is_array( $edit_lock ) && ! empty( $edit_lock[0] ) ) ? $edit_lock[0] : null;
×
NEW
55
                                                                return ! empty( $time ) ? Utils::prepare_date_response( $time, gmdate( 'Y-m-d H:i:s', (int) $time ) ) : null;
×
56
                                                        }
57
                                                        return null;
×
58
                                                },
593✔
59
                                        ],
593✔
60
                                ],
593✔
61
                                'fromFieldName'      => 'editingLockedBy',
593✔
62
                                'description'        => __( 'If a user has edited the node within the past 15 seconds, this will return the user that last edited. Null if the edit lock doesn\'t exist or is greater than 15 seconds', 'wp-graphql' ),
593✔
63
                                'oneToOne'           => true,
593✔
64
                                'resolve'            => static function ( Post $source, $args, $context, $info ) {
593✔
65
                                        if ( ! isset( $source->editLock[1] ) || ! absint( $source->editLock[1] ) ) {
3✔
66
                                                return null;
3✔
67
                                        }
68

69
                                        $resolver = new UserConnectionResolver( $source, $args, $context, $info );
×
70
                                        $resolver->one_to_one()->set_query_arg( 'include', [ absint( $source->editLock[1] ) ] );
×
71

72
                                        return $resolver->get_connection();
×
73
                                },
593✔
74
                        ]
593✔
75
                );
593✔
76

77
                register_graphql_connection(
593✔
78
                        [
593✔
79
                                'fromType'           => 'ContentNode',
593✔
80
                                'toType'             => 'User',
593✔
81
                                'fromFieldName'      => 'lastEditedBy',
593✔
82
                                'connectionTypeName' => 'ContentNodeToEditLastConnection',
593✔
83
                                'description'        => __( 'The user that most recently edited the node', 'wp-graphql' ),
593✔
84
                                'oneToOne'           => true,
593✔
85
                                'resolve'            => static function ( Post $source, $args, $context, $info ) {
593✔
86
                                        if ( empty( $source->editLastId ) ) {
3✔
87
                                                return null;
×
88
                                        }
89

90
                                        $resolver = new UserConnectionResolver( $source, $args, $context, $info );
3✔
91
                                        $resolver->set_query_arg( 'include', [ absint( $source->editLastId ) ] );
3✔
92
                                        return $resolver->one_to_one()->get_connection();
3✔
93
                                },
593✔
94
                        ]
593✔
95
                );
593✔
96

97
                register_graphql_connection(
593✔
98
                        [
593✔
99
                                'fromType'      => 'NodeWithAuthor',
593✔
100
                                'toType'        => 'User',
593✔
101
                                'fromFieldName' => 'author',
593✔
102
                                'oneToOne'      => true,
593✔
103
                                'resolve'       => static function ( Post $post, $args, AppContext $context, ResolveInfo $info ) {
593✔
104
                                        if ( empty( $post->authorDatabaseId ) ) {
26✔
105
                                                return null;
4✔
106
                                        }
107

108
                                        $resolver = new UserConnectionResolver( $post, $args, $context, $info );
22✔
109
                                        $resolver->set_query_arg( 'include', [ absint( $post->authorDatabaseId ) ] );
22✔
110
                                        return $resolver->one_to_one()->get_connection();
22✔
111
                                },
593✔
112
                        ]
593✔
113
                );
593✔
114
        }
115

116
        /**
117
         * Returns the connection args for use in the connection
118
         *
119
         * @return array<string,array<string,mixed>>
120
         */
121
        public static function get_connection_args() {
593✔
122
                return [
593✔
123
                        'role'              => [
593✔
124
                                'type'        => 'UserRoleEnum',
593✔
125
                                'description' => __( 'An array of role names that users must match to be included in results. Note that this is an inclusive list: users must match *each* role.', 'wp-graphql' ),
593✔
126
                        ],
593✔
127
                        'roleIn'            => [
593✔
128
                                'type'        => [
593✔
129
                                        'list_of' => 'UserRoleEnum',
593✔
130
                                ],
593✔
131
                                'description' => __( 'An array of role names. Matched users must have at least one of these roles.', 'wp-graphql' ),
593✔
132
                        ],
593✔
133
                        'roleNotIn'         => [
593✔
134
                                'type'        => [
593✔
135
                                        'list_of' => 'UserRoleEnum',
593✔
136
                                ],
593✔
137
                                'description' => __( 'An array of role names to exclude. Users matching one or more of these roles will not be included in results.', 'wp-graphql' ),
593✔
138
                        ],
593✔
139
                        'include'           => [
593✔
140
                                'type'        => [
593✔
141
                                        'list_of' => 'Int',
593✔
142
                                ],
593✔
143
                                'description' => __( 'Array of userIds to include.', 'wp-graphql' ),
593✔
144
                        ],
593✔
145
                        'exclude'           => [ // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
593✔
146
                                'type'        => [
593✔
147
                                        'list_of' => 'Int',
593✔
148
                                ],
593✔
149
                                'description' => __( 'Array of userIds to exclude.', 'wp-graphql' ),
593✔
150
                        ],
593✔
151
                        'search'            => [
593✔
152
                                'type'        => 'String',
593✔
153
                                'description' => __( 'Search keyword. Searches for possible string matches on columns. When "searchColumns" is left empty, it tries to determine which column to search in based on search string.', 'wp-graphql' ),
593✔
154
                        ],
593✔
155
                        'searchColumns'     => [
593✔
156
                                'type'        => [
593✔
157
                                        'list_of' => 'UsersConnectionSearchColumnEnum',
593✔
158
                                ],
593✔
159
                                'description' => __( 'Array of column names to be searched. Accepts \'ID\', \'login\', \'nicename\', \'email\', \'url\'.', 'wp-graphql' ),
593✔
160
                        ],
593✔
161
                        'hasPublishedPosts' => [
593✔
162
                                'type'        => [
593✔
163
                                        'list_of' => 'ContentTypeEnum',
593✔
164
                                ],
593✔
165
                                'description' => __( 'Pass an array of post types to filter results to users who have published posts in those post types.', 'wp-graphql' ),
593✔
166
                        ],
593✔
167
                        'nicename'          => [
593✔
168
                                'type'        => 'String',
593✔
169
                                'description' => __( 'The user nicename.', 'wp-graphql' ),
593✔
170
                        ],
593✔
171
                        'nicenameIn'        => [
593✔
172
                                'type'        => [
593✔
173
                                        'list_of' => 'String',
593✔
174
                                ],
593✔
175
                                'description' => __( 'An array of nicenames to include. Users matching one of these nicenames will be included in results.', 'wp-graphql' ),
593✔
176
                        ],
593✔
177
                        'nicenameNotIn'     => [
593✔
178
                                'type'        => [
593✔
179
                                        'list_of' => 'String',
593✔
180
                                ],
593✔
181
                                'description' => __( 'An array of nicenames to exclude. Users matching one of these nicenames will not be included in results.', 'wp-graphql' ),
593✔
182
                        ],
593✔
183
                        'login'             => [
593✔
184
                                'type'        => 'String',
593✔
185
                                'description' => __( 'The user login.', 'wp-graphql' ),
593✔
186
                        ],
593✔
187
                        'loginIn'           => [
593✔
188
                                'type'        => [
593✔
189
                                        'list_of' => 'String',
593✔
190
                                ],
593✔
191
                                'description' => __( 'An array of logins to include. Users matching one of these logins will be included in results.', 'wp-graphql' ),
593✔
192
                        ],
593✔
193
                        'loginNotIn'        => [
593✔
194
                                'type'        => [
593✔
195
                                        'list_of' => 'String',
593✔
196
                                ],
593✔
197
                                'description' => __( 'An array of logins to exclude. Users matching one of these logins will not be included in results.', 'wp-graphql' ),
593✔
198
                        ],
593✔
199
                        'orderby'           => [
593✔
200
                                'type'        => [
593✔
201
                                        'list_of' => 'UsersConnectionOrderbyInput',
593✔
202
                                ],
593✔
203
                                'description' => __( 'What parameter to use to order the objects by.', 'wp-graphql' ),
593✔
204
                        ],
593✔
205
                ];
593✔
206
        }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc