-
Notifications
You must be signed in to change notification settings - Fork 440
/
RootQuery.php
951 lines (888 loc) · 33.6 KB
/
RootQuery.php
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
<?php
namespace WPGraphQL\Type\ObjectType;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\ContentTypeConnectionResolver;
use WPGraphQL\Data\Connection\EnqueuedScriptsConnectionResolver;
use WPGraphQL\Data\Connection\EnqueuedStylesheetConnectionResolver;
use WPGraphQL\Data\Connection\MenuConnectionResolver;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
use WPGraphQL\Data\Connection\ThemeConnectionResolver;
use WPGraphQL\Data\Connection\UserRoleConnectionResolver;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Model\Post;
use WPGraphQL\Type\Connection\PostObjects;
use WPGraphQL\Utils\Utils;
/**
* Class RootQuery
*
* @package WPGraphQL\Type\Object
*/
class RootQuery {
/**
* Register the RootQuery type
*
* @return void
*/
public static function register_type() {
register_graphql_object_type(
'RootQuery',
[
'description' => __( 'The root entry point into the Graph', 'wp-graphql' ),
'connections' => [
'contentTypes' => [
'toType' => 'ContentType',
'resolve' => static function ( $source, $args, $context, $info ) {
$resolver = new ContentTypeConnectionResolver( $source, $args, $context, $info );
return $resolver->get_connection();
},
],
'menus' => [
'toType' => 'Menu',
'connectionArgs' => [
'id' => [
'type' => 'Int',
'description' => __( 'The database ID of the object', 'wp-graphql' ),
],
'location' => [
'type' => 'MenuLocationEnum',
'description' => __( 'The menu location for the menu being queried', 'wp-graphql' ),
],
'slug' => [
'type' => 'String',
'description' => __( 'The slug of the menu to query items for', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, $args, $context, $info ) {
$resolver = new MenuConnectionResolver( $source, $args, $context, $info, 'nav_menu' );
return $resolver->get_connection();
},
],
'plugins' => [
'toType' => 'Plugin',
'connectionArgs' => [
'search' => [
'name' => 'search',
'type' => 'String',
'description' => __( 'Show plugin based on a keyword search.', 'wp-graphql' ),
],
'status' => [
'type' => 'PluginStatusEnum',
'description' => __( 'Show plugins with a specific status.', 'wp-graphql' ),
],
'stati' => [
'type' => [ 'list_of' => 'PluginStatusEnum' ],
'description' => __( 'Retrieve plugins where plugin status is in an array.', 'wp-graphql' ),
],
],
'resolve' => static function ( $root, $args, $context, $info ) {
return DataSource::resolve_plugins_connection( $root, $args, $context, $info );
},
],
'registeredScripts' => [
'toType' => 'EnqueuedScript',
'resolve' => static function ( $source, $args, $context, $info ) {
// The connection resolver expects the source to include
// enqueuedScriptsQueue
$source = new \stdClass();
$source->enqueuedScriptsQueue = [];
global $wp_scripts;
do_action( 'wp_enqueue_scripts' );
$source->enqueuedScriptsQueue = array_keys( $wp_scripts->registered );
$resolver = new EnqueuedScriptsConnectionResolver( $source, $args, $context, $info );
return $resolver->get_connection();
},
],
'registeredStylesheets' => [
'toType' => 'EnqueuedStylesheet',
'resolve' => static function ( $source, $args, $context, $info ) {
// The connection resolver expects the source to include
// enqueuedStylesheetsQueue
$source = new \stdClass();
$source->enqueuedStylesheetsQueue = [];
global $wp_styles;
do_action( 'wp_enqueue_scripts' );
$source->enqueuedStylesheetsQueue = array_keys( $wp_styles->registered );
$resolver = new EnqueuedStylesheetConnectionResolver( $source, $args, $context, $info );
return $resolver->get_connection();
},
],
'themes' => [
'toType' => 'Theme',
'resolve' => static function ( $root, $args, $context, $info ) {
$resolver = new ThemeConnectionResolver( $root, $args, $context, $info );
return $resolver->get_connection();
},
],
'revisions' => [
'toType' => 'ContentNode',
'queryClass' => 'WP_Query',
'connectionArgs' => PostObjects::get_connection_args(),
'resolve' => static function ( $root, $args, $context, $info ) {
$resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'revision' );
return $resolver->get_connection();
},
],
'userRoles' => [
'toType' => 'UserRole',
'fromFieldName' => 'userRoles',
'resolve' => static function ( $user, $args, $context, $info ) {
$resolver = new UserRoleConnectionResolver( $user, $args, $context, $info );
return $resolver->get_connection();
},
],
],
'fields' => [
'allSettings' => [
'type' => 'Settings',
'description' => __( 'Entry point to get all settings for the site', 'wp-graphql' ),
'resolve' => static function () {
return true;
},
],
'comment' => [
'type' => 'Comment',
'description' => __( 'Returns a Comment', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'Unique identifier for the comment node.', 'wp-graphql' ),
],
'idType' => [
'type' => 'CommentNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a comment by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $_source, array $args, AppContext $context ) {
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
switch ( $id_type ) {
case 'database_id':
$id = absint( $args['id'] );
break;
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
}
$id = absint( $id_components['id'] );
break;
}
return $context->get_loader( 'comment' )->load_deferred( $id );
},
],
'contentNode' => [
'type' => 'ContentNode',
'description' => __( 'A node used to manage content', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'Unique identifier for the content node.', 'wp-graphql' ),
],
'idType' => [
'type' => 'ContentNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a content node by. Default is Global ID', 'wp-graphql' ),
],
'contentType' => [
'type' => 'ContentTypeEnum',
'description' => __( 'The content type the node is used for. Required when idType is set to "name" or "slug"', 'wp-graphql' ),
],
'asPreview' => [
'type' => 'Boolean',
'description' => __( 'Whether to return the Preview Node instead of the Published Node. When the ID of a Node is provided along with asPreview being set to true, the preview node with un-published changes will be returned instead of the published node. If no preview node exists or the requester doesn\'t have proper capabilities to preview, no node will be returned. If the ID provided is a URI and has a preview query arg, it will be used as a fallback if the "asPreview" argument is not explicitly provided as an argument.', 'wp-graphql' ),
],
],
'resolve' => static function ( $_root, $args, AppContext $context ) {
$idType = $args['idType'] ?? 'global_id';
switch ( $idType ) {
case 'uri':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'nodeType' => 'ContentNode',
'asPreview' => $args['asPreview'] ?? null,
]
);
case 'database_id':
$post_id = absint( $args['id'] );
break;
case 'global_id':
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid. Make sure you set the proper idType for your input.', 'wp-graphql' ) );
}
$post_id = absint( $id_components['id'] );
break;
}
if ( isset( $args['asPreview'] ) && true === $args['asPreview'] ) {
$post_id = Utils::get_post_preview_id( $post_id );
}
$allowed_post_types = \WPGraphQL::get_allowed_post_types();
$allowed_post_types[] = 'revision';
return absint( $post_id ) ? $context->get_loader( 'post' )->load_deferred( $post_id )->then(
static function ( $post ) use ( $allowed_post_types ) {
// if the post isn't an instance of a Post model, return
if ( ! $post instanceof Post ) {
return null;
}
if ( ! isset( $post->post_type ) || ! in_array( $post->post_type, $allowed_post_types, true ) ) {
return null;
}
return $post;
}
) : null;
},
],
'contentType' => [
'type' => 'ContentType',
'description' => __( 'Fetch a Content Type node by unique Identifier', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'Unique Identifier for the Content Type node.', 'wp-graphql' ),
],
'idType' => [
'type' => 'ContentTypeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a content type by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $_root, $args, $context ) {
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
$id = null;
switch ( $id_type ) {
case 'name':
$id = $args['id'];
break;
case 'id':
default:
$id_parts = Relay::fromGlobalId( $args['id'] );
if ( isset( $id_parts['id'] ) ) {
$id = $id_parts['id'];
}
}
return ! empty( $id ) ? $context->get_loader( 'post_type' )->load_deferred( $id ) : null;
},
],
'taxonomy' => [
'type' => 'Taxonomy',
'description' => __( 'Fetch a Taxonomy node by unique Identifier', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'Unique Identifier for the Taxonomy node.', 'wp-graphql' ),
],
'idType' => [
'type' => 'TaxonomyIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a taxonomy by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $_root, $args, $context ) {
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
$id = null;
switch ( $id_type ) {
case 'name':
$id = $args['id'];
break;
case 'id':
default:
$id_parts = Relay::fromGlobalId( $args['id'] );
if ( isset( $id_parts['id'] ) ) {
$id = $id_parts['id'];
}
}
return ! empty( $id ) ? $context->get_loader( 'taxonomy' )->load_deferred( $id ) : null;
},
],
'node' => [
'type' => 'Node',
'description' => __( 'Fetches an object given its ID', 'wp-graphql' ),
'args' => [
'id' => [
'type' => 'ID',
'description' => __( 'The unique identifier of the node', 'wp-graphql' ),
],
],
'resolve' => static function ( $root, $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $args['id'] ) ? DataSource::resolve_node( $args['id'], $context, $info ) : null;
},
],
'nodeByUri' => [
'type' => 'UniformResourceIdentifiable',
'description' => __( 'Fetches an object given its Unique Resource Identifier', 'wp-graphql' ),
'args' => [
'uri' => [
'type' => [ 'non_null' => 'String' ],
'description' => __( 'Unique Resource Identifier in the form of a path or permalink for a node. Ex: "/hello-world"', 'wp-graphql' ),
],
],
'resolve' => static function ( $root, $args, AppContext $context ) {
return ! empty( $args['uri'] ) ? $context->node_resolver->resolve_uri( $args['uri'] ) : null;
},
],
'menu' => [
'type' => 'Menu',
'description' => __( 'A WordPress navigation menu', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the menu.', 'wp-graphql' ),
],
'idType' => [
'type' => 'MenuNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a menu by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args, AppContext $context ) {
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
switch ( $id_type ) {
case 'database_id':
$id = absint( $args['id'] );
break;
case 'location':
$locations = get_nav_menu_locations();
if ( ! isset( $locations[ $args['id'] ] ) || ! absint( $locations[ $args['id'] ] ) ) {
throw new UserError( esc_html__( 'No menu set for the provided location', 'wp-graphql' ) );
}
$id = absint( $locations[ $args['id'] ] );
break;
case 'name':
$menu = new \WP_Term_Query(
[
'taxonomy' => 'nav_menu',
'fields' => 'ids',
'name' => $args['id'],
'include_children' => false,
'count' => false,
]
);
$id = ! empty( $menu->terms ) ? (int) $menu->terms[0] : null;
break;
case 'slug':
$menu = new \WP_Term_Query(
[
'taxonomy' => 'nav_menu',
'fields' => 'ids',
'slug' => $args['id'],
'include_children' => false,
'count' => false,
]
);
$id = ! empty( $menu->terms ) ? (int) $menu->terms[0] : null;
break;
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
}
$id = absint( $id_components['id'] );
break;
}
return ! empty( $id ) ? $context->get_loader( 'term' )->load_deferred( absint( $id ) ) : null;
},
],
'menuItem' => [
'type' => 'MenuItem',
'description' => __( 'A WordPress navigation menu item', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the menu item.', 'wp-graphql' ),
],
'idType' => [
'type' => 'MenuItemNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a menu item by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args, AppContext $context ) {
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
switch ( $id_type ) {
case 'database_id':
$id = absint( $args['id'] );
break;
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
}
$id = absint( $id_components['id'] );
break;
}
return $context->get_loader( 'post' )->load_deferred( absint( $id ) );
},
],
'plugin' => [
'type' => 'Plugin',
'description' => __( 'A WordPress plugin', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the plugin.', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args, AppContext $context ) {
$id_components = Relay::fromGlobalId( $args['id'] );
return ! empty( $id_components['id'] ) ? $context->get_loader( 'plugin' )->load_deferred( $id_components['id'] ) : null;
},
],
'termNode' => [
'type' => 'TermNode',
'description' => __( 'A node in a taxonomy used to group and relate content nodes', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'Unique identifier for the term node.', 'wp-graphql' ),
],
'idType' => [
'type' => 'TermNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a term node by. Default is Global ID', 'wp-graphql' ),
],
'taxonomy' => [
'type' => 'TaxonomyEnum',
'description' => __( 'The taxonomy of the tern node. Required when idType is set to "name" or "slug"', 'wp-graphql' ),
],
],
'resolve' => static function ( $root, $args, AppContext $context ) {
$idType = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
$term_id = null;
switch ( $idType ) {
case 'slug':
case 'name':
case 'database_id':
$taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : null;
if ( empty( $taxonomy ) && in_array(
$idType,
[
'name',
'slug',
],
true
) ) {
throw new UserError( esc_html__( 'When fetching a Term Node by "slug" or "name", the "taxonomy" also needs to be set as an input.', 'wp-graphql' ) );
}
if ( 'database_id' === $idType ) {
$term = get_term( absint( $args['id'] ) );
} else {
$term = get_term_by( $idType, $args['id'], $taxonomy );
}
$term_id = isset( $term->term_id ) ? absint( $term->term_id ) : null;
break;
case 'uri':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'nodeType' => 'TermNode',
]
);
case 'global_id':
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
}
$term_id = absint( $id_components['id'] );
break;
}
return ! empty( $term_id ) ? $context->get_loader( 'term' )->load_deferred( $term_id ) : null;
},
],
'theme' => [
'type' => 'Theme',
'description' => __( 'A Theme object', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the theme.', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args ) {
$id_components = Relay::fromGlobalId( $args['id'] );
return DataSource::resolve_theme( $id_components['id'] );
},
],
'user' => [
'type' => 'User',
'description' => __( 'Returns a user', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the user.', 'wp-graphql' ),
],
'idType' => [
'type' => 'UserNodeIdTypeEnum',
'description' => __( 'Type of unique identifier to fetch a user by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args, $context ) {
$idType = isset( $args['idType'] ) ? $args['idType'] : 'id';
switch ( $idType ) {
case 'database_id':
$id = absint( $args['id'] );
break;
case 'uri':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'nodeType' => 'User',
]
);
case 'login':
$current_user = wp_get_current_user();
if ( $current_user->user_login !== $args['id'] ) {
if ( ! current_user_can( 'list_users' ) ) {
throw new UserError( esc_html__( 'You do not have permission to request a User by Username', 'wp-graphql' ) );
}
}
$user = get_user_by( 'login', $args['id'] );
$id = isset( $user->ID ) ? $user->ID : null;
break;
case 'email':
$current_user = wp_get_current_user();
if ( $current_user->user_email !== $args['id'] ) {
if ( ! current_user_can( 'list_users' ) ) {
throw new UserError( esc_html__( 'You do not have permission to request a User by Email', 'wp-graphql' ) );
}
}
$user = get_user_by( 'email', $args['id'] );
$id = isset( $user->ID ) ? $user->ID : null;
break;
case 'slug':
$user = get_user_by( 'slug', $args['id'] );
$id = isset( $user->ID ) ? $user->ID : null;
break;
case 'id':
default:
$id_components = Relay::fromGlobalId( $args['id'] );
$id = absint( $id_components['id'] );
break;
}
return ! empty( $id ) ? $context->get_loader( 'user' )->load_deferred( $id ) : null;
},
],
'userRole' => [
'type' => 'UserRole',
'description' => __( 'Returns a user role', 'wp-graphql' ),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the user object.', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args ) {
$id_components = Relay::fromGlobalId( $args['id'] );
return DataSource::resolve_user_role( $id_components['id'] );
},
],
'viewer' => [
'type' => 'User',
'description' => __( 'Returns the current user', 'wp-graphql' ),
'resolve' => static function ( $source, array $args, AppContext $context ) {
return ! empty( $context->viewer->ID ) ? $context->get_loader( 'user' )->load_deferred( $context->viewer->ID ) : null;
},
],
],
]
);
}
/**
* Register RootQuery fields for Post Objects of supported post types
*
* @return void
*/
public static function register_post_object_fields() {
/** @var \WP_Post_Type[] */
$allowed_post_types = \WPGraphQL::get_allowed_post_types( 'objects', [ 'graphql_register_root_field' => true ] );
foreach ( $allowed_post_types as $post_type_object ) {
register_graphql_field(
'RootQuery',
$post_type_object->graphql_single_name,
[
'type' => $post_type_object->graphql_single_name,
'description' => sprintf(
// translators: %1$s is the post type GraphQL name, %2$s is the post type description
__( 'An object of the %1$s Type. %2$s', 'wp-graphql' ),
$post_type_object->graphql_single_name,
$post_type_object->description
),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the object.', 'wp-graphql' ),
],
'idType' => [
'type' => $post_type_object->graphql_single_name . 'IdType',
'description' => __( 'Type of unique identifier to fetch by. Default is Global ID', 'wp-graphql' ),
],
'asPreview' => [
'type' => 'Boolean',
'description' => __( 'Whether to return the Preview Node instead of the Published Node. When the ID of a Node is provided along with asPreview being set to true, the preview node with un-published changes will be returned instead of the published node. If no preview node exists or the requester doesn\'t have proper capabilities to preview, no node will be returned. If the ID provided is a URI and has a preview query arg, it will be used as a fallback if the "asPreview" argument is not explicitly provided as an argument.', 'wp-graphql' ),
],
],
'resolve' => static function ( $source, array $args, AppContext $context ) use ( $post_type_object ) {
$idType = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
$post_id = null;
switch ( $idType ) {
case 'slug':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'name' => $args['id'],
'post_type' => $post_type_object->name,
'nodeType' => 'ContentNode',
'asPreview' => $args['asPreview'] ?? null,
]
);
case 'uri':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'post_type' => $post_type_object->name,
'archive' => false,
'nodeType' => 'ContentNode',
'asPreview' => $args['asPreview'] ?? null,
]
);
case 'database_id':
$post_id = absint( $args['id'] );
break;
case 'source_url':
$url = $args['id'];
$post_id = attachment_url_to_postid( $url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid
if ( empty( $post_id ) ) {
return null;
}
$post_id = absint( attachment_url_to_postid( $url ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid
break;
case 'global_id':
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid. Make sure you set the proper idType for your input.', 'wp-graphql' ) );
}
$post_id = absint( $id_components['id'] );
break;
}
if ( isset( $args['asPreview'] ) && true === $args['asPreview'] ) {
$post_id = Utils::get_post_preview_id( $post_id );
}
return absint( $post_id ) ? $context->get_loader( 'post' )->load_deferred( $post_id )->then(
static function ( $post ) use ( $post_type_object ) {
// if the post isn't an instance of a Post model, return
if ( ! $post instanceof Post ) {
return null;
}
if ( ! isset( $post->post_type ) || ! in_array(
$post->post_type,
[
'revision',
$post_type_object->name,
],
true
) ) {
return null;
}
return $post;
}
) : null;
},
]
);
$post_by_args = [
'id' => [
'type' => 'ID',
'description' => sprintf(
// translators: %s is the post type's GraphQL name.
__( 'Get the %s object by its global ID', 'wp-graphql' ),
$post_type_object->graphql_single_name
),
],
$post_type_object->graphql_single_name . 'Id' => [
'type' => 'Int',
'description' => sprintf(
// translators: %s is the post type's GraphQL name.
__( 'Get the %s by its database ID', 'wp-graphql' ),
$post_type_object->graphql_single_name
),
],
'uri' => [
'type' => 'String',
'description' => sprintf(
// translators: %s is the post type's GraphQL name.
__( 'Get the %s by its uri', 'wp-graphql' ),
$post_type_object->graphql_single_name
),
],
];
if ( false === $post_type_object->hierarchical ) {
$post_by_args['slug'] = [
'type' => 'String',
'description' => sprintf(
// translators: %s is the post type's GraphQL name.
__( 'Get the %s by its slug (only available for non-hierarchical types)', 'wp-graphql' ),
$post_type_object->graphql_single_name
),
];
}
/**
* @deprecated Deprecated in favor of single node entry points
*/
register_graphql_field(
'RootQuery',
$post_type_object->graphql_single_name . 'By',
[
'type' => $post_type_object->graphql_single_name,
'deprecationReason' => __( 'Deprecated in favor of using the single entry point for this type with ID and IDType fields. For example, instead of postBy( id: "" ), use post(id: "" idType: "")', 'wp-graphql' ),
'description' => sprintf(
// translators: %s is the post type's GraphQL name.
__( 'A %s object', 'wp-graphql' ),
$post_type_object->graphql_single_name
),
'args' => $post_by_args,
'resolve' => static function ( $source, array $args, $context ) use ( $post_type_object ) {
$post_id = 0;
if ( ! empty( $args['id'] ) ) {
$id_components = Relay::fromGlobalId( $args['id'] );
if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
throw new UserError( esc_html__( 'The "id" is invalid', 'wp-graphql' ) );
}
$post_id = absint( $id_components['id'] );
} elseif ( ! empty( $args[ lcfirst( $post_type_object->graphql_single_name . 'Id' ) ] ) ) {
$id = $args[ lcfirst( $post_type_object->graphql_single_name . 'Id' ) ];
$post_id = absint( $id );
} elseif ( ! empty( $args['uri'] ) ) {
return $context->node_resolver->resolve_uri(
$args['uri'],
[
'post_type' => $post_type_object->name,
'archive' => false,
'nodeType' => 'ContentNode',
]
);
} elseif ( ! empty( $args['slug'] ) ) {
$slug = esc_html( $args['slug'] );
return $context->node_resolver->resolve_uri(
$slug,
[
'name' => $slug,
'post_type' => $post_type_object->name,
'nodeType' => 'ContentNode',
]
);
}
return $context->get_loader( 'post' )->load_deferred( $post_id )->then(
static function ( $post ) use ( $post_type_object ) {
// if the post type object isn't an instance of WP_Post_Type, return
if ( ! $post_type_object instanceof \WP_Post_Type ) {
return null;
}
// if the post isn't an instance of a Post model, return
if ( ! $post instanceof Post ) {
return null;
}
if ( ! isset( $post->post_type ) || ! in_array(
$post->post_type,
[
'revision',
$post_type_object->name,
],
true
) ) {
return null;
}
return $post;
}
);
},
]
);
}
}
/**
* Register RootQuery fields for Term Objects of supported taxonomies
*
* @return void
*/
public static function register_term_object_fields() {
/** @var \WP_Taxonomy[] $allowed_taxonomies */
$allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies( 'objects', [ 'graphql_register_root_field' => true ] );
foreach ( $allowed_taxonomies as $tax_object ) {
register_graphql_field(
'RootQuery',
$tax_object->graphql_single_name,
[
'type' => $tax_object->graphql_single_name,
'description' => sprintf(
// translators: %s is the taxonomys' GraphQL name.
__( 'A % object', 'wp-graphql' ),
$tax_object->graphql_single_name
),
'args' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'description' => __( 'The globally unique identifier of the object.', 'wp-graphql' ),
],
'idType' => [
'type' => $tax_object->graphql_single_name . 'IdType',
'description' => __( 'Type of unique identifier to fetch by. Default is Global ID', 'wp-graphql' ),
],
],
'resolve' => static function ( $_source, array $args, $context ) use ( $tax_object ) {
$idType = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
$term_id = null;
switch ( $idType ) {
case 'slug':
case 'name':
case 'database_id':
if ( 'database_id' === $idType ) {
$idType = 'id';
}
$term = get_term_by( $idType, $args['id'], $tax_object->name );
$term_id = isset( $term->term_id ) ? absint( $term->term_id ) : null;
break;
case 'uri':
return $context->node_resolver->resolve_uri(
$args['id'],
[
'nodeType' => 'TermNode',
'taxonomy' => $tax_object->name,
]
);
case 'global_id':
default:
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
}
$term_id = absint( $id_components['id'] );
break;
}
return ! empty( $term_id ) ? $context->get_loader( 'term' )->load_deferred( (int) $term_id ) : null;
},
]
);
}
}
}