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

wp-graphql / wp-graphql / 20311247040

17 Dec 2025 05:15PM UTC coverage: 83.642% (+0.02%) from 83.619%
20311247040

push

github

actions-user
release: merge develop into master for v2.5.4

16296 of 19483 relevant lines covered (83.64%)

264.62 hits per line

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

69.49
/src/Data/DataSource.php
1
<?php
2

3
namespace WPGraphQL\Data;
4

5
use GraphQL\Error\UserError;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use GraphQLRelay\Relay;
8
use WPGraphQL\AppContext;
9
use WPGraphQL\Data\Connection\CommentConnectionResolver;
10
use WPGraphQL\Data\Connection\PluginConnectionResolver;
11
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
12
use WPGraphQL\Data\Connection\TermObjectConnectionResolver;
13
use WPGraphQL\Data\Connection\ThemeConnectionResolver;
14
use WPGraphQL\Data\Connection\UserConnectionResolver;
15
use WPGraphQL\Data\Connection\UserRoleConnectionResolver;
16
use WPGraphQL\Model\Avatar;
17
use WPGraphQL\Model\Comment;
18
use WPGraphQL\Model\CommentAuthor;
19
use WPGraphQL\Model\Menu;
20
use WPGraphQL\Model\Plugin;
21
use WPGraphQL\Model\Post;
22
use WPGraphQL\Model\PostType;
23
use WPGraphQL\Model\Taxonomy;
24
use WPGraphQL\Model\Term;
25
use WPGraphQL\Model\Theme;
26
use WPGraphQL\Model\User;
27
use WPGraphQL\Model\UserRole;
28
use WPGraphQL\Registry\TypeRegistry;
29

30
/**
31
 * Class DataSource
32
 *
33
 * This class serves as a factory for all the resolvers for queries and mutations. This layer of
34
 * abstraction over the actual resolve functions allows easier, granular control over versioning as
35
 * we can change big things behind the scenes if/when needed, and we just need to ensure the
36
 * call to the DataSource method returns the expected data later on. This should make it easy
37
 * down the road to version resolvers if/when changes to the WordPress API are rolled out.
38
 *
39
 * @package WPGraphQL\Data
40
 * @since   0.0.4
41
 */
42
class DataSource {
43

44
        /**
45
         * Stores an array of node definitions
46
         *
47
         * @var mixed[] $node_definition
48
         * @since  0.0.4
49
         */
50
        protected static $node_definition;
51

52

53
        /**
54
         * Retrieves a WP_Comment object for the ID that gets passed
55
         *
56
         * @param int $comment_id The ID of the comment the comment author is associated with.
57
         *
58
         * @return \WPGraphQL\Model\CommentAuthor|null
59
         * @throws \Exception Throws Exception.
60
         */
61
        public static function resolve_comment_author( int $comment_id ) {
×
62
                $comment_author = get_comment( $comment_id );
×
63

64
                return ! empty( $comment_author ) ? new CommentAuthor( $comment_author ) : null;
×
65
        }
66

67
        /**
68
         * Wrapper for the CommentsConnectionResolver class
69
         *
70
         * @param mixed                                $source  The object the connection is coming from
71
         * @param array<string,mixed>                  $args    Query args to pass to the connection resolver
72
         * @param \WPGraphQL\AppContext                $context The context of the query to pass along
73
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
74
         *
75
         * @return \GraphQL\Deferred
76
         * @throws \Exception
77
         * @since 0.0.5
78
         */
79
        public static function resolve_comments_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
17✔
80
                $resolver = new CommentConnectionResolver( $source, $args, $context, $info );
17✔
81

82
                return $resolver->get_connection();
17✔
83
        }
84

85
        /**
86
         * Wrapper for PluginsConnectionResolver::resolve
87
         *
88
         * @param mixed                                $source  The object the connection is coming from
89
         * @param array<string,mixed>                  $args    Array of arguments to pass to resolve method
90
         * @param \WPGraphQL\AppContext                $context AppContext object passed down
91
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
92
         *
93
         * @return \GraphQL\Deferred
94
         * @throws \Exception
95
         * @since  0.0.5
96
         */
97
        public static function resolve_plugins_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
7✔
98
                $resolver = new PluginConnectionResolver( $source, $args, $context, $info );
7✔
99
                return $resolver->get_connection();
7✔
100
        }
101

102
        /**
103
         * Wrapper for PostObjectsConnectionResolver
104
         *
105
         * @param mixed                                $source    The object the connection is coming from
106
         * @param array<string,mixed>                  $args      Arguments to pass to the resolve method
107
         * @param \WPGraphQL\AppContext                $context AppContext object to pass down
108
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
109
         * @param mixed|string|string[]                $post_type Post type of the post we are trying to resolve
110
         *
111
         * @return \GraphQL\Deferred
112
         * @throws \Exception
113
         * @since  0.0.5
114
         */
115
        public static function resolve_post_objects_connection( $source, array $args, AppContext $context, ResolveInfo $info, $post_type ) {
133✔
116
                $resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, $post_type );
133✔
117

118
                return $resolver->get_connection();
133✔
119
        }
120

121
        /**
122
         * Retrieves the taxonomy object for the name of the taxonomy passed
123
         *
124
         * @param string $taxonomy Name of the taxonomy you want to retrieve the taxonomy object for
125
         *
126
         * @return \WPGraphQL\Model\Taxonomy object
127
         * @throws \GraphQL\Error\UserError If no taxonomy is found with the name passed.
128
         * @since  0.0.5
129
         */
130
        public static function resolve_taxonomy( $taxonomy ) {
×
131

132
                /**
133
                 * Get the allowed_taxonomies.
134
                 */
135
                $allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies();
×
136

137
                if ( ! in_array( $taxonomy, $allowed_taxonomies, true ) ) {
×
138
                        // translators: %s is the name of the taxonomy.
139
                        throw new UserError( esc_html( sprintf( __( 'No taxonomy was found with the name %s', 'wp-graphql' ), $taxonomy ) ) );
×
140
                }
141

142
                $tax_object = get_taxonomy( $taxonomy );
×
143

144
                if ( ! $tax_object instanceof \WP_Taxonomy ) {
×
145
                        // translators: %s is the name of the taxonomy.
146
                        throw new UserError( esc_html( sprintf( __( 'No taxonomy was found with the name %s', 'wp-graphql' ), $taxonomy ) ) );
×
147
                }
148

149
                return new Taxonomy( $tax_object );
×
150
        }
151

152
        /**
153
         * Wrapper for TermObjectConnectionResolver::resolve
154
         *
155
         * @param mixed                                $source   The object the connection is coming from
156
         * @param array<string,mixed>                  $args     Array of args to be passed to the resolve method
157
         * @param \WPGraphQL\AppContext                $context The AppContext object to be passed down
158
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
159
         * @param string                               $taxonomy The name of the taxonomy the term belongs to
160
         *
161
         * @return \GraphQL\Deferred
162
         * @throws \Exception
163
         * @since  0.0.5
164
         */
165
        public static function resolve_term_objects_connection( $source, array $args, AppContext $context, ResolveInfo $info, string $taxonomy ) {
27✔
166
                $resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $taxonomy );
27✔
167

168
                return $resolver->get_connection();
27✔
169
        }
170

171
        /**
172
         * Retrieves the theme object for the theme you are looking for
173
         *
174
         * @param string $stylesheet Directory name for the theme.
175
         *
176
         * @return \WPGraphQL\Model\Theme object
177
         * @throws \GraphQL\Error\UserError
178
         * @since  0.0.5
179
         */
180
        public static function resolve_theme( $stylesheet ) {
2✔
181
                $theme = wp_get_theme( $stylesheet );
2✔
182
                if ( $theme->exists() ) {
2✔
183
                        return new Theme( $theme );
1✔
184
                } else {
185
                        // translators: %s is the name of the theme stylesheet.
186
                        throw new UserError( esc_html( sprintf( __( 'No theme was found with the stylesheet: %s', 'wp-graphql' ), $stylesheet ) ) );
1✔
187
                }
188
        }
189

190
        /**
191
         * Wrapper for the ThemesConnectionResolver::resolve method
192
         *
193
         * @param mixed                                $source  The object the connection is coming from
194
         * @param array<string,mixed>                  $args    Passes an array of arguments to the resolve method
195
         * @param \WPGraphQL\AppContext                $context The AppContext object to be passed down
196
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
197
         *
198
         * @return \GraphQL\Deferred
199
         * @throws \Exception
200
         * @since  0.0.5
201
         */
202
        public static function resolve_themes_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
×
203
                $resolver = new ThemeConnectionResolver( $source, $args, $context, $info );
×
204
                return $resolver->get_connection();
×
205
        }
206

207
        /**
208
         * Wrapper for the UsersConnectionResolver::resolve method
209
         *
210
         * @param mixed                                $source  The object the connection is coming from
211
         * @param array<string,mixed>                  $args    Array of args to be passed down to the resolve method
212
         * @param \WPGraphQL\AppContext                $context The AppContext object to be passed down
213
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
214
         *
215
         * @return \GraphQL\Deferred
216
         * @throws \Exception
217
         * @since  0.0.5
218
         */
219
        public static function resolve_users_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
52✔
220
                $resolver = new UserConnectionResolver( $source, $args, $context, $info );
52✔
221

222
                return $resolver->get_connection();
49✔
223
        }
224

225
        /**
226
         * Returns an array of data about the user role you are requesting
227
         *
228
         * @param string $name Name of the user role you want info for
229
         *
230
         * @return \WPGraphQL\Model\UserRole
231
         * @throws \GraphQL\Error\UserError If no user role is found with the name passed.
232
         * @since  0.0.30
233
         */
234
        public static function resolve_user_role( $name ) {
2✔
235
                $role = isset( wp_roles()->roles[ $name ] ) ? wp_roles()->roles[ $name ] : null;
2✔
236

237
                if ( null === $role ) {
2✔
238
                        // translators: %s is the name of the user role.
239
                        throw new UserError( esc_html( sprintf( __( 'No user role was found with the name %s', 'wp-graphql' ), $name ) ) );
1✔
240
                } else {
241
                        $role                = (array) $role;
1✔
242
                        $role['id']          = $name;
1✔
243
                        $role['displayName'] = $role['name'];
1✔
244
                        $role['name']        = $name;
1✔
245

246
                        return new UserRole( $role );
1✔
247
                }
248
        }
249

250
        /**
251
         * Resolve the avatar for a user
252
         *
253
         * @param int                 $user_id ID of the user to get the avatar data for
254
         * @param array<string,mixed> $args    The args to pass to the get_avatar_data function
255
         *
256
         * @return \WPGraphQL\Model\Avatar|null
257
         * @throws \Exception
258
         */
259
        public static function resolve_avatar( int $user_id, array $args ) {
6✔
260
                $avatar = get_avatar_data( absint( $user_id ), $args );
6✔
261

262
                // if there's no url returned, return null
263
                if ( empty( $avatar['url'] ) ) {
6✔
264
                        return null;
×
265
                }
266

267
                $avatar = new Avatar( $avatar );
6✔
268

269
                if ( 'private' === $avatar->get_visibility() ) {
6✔
270
                        return null;
1✔
271
                }
272

273
                return $avatar;
6✔
274
        }
275

276
        /**
277
         * Resolve the connection data for user roles
278
         *
279
         * @param mixed[]                              $source  The Query results
280
         * @param array<string,mixed>                  $args    The query arguments
281
         * @param \WPGraphQL\AppContext                $context The AppContext passed down to the query
282
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
283
         *
284
         * @return \GraphQL\Deferred
285
         * @throws \Exception
286
         */
287
        public static function resolve_user_role_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
×
288
                $resolver = new UserRoleConnectionResolver( $source, $args, $context, $info );
×
289

290
                return $resolver->get_connection();
×
291
        }
292

293
        /**
294
         * Format the setting group name to our standard.
295
         *
296
         * @param string $group
297
         *
298
         * @return string $group
299
         */
300
        public static function format_group_name( string $group ) {
633✔
301
                $replaced_group = graphql_format_name( $group, ' ', '/[^a-zA-Z0-9 -]/' );
633✔
302

303
                if ( ! empty( $replaced_group ) ) {
633✔
304
                        $group = $replaced_group;
633✔
305
                }
306

307
                $group = lcfirst( str_replace( '_', ' ', ucwords( $group, '_' ) ) );
633✔
308
                $group = lcfirst( str_replace( '-', ' ', ucwords( $group, '_' ) ) );
633✔
309
                $group = lcfirst( str_replace( ' ', '', ucwords( $group, ' ' ) ) );
633✔
310

311
                return $group;
633✔
312
        }
313

314
        /**
315
         * Get all of the allowed settings by group and return the
316
         * settings group that matches the group param
317
         *
318
         * @param string                           $group
319
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
320
         *
321
         * @return array<string,mixed>
322
         */
323
        public static function get_setting_group_fields( string $group, TypeRegistry $type_registry ) {
633✔
324

325
                /**
326
                 * Get all of the settings, sorted by group
327
                 */
328
                $settings_groups = self::get_allowed_settings_by_group( $type_registry );
633✔
329

330
                return ! empty( $settings_groups[ $group ] ) ? $settings_groups[ $group ] : [];
633✔
331
        }
332

333
        /**
334
         * Get all of the allowed settings by group
335
         *
336
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
337
         *
338
         * @return array<string,array<string,mixed>> $allowed_settings_by_group
339
         */
340
        public static function get_allowed_settings_by_group( TypeRegistry $type_registry ) {
633✔
341

342
                /**
343
                 * Get all registered settings
344
                 */
345
                $registered_settings = get_registered_settings();
633✔
346

347
                /**
348
                 * Loop through the $registered_settings array and build an array of
349
                 * settings for each group ( general, reading, discussion, writing, reading, etc. )
350
                 * if the setting is allowed in REST or GraphQL
351
                 */
352
                $allowed_settings_by_group = [];
633✔
353
                foreach ( $registered_settings as $key => $setting ) {
633✔
354
                        // Bail if the setting doesn't have a group.
355
                        if ( ! isset( $setting['group'] ) || empty( $setting['group'] ) ) {
633✔
356
                                continue;
×
357
                        }
358

359
                        /** @var string $setting_group */
360
                        $setting_group = $setting['group'];
633✔
361
                        $group         = self::format_group_name( $setting_group );
633✔
362

363
                        if ( ! isset( $setting['type'] ) || ! $type_registry->get_type( $setting['type'] ) ) {
633✔
364
                                continue;
×
365
                        }
366

367
                        if ( ! isset( $setting['show_in_graphql'] ) ) {
633✔
368
                                if ( isset( $setting['show_in_rest'] ) && false !== $setting['show_in_rest'] ) {
633✔
369
                                        $setting['key']                              = $key;
633✔
370
                                        $allowed_settings_by_group[ $group ][ $key ] = $setting;
633✔
371
                                }
372
                        } elseif ( true === $setting['show_in_graphql'] ) {
116✔
373
                                $setting['key']                              = $key;
116✔
374
                                $allowed_settings_by_group[ $group ][ $key ] = $setting;
116✔
375
                        }
376
                }
377

378
                /**
379
                 * Set the setting groups that are allowed
380
                 */
381
                $allowed_settings_by_group = ! empty( $allowed_settings_by_group ) ? $allowed_settings_by_group : [];
633✔
382

383
                /**
384
                 * Filter the $allowed_settings_by_group to allow enabling or disabling groups in the GraphQL Schema.
385
                 *
386
                 * @since 0.0.1
387
                 */
388
                return apply_filters( 'graphql_allowed_settings_by_group', $allowed_settings_by_group );
633✔
389
        }
390

391
        /**
392
         * Get all of the $allowed_settings
393
         *
394
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The WPGraphQL TypeRegistry
395
         *
396
         * @return array<string,array<string,mixed>> $allowed_settings
397
         */
398
        public static function get_allowed_settings( TypeRegistry $type_registry ) {
633✔
399

400
                /**
401
                 * Get all registered settings
402
                 */
403
                $registered_settings = get_registered_settings();
633✔
404

405
                /**
406
                 * Set allowed settings variable.
407
                 */
408
                $allowed_settings = [];
633✔
409

410
                if ( ! empty( $registered_settings ) ) {
633✔
411

412
                        /**
413
                         * Loop through the $registered_settings and if the setting is allowed in REST or GraphQL
414
                         * add it to the $allowed_settings array
415
                         */
416
                        foreach ( $registered_settings as $key => $setting ) {
633✔
417
                                if ( ! isset( $setting['type'] ) || ! $type_registry->get_type( $setting['type'] ) ) {
633✔
418
                                        continue;
×
419
                                }
420

421
                                if ( ! isset( $setting['show_in_graphql'] ) ) {
633✔
422
                                        if ( isset( $setting['show_in_rest'] ) && false !== $setting['show_in_rest'] ) {
633✔
423
                                                $setting['key']           = $key;
633✔
424
                                                $allowed_settings[ $key ] = $setting;
633✔
425
                                        }
426
                                } elseif ( true === $setting['show_in_graphql'] ) {
116✔
427
                                        $setting['key']           = $key;
116✔
428
                                        $allowed_settings[ $key ] = $setting;
116✔
429
                                }
430
                        }
431
                }
432

433
                /**
434
                 * Verify that we have the allowed settings
435
                 */
436
                $allowed_settings = ! empty( $allowed_settings ) ? $allowed_settings : [];
633✔
437

438
                /**
439
                 * Filter the $allowed_settings to allow some to be enabled or disabled from showing in
440
                 * the GraphQL Schema.
441
                 *
442
                 * @since 0.0.1
443
                 */
444
                return apply_filters( 'graphql_allowed_setting_groups', $allowed_settings );
633✔
445
        }
446

447
        /**
448
         * We get the node interface and field from the relay library.
449
         *
450
         * The first method is the way we resolve an ID to its object. The second is the way we resolve
451
         * an object that implements node to its type.
452
         *
453
         * @return mixed[]
454
         * @throws \GraphQL\Error\UserError
455
         */
456
        public static function get_node_definition() {
×
457
                if ( null === self::$node_definition ) {
×
458
                        $node_definition = Relay::nodeDefinitions(
×
459
                        // The ID fetcher definition
460
                                static function ( $global_id, AppContext $context, ResolveInfo $info ) {
×
461
                                        self::resolve_node( $global_id, $context, $info );
×
462
                                },
×
463
                                // Type resolver
464
                                static function ( $node ) {
×
465
                                        self::resolve_node_type( $node );
×
466
                                }
×
467
                        );
×
468

469
                        self::$node_definition = $node_definition;
×
470
                }
471

472
                return self::$node_definition;
×
473
        }
474

475
        /**
476
         * Given a node, returns the GraphQL Type
477
         *
478
         * @param mixed $node The node to resolve the type of
479
         *
480
         * @return string
481
         * @throws \GraphQL\Error\UserError If no type is found for the node.
482
         */
483
        public static function resolve_node_type( $node ) {
13✔
484
                $type = null;
13✔
485

486
                if ( true === is_object( $node ) ) {
13✔
487
                        switch ( true ) {
488
                                case $node instanceof Post:
13✔
489
                                        if ( $node->isRevision ) {
4✔
490
                                                /** @var ?\WP_Post */
491
                                                $parent_post = get_post( $node->parentDatabaseId );
×
492

493
                                                if ( ! empty( $parent_post ) ) {
×
494
                                                        /** @var \WP_Post_Type $post_type_object */
495
                                                        $post_type_object = get_post_type_object( $parent_post->post_type );
×
496
                                                        $type             = $post_type_object->graphql_single_name ?? null;
×
497

498
                                                        break;
×
499
                                                }
500
                                        }
501

502
                                        /** @var \WP_Post_Type $post_type_object */
503
                                        $post_type_object = isset( $node->post_type ) ? get_post_type_object( $node->post_type ) : null;
4✔
504
                                        $type             = $post_type_object->graphql_single_name ?? null;
4✔
505
                                        break;
4✔
506
                                case $node instanceof Term:
9✔
507
                                        /** @var \WP_Taxonomy $tax_object */
508
                                        $tax_object = isset( $node->taxonomyName ) ? get_taxonomy( $node->taxonomyName ) : null;
×
509
                                        $type       = $tax_object->graphql_single_name;
×
510
                                        break;
×
511
                                case $node instanceof Comment:
9✔
512
                                        $type = 'Comment';
1✔
513
                                        break;
1✔
514
                                case $node instanceof PostType:
8✔
515
                                        $type = 'ContentType';
1✔
516
                                        break;
1✔
517
                                case $node instanceof Taxonomy:
7✔
518
                                        $type = 'Taxonomy';
1✔
519
                                        break;
1✔
520
                                case $node instanceof Theme:
6✔
521
                                        $type = 'Theme';
1✔
522
                                        break;
1✔
523
                                case $node instanceof User:
5✔
524
                                        $type = 'User';
3✔
525
                                        break;
3✔
526
                                case $node instanceof Plugin:
2✔
527
                                        $type = 'Plugin';
1✔
528
                                        break;
1✔
529
                                case $node instanceof CommentAuthor:
1✔
530
                                        $type = 'CommentAuthor';
1✔
531
                                        break;
1✔
532
                                case $node instanceof Menu:
×
533
                                        $type = 'Menu';
×
534
                                        break;
×
535
                                case $node instanceof \_WP_Dependency:
×
536
                                        $type = isset( $node->type ) ? $node->type : null;
×
537
                                        break;
×
538
                                default:
539
                                        $type = null;
×
540
                        }
541
                }
542

543
                /**
544
                 * Add a filter to allow externally registered node types to return the proper type
545
                 * based on the node_object that's returned
546
                 *
547
                 * @param mixed|object|array $type The type definition the node should resolve to.
548
                 * @param mixed|object|array $node The $node that is being resolved
549
                 *
550
                 * @since 0.0.6
551
                 */
552
                $type = apply_filters( 'graphql_resolve_node_type', $type, $node );
13✔
553

554
                /**
555
                 * If the $type is not properly resolved, throw an exception
556
                 *
557
                 * @since 0.0.6
558
                 */
559
                if ( empty( $type ) ) {
13✔
560
                        throw new UserError( esc_html__( 'No type was found matching the node', 'wp-graphql' ) );
×
561
                }
562

563
                /**
564
                 * Return the resolved $type for the $node
565
                 *
566
                 * @since 0.0.5
567
                 */
568
                return ucfirst( $type );
13✔
569
        }
570

571
        /**
572
         * Given the ID of a node, this resolves the data
573
         *
574
         * @param string                               $global_id The Global ID of the node
575
         * @param \WPGraphQL\AppContext                $context The Context of the GraphQL Request
576
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo for the GraphQL Request
577
         *
578
         * @return ?\GraphQL\Deferred
579
         * @throws \GraphQL\Error\UserError If no ID is passed.
580
         */
581
        public static function resolve_node( $global_id, AppContext $context, ResolveInfo $info ) {
17✔
582
                if ( empty( $global_id ) ) {
17✔
583
                        throw new UserError( esc_html__( 'An ID needs to be provided to resolve a node.', 'wp-graphql' ) );
×
584
                }
585

586
                /**
587
                 * Convert the encoded ID into an array we can work with
588
                 *
589
                 * @since 0.0.4
590
                 */
591
                $id_components = Relay::fromGlobalId( $global_id );
17✔
592

593
                /**
594
                 * $id_components is an array with the id and type
595
                 *
596
                 * @since 0.0.5
597
                 */
598
                if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
17✔
599
                        // translators: %s is the global ID.
600
                        throw new UserError( esc_html( sprintf( __( 'The global ID isn\'t recognized ID: %s', 'wp-graphql' ), $global_id ) ) );
1✔
601
                }
602

603
                /**
604
                 * Get the allowed_post_types and allowed_taxonomies
605
                 *
606
                 * @since 0.0.5
607
                 */
608

609
                $loader = $context->get_loader( $id_components['type'] );
16✔
610

611
                if ( $loader ) {
16✔
612
                        return $loader->load_deferred( $id_components['id'] );
16✔
613
                }
614

615
                return null;
×
616
        }
617

618
        /**
619
         * Returns array of nav menu location names
620
         *
621
         * @return string[]
622
         */
623
        public static function get_registered_nav_menu_locations() {
633✔
624
                global $_wp_registered_nav_menus;
633✔
625

626
                return ! empty( $_wp_registered_nav_menus ) && is_array( $_wp_registered_nav_menus ) ? array_keys( $_wp_registered_nav_menus ) : [];
633✔
627
        }
628

629
        /**
630
         * This resolves a resource, given a URI (the path / permalink to a resource)
631
         *
632
         * Based largely on the core parse_request function in wp-includes/class-wp.php
633
         *
634
         * @param string                               $uri     The URI to fetch a resource from
635
         * @param \WPGraphQL\AppContext                $context The AppContext passed through the GraphQL Resolve Tree
636
         * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed through the GraphQL Resolve tree
637
         *
638
         * @return \GraphQL\Deferred
639
         * @throws \Exception
640
         */
641
        public static function resolve_resource_by_uri( $uri, $context, $info ) {
×
642
                $node_resolver = new NodeResolver( $context );
×
643

644
                return $node_resolver->resolve_uri( $uri );
×
645
        }
646

647
        /**
648
         * @todo remove in 3.0.0
649
         * @deprecated Use the Loader passed in $context instead
650
         * @codeCoverageIgnore
651
         *
652
         * @param int                   $id      ID of the comment we want to get the object for.
653
         * @param \WPGraphQL\AppContext $context The context of the request.
654
         *
655
         * @return \GraphQL\Deferred object
656
         * @throws \GraphQL\Error\UserError Throws UserError.
657
         * @throws \Exception Throws UserError.
658
         */
659
        public static function resolve_comment( $id, $context ) {
660
                _doing_it_wrong(
661
                        __METHOD__,
662
                        sprintf(
663
                                /* translators: %s is the method name */
664
                                esc_html__( 'This method will be removed in the next major release. Use %s instead.', 'wp-graphql' ),
665
                                '$context->get_loader( \'comment\' )->load_deferred( $id )'
666
                        ),
667
                        '0.8.4'
668
                );
669

670
                return $context->get_loader( 'comment' )->load_deferred( $id );
671
        }
672

673
        /**
674
         * @todo remove in 3.0.0
675
         * @deprecated Use the Loader passed in $context instead
676
         * @codeCoverageIgnore
677
         *
678
         * @param int                   $id      ID of the post you are trying to retrieve
679
         * @param \WPGraphQL\AppContext $context The context of the GraphQL Request
680
         *
681
         * @return \GraphQL\Deferred
682
         *
683
         * @throws \GraphQL\Error\UserError
684
         * @throws \Exception
685
         */
686
        public static function resolve_post_object( int $id, AppContext $context ) {
687
                _doing_it_wrong(
688
                        __METHOD__,
689
                        sprintf(
690
                                /* translators: %s is the method name */
691
                                esc_html__( 'This method will be removed in the next major release. Use %s instead.', 'wp-graphql' ),
692
                                '$context->get_loader( \'post\' )->load_deferred( $id )'
693
                        ),
694
                        '0.8.4'
695
                );
696
                return $context->get_loader( 'post' )->load_deferred( $id );
697
        }
698

699
        /**
700
         * @todo remove in 3.0.0
701
         * @deprecated Use the Loader passed in $context instead
702
         * @codeCoverageIgnore
703
         *
704
         * @param int                   $id      The ID of the menu item to load
705
         * @param \WPGraphQL\AppContext $context The context of the GraphQL request
706
         *
707
         * @return \GraphQL\Deferred|null
708
         * @throws \Exception
709
         */
710
        public static function resolve_menu_item( int $id, AppContext $context ) {
711
                _doing_it_wrong(
712
                        __METHOD__,
713
                        sprintf(
714
                                /* translators: %s is the method name */
715
                                esc_html__( 'This method will be removed in the next major release. Use %s instead.', 'wp-graphql' ),
716
                                '$context->get_loader( \'menu_item\' )->load_deferred( $id )'
717
                        ),
718
                        '0.8.4'
719
                );
720
                return $context->get_loader( 'post' )->load_deferred( $id );
721
        }
722

723
        /**
724
         * @todo remove in 3.0.0
725
         * @deprecated Use the Loader passed in $context instead
726
         * @codeCoverageIgnore
727
         *
728
         * @param int                   $id      ID of the term you are trying to retrieve the object for
729
         * @param \WPGraphQL\AppContext $context The context of the GraphQL Request
730
         *
731
         * @return \GraphQL\Deferred
732
         * @throws \Exception
733
         */
734
        public static function resolve_term_object( $id, AppContext $context ) {
735
                _doing_it_wrong(
736
                        __METHOD__,
737
                        sprintf(
738
                                /* translators: %s is the method name */
739
                                esc_html__( 'This method will be removed in the next major release. Use %s instead.', 'wp-graphql' ),
740
                                '$context->get_loader( \'term\' )->load_deferred( $id )'
741
                        ),
742
                        '0.8.4'
743
                );
744
                return $context->get_loader( 'term' )->load_deferred( $id );
745
        }
746

747
        /**
748
         * @todo remove in 3.0.0
749
         * @deprecated Use the Loader passed in $context instead
750
         * @codeCoverageIgnore
751
         *
752
         * @param int                   $id      ID of the user you want the object for
753
         * @param \WPGraphQL\AppContext $context The AppContext
754
         *
755
         * @return \GraphQL\Deferred
756
         * @throws \Exception
757
         */
758
        public static function resolve_user( $id, AppContext $context ) {
759
                _doing_it_wrong(
760
                        __METHOD__,
761
                        sprintf(
762
                                /* translators: %s is the method name */
763
                                esc_html__( 'This method will be removed in the next major release. Use %s instead.', 'wp-graphql' ),
764
                                '$context->get_loader( \'user\' )->load_deferred( $id )'
765
                        ),
766
                        '0.8.4'
767
                );
768
                return $context->get_loader( 'user' )->load_deferred( $id );
769
        }
770
}
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

© 2025 Coveralls, Inc