forked from wp-graphql/wp-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-graphql.php
executable file
·844 lines (725 loc) · 25.2 KB
/
wp-graphql.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
<?php
/**
* Plugin Name: WP GraphQL
* Plugin URI: https://github.com/wp-graphql/wp-graphql
* Description: GraphQL API for WordPress
* Author: WPGraphQL
* Author URI: http://www.wpgraphql.com
* Version: 0.0.34
* Text Domain: wp-graphql
* Domain Path: /languages/
* Requires at least: 4.7.0
* Tested up to: 4.7.1
* Requires PHP: 5.5
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*
*
* @package WPGraphQL
* @category Core
* @author WPGraphQL
* @version 0.0.34
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* If the codeception remote coverage file exists, require it.
*
* This file should only exist locally or when CI bootstraps the environment for testing
*/
if ( file_exists( __DIR__ . '/c3.php' ) ) {
require_once( __DIR__ . '/c3.php' );
}
/**
* This plugin brings the power of GraphQL (http://graphql.org/) to WordPress.
*
* This plugin is based on the hard work of Jason Bahl, Ryan Kanner, Hughie Devore and Peter Pak of
* Digital First Media
* (https://github.com/dfmedia), and Edwin Cromley of BE-Webdesign
* (https://github.com/BE-Webdesign).
*
* The plugin is built on top of the graphql-php library by Webonyx
* (https://github.com/webonyx/graphql-php) and makes use of the graphql-relay-php library by Ivome
* (https://github.com/ivome/graphql-relay-php/)
*
* Special thanks to Digital First Media (http://digitalfirstmedia.com) for allocating development
* resources to push the project forward.
*
* Some of the concepts and code are based on the WordPress Rest API.
* Much love to the folks (https://github.com/orgs/WP-API/people) that put their blood, sweat and
* tears into the WP-API project, as it's been huge in moving WordPress forward as a platform and
* helped inspire and direct the development of WPGraphQL.
*
* Much love to Facebook® for open sourcing the GraphQL spec (https://facebook.github.io/graphql/)
* and maintaining the JS reference implementation (https://github.com/graphql/graphql-js)
*
* Much love to Apollo (Meteor Development Group) for their work on driving GraphQL forward and
* providing a lot of insight into how to design GraphQL schemas, etc. Check them out:
* http://www.apollodata.com/
*/
if ( ! class_exists( 'WPGraphQL' ) ) :
/**
* This is the one true WPGraphQL class
*/
final class WPGraphQL {
/**
* Stores the instance of the WPGraphQL class
*
* @var WPGraphQL The one true WPGraphQL
* @since 0.0.1
* @access private
*/
private static $instance;
/**
* Holds the Schema def
*
* @var \WPGraphQL\WPSchema
*/
protected static $schema;
/**
* Stores an array of allowed post types
*
* @var array allowed_post_types
* @since 0.0.5
* @access public
*/
public static $allowed_post_types;
/**
* Stores an array of allowed taxonomies
*
* @var array allowed_taxonomies
* @since 0.0.5
* @access public
*/
public static $allowed_taxonomies;
/**
* The instance of the WPGraphQL object
*
* @return object|WPGraphQL - The one true WPGraphQL
* @since 0.0.1
* @access public
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPGraphQL ) ) {
self::$instance = new WPGraphQL;
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->actions();
self::$instance->filters();
}
/**
* Return the WPGraphQL Instance
*/
return self::$instance;
}
/**
* Throw error on object clone.
* The whole idea of the singleton design pattern is that there is a single object
* therefore, we don't want the object to be cloned.
*
* @since 0.0.1
* @access public
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'The WPGraphQL class should not be cloned.', 'wp-graphql' ), '0.0.1' );
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1
* @access protected
* @return void
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'De-serializing instances of the WPGraphQL class is not allowed', 'wp-graphql' ), '0.0.1' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 0.0.1
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_VERSION' ) ) {
define( 'WPGRAPHQL_VERSION', '0.0.34' );
}
// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_PLUGIN_FILE', __FILE__ );
}
// Whether to autoload the files or not
if ( ! defined( 'WPGRAPHQL_AUTOLOAD' ) ) {
define( 'WPGRAPHQL_AUTOLOAD', true );
}
// Whether to run the plugin in debug mode. Default is false.
if ( ! defined( 'GRAPHQL_DEBUG' ) ) {
define( 'GRAPHQL_DEBUG', false );
}
}
/**
* Include required files.
* Uses composer's autoload
*
* @access private
* @since 0.0.1
* @return void
*/
private function includes() {
/**
* WPGRAPHQL_AUTOLOAD can be set to "false" to prevent the autoloader from running.
* In most cases, this is not something that should be disabled, but some environments
* may bootstrap their dependencies in a global autoloader that will autoload files
* before we get to this point, and requiring the autoloader again can trigger fatal errors.
*
* The codeception tests are an example of an environment where adding the autoloader again causes issues
* so this is set to false for tests.
*/
if ( defined( 'WPGRAPHQL_AUTOLOAD' ) && true === WPGRAPHQL_AUTOLOAD ) {
// Autoload Required Classes
require_once( WPGRAPHQL_PLUGIN_DIR . 'vendor/autoload.php' );
}
// Required non-autoloaded classes
require_once( WPGRAPHQL_PLUGIN_DIR . 'access-functions.php' );
}
/**
* Sets up actions to run at certain spots throughout WordPress and the WPGraphQL execution cycle
*/
private function actions() {
/**
* Init WPGraphQL after themes have been setup,
* allowing for both plugins and themes to register
* things before graphql_init
*/
add_action( 'after_setup_theme', function () {
new \WPGraphQL\Data\Config();
new \WPGraphQL\Router();
/**
* Fire off init action
*
* @param WPGraphQL $instance The instance of the WPGraphQL class
*/
do_action( 'graphql_init', self::$instance );
} );
/**
* Flush permalinks if the registered GraphQL endpoint has not yet been registered.
*/
add_action( 'wp_loaded', [ $this, 'maybe_flush_permalinks' ] );
/**
* Hook in before fields resolve to check field permissions
*/
add_action( 'graphql_before_resolve_field', [
'\WPGraphQL\Utils\InstrumentSchema',
'check_field_permissions'
], 10, 8 );
/**
* Determine what to show in graphql
*/
add_action( 'do_graphql_request', 'register_initial_settings', 10 );
add_action( 'init_graphql_request', [ $this, 'setup_types' ], 10 );
}
/**
* Determine the post_types and taxonomies, etc that should show in GraphQL
*/
public function setup_types() {
/**
* Only do this if we're in the context of a GraphQL request (note, this doesn't
* mean just an HTTP request, but even an internal request via "do_graphql_request")
*/
if ( defined( 'GRAPHQL_REQUEST' ) && true === GRAPHQL_REQUEST ) {
/**
* Setup the settings, post_types and taxonomies to show_in_graphql
*/
\WPGraphQL::show_in_graphql();
\WPGraphQL::get_allowed_post_types();
\WPGraphQL::get_allowed_taxonomies();
}
}
/**
* Flush permalinks if the GraphQL Endpoint route isn't yet registered
*/
public function maybe_flush_permalinks() {
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules[ \WPGraphQL\Router::$route . '/?$' ] ) ) {
flush_rewrite_rules();
}
}
/**
* Setup filters
*/
private function filters() {
/**
* mediaItems are the attachment postObject, but they have a different schema shape
* than postObjects out of the box, so this filter adjusts the core mediaItem
* shape of data
*/
add_filter( 'graphql_mediaItem_fields', [
'\WPGraphQL\Type\MediaItem\MediaItemType',
'fields'
], 10, 1 );
/**
* Instrument the Schema to provide Resolve Hooks and sanitize Schema output
*/
add_filter( 'graphql_schema', [
'\WPGraphQL\Utils\InstrumentSchema',
'instrument_schema'
], 10, 1 );
}
/**
* Function to execute when the user activates the plugin.
*
* @since 0.0.17
*/
public function activate() {
flush_rewrite_rules();
// Save the version of the plugin as an option in order to force actions
// on upgrade.
update_option( 'wp_graphql_version', WPGRAPHQL_VERSION, 'no' );
}
/**
* Function to execute when the user deactivates the plugin.
*
* @since 0.0.17
*/
public function deactivate() {
flush_rewrite_rules();
delete_option( 'wp_graphql_version' );
}
/**
* This sets up built-in post_types and taxonomies to show in the GraphQL Schema
*
* @since 0.0.2
* @access public
* @return void
*/
public static function show_in_graphql() {
global $wp_post_types, $wp_taxonomies;
// Adds GraphQL support for attachments
if ( isset( $wp_post_types['attachment'] ) ) {
$wp_post_types['attachment']->show_in_graphql = true;
$wp_post_types['attachment']->graphql_single_name = 'mediaItem';
$wp_post_types['attachment']->graphql_plural_name = 'mediaItems';
}
// Adds GraphQL support for pages
if ( isset( $wp_post_types['page'] ) ) {
$wp_post_types['page']->show_in_graphql = true;
$wp_post_types['page']->graphql_single_name = 'page';
$wp_post_types['page']->graphql_plural_name = 'pages';
}
// Adds GraphQL support for posts
if ( isset( $wp_post_types['post'] ) ) {
$wp_post_types['post']->show_in_graphql = true;
$wp_post_types['post']->graphql_single_name = 'post';
$wp_post_types['post']->graphql_plural_name = 'posts';
}
// Adds GraphQL support for categories
if ( isset( $wp_taxonomies['category'] ) ) {
$wp_taxonomies['category']->show_in_graphql = true;
$wp_taxonomies['category']->graphql_single_name = 'category';
$wp_taxonomies['category']->graphql_plural_name = 'categories';
}
// Adds GraphQL support for tags
if ( isset( $wp_taxonomies['post_tag'] ) ) {
$wp_taxonomies['post_tag']->show_in_graphql = true;
$wp_taxonomies['post_tag']->graphql_single_name = 'tag';
$wp_taxonomies['post_tag']->graphql_plural_name = 'tags';
}
}
/**
* Get the post types that are allowed to be used in GraphQL. This gets all post_types that
* are set to show_in_graphql, but allows for external code (plugins/theme) to filter the
* list of allowed_post_types to add/remove additional post_types
*
* @return array
* @since 0.0.4
* @access public
*/
public static function get_allowed_post_types() {
/**
* Get all post_types
*/
$post_types = get_post_types( [
'show_in_graphql' => true,
] );
/**
* Validate that the post_types have a graphql_single_name and graphql_plural_name
*/
array_map( function ( $post_type ) {
$post_type_object = get_post_type_object( $post_type );
if ( empty( $post_type_object->graphql_single_name ) || empty( $post_type_object->graphql_plural_name ) ) {
throw new \GraphQL\Error\UserError( sprintf( __( 'The %s post_type isn\'t configured properly to show in GraphQL. It needs a "graphql_single_name" and a "graphql_plural_name"', 'wp-graphql' ), $post_type_object->name ) );
}
}, $post_types );
/**
* Define the $allowed_post_types to be exposed by GraphQL Queries Pass through a filter
* to allow the post_types to be modified (for example if a certain post_type should
* not be exposed to the GraphQL API)
*
* @since 0.0.2
*
* @param array $post_types Array of post types
*
* @return array
*/
self::$allowed_post_types = apply_filters( 'graphql_post_entities_allowed_post_types', $post_types );
/**
* Returns the array of allowed_post_types
*/
return self::$allowed_post_types;
}
/**
* Get the taxonomies that are allowed to be used in GraphQL/This gets all taxonomies that
* are set to "show_in_graphql" but allows for external code (plugins/themes) to filter
* the list of allowed_taxonomies to add/remove additional taxonomies
*
* @since 0.0.4
* @access public
* @return array
*/
public static function get_allowed_taxonomies() {
/**
* Get all taxonomies
*/
$taxonomies = get_taxonomies( [
'show_in_graphql' => true,
] );
/**
* Validate that the taxonomies have a graphql_single_name and graphql_plural_name
*/
array_map( function ( $taxonomy ) {
$tax_object = get_taxonomy( $taxonomy );
if ( empty( $tax_object->graphql_single_name ) || empty( $tax_object->graphql_plural_name ) ) {
throw new \GraphQL\Error\UserError( sprintf( __( 'The %s taxonomy isn\'t configured properly to show in GraphQL. It needs a "graphql_single_name" and a "graphql_plural_name"', 'wp-graphql' ), $tax_object->name ) );
}
}, $taxonomies );
/**
* Define the $allowed_taxonomies to be exposed by GraphQL Queries Pass through a filter
* to allow the taxonomies to be modified (for example if a certain taxonomy should not
* be exposed to the GraphQL API)
*
* @since 0.0.2
* @return array
*
* @param array $taxonomies Array of taxonomy objects
*/
self::$allowed_taxonomies = apply_filters( 'graphql_term_entities_allowed_taxonomies', $taxonomies );
/**
* Returns the array of $allowed_taxonomies
*/
return self::$allowed_taxonomies;
}
/**
* Returns the Schema as defined by static registrations throughout
* the WP Load.
*
* @access protected
* @return \WPGraphQL\WPSchema
*/
public static function get_schema() {
/**
* Fire an action when the Schema is returned
*/
do_action( 'graphql_get_schema', self::$schema );
if ( null === self::$schema ) {
/**
* Create an executable Schema from the registered
* root_Query and root_mutation
*/
$executable_schema = [
'query' => \WPGraphQL\Types::root_query(),
'mutation' => \WPGraphQL\Types::root_mutation(),
];
/**
* Generate the Schema
*/
$schema = new \WPGraphQL\WPSchema( $executable_schema );
/**
* Generate & Filter the schema.
*
* @since 0.0.5
*
* @param array $schema The executable Schema that GraphQL executes against
* @param \WPGraphQL\AppContext $app_context Object The AppContext object containing all of the
* information about the context we know at this point
*/
self::$schema = apply_filters( 'graphql_schema', $schema, self::get_app_context() );
}
/**
* Return the Schema after applying filters
*/
return ! empty( self::$schema ) ? self::$schema : null;
}
/**
* Return the static schema if there is one
*
* @return null|string
* @access public
*/
public static function get_static_schema() {
$schema = null;
if ( file_exists( WPGRAPHQL_PLUGIN_DIR . 'schema.graphql' ) && ! empty( file_get_contents( WPGRAPHQL_PLUGIN_DIR . 'schema.graphql' ) ) ) {
$schema = file_get_contents( WPGRAPHQL_PLUGIN_DIR . 'schema.graphql' );
}
return $schema;
}
/**
* Get the AppContext for use in passing down the Resolve Tree
*
* @return \WPGraphQL\AppContext
* @access public
*/
public static function get_app_context() {
/**
* Configure the app_context which gets passed down to all the resolvers.
*
* @since 0.0.4
*/
$app_context = new \WPGraphQL\AppContext();
$app_context->viewer = wp_get_current_user();
$app_context->root_url = get_bloginfo( 'url' );
$app_context->request = ! empty( $_REQUEST ) ? $_REQUEST : null;
return $app_context;
}
/**
* Initialize the GraphQL Request.
*
* This defines that the Request is a GraphQL Request and fires off the
* `init_graphql_request` hook which is a great place for plugins to hook
* in and modify things that should only occur in the context
* of a GraphQL Request.
*/
protected static function init_graphql_request() {
/**
* Whether it's a GraphQL Request (http or internal)
*
* @since 0.0.5
*/
if ( ! defined( 'GRAPHQL_REQUEST' ) ) {
define( 'GRAPHQL_REQUEST', true );
}
/**
* Action – intentionally with no context – to indicate a GraphQL Request has started
*/
do_action( 'init_graphql_request' );
}
/**
* This processes a GraphQL request, given a $request and optional $variables
*
* This function is used to resolve the HTTP requests for the GraphQL API, but can also be
* used internally to run GraphQL queries inside WordPress via PHP.
*
* @since 0.0.5
*
* @param string $request The GraphQL request to be run
* @param string $operation_name The name of the operation
* @param string $variables Variables to be passed to your GraphQL request
*
* @return array $result The results of your request
*/
public static function do_graphql_request( $request, $operation_name = '', $variables = '' ) {
/**
* Initialize the GraphQL Request
*/
self::init_graphql_request();
/**
* Store the global post so it can be reset after GraphQL execution
*
* This allows for a GraphQL query to be used in the middle of post content, such as in a Shortcode
* without disrupting the flow of the post as the global POST before and after GraphQL execution will be
* the same.
*/
$global_post = ! empty( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
/**
* Run an action as soon when do_graphql_request begins.
*
* @param string $request The GraphQL request to be run
* @param string $operation_name The name of the operation
* @param string $variables Variables to be passed to your GraphQL request
*/
do_action( 'do_graphql_request', $request, $operation_name, $variables );
if ( ! is_array( $variables ) ) {
$variables = (string) $variables;
$variables = (array) json_decode( $variables );
}
/**
* Executes the request and captures the result
*/
$result = \GraphQL\GraphQL::executeAndReturnResult(
self::get_schema(),
$request,
null,
self::get_app_context(),
$variables,
$operation_name
);
/**
* Run an action. This is a good place for debug tools to hook in to log things, etc.
*
* @since 0.0.4
*
* @param array $result The result of your GraphQL request
* @param \WPGraphQL\WPSchema $schema The schema object for the root request
* @param string $operation_name The name of the operation
* @param string $request The request that GraphQL executed
* @param array|null $variables Variables to passed to your GraphQL query
*/
do_action( 'graphql_execute', $result, self::get_schema(), $operation_name, $request, $variables );
/**
* Filter the $result of the GraphQL execution. This allows for the response to be filtered before
* it's returned, allowing granular control over the response at the latest point.
*
* POSSIBLE USAGE EXAMPLES:
* This could be used to ensure that certain fields never make it to the response if they match
* certain criteria, etc. For example, this filter could be used to check if a current user is
* allowed to see certain things, and if they are not, the $result could be filtered to remove
* the data they should not be allowed to see.
*
* Or, perhaps some systems want the result to always include some additional piece of data in
* every response, regardless of the request that was sent to it, this could allow for that
* to be hooked in and included in the $result
*
* @since 0.0.5
*
* @param array $result The result of your GraphQL query
* @param \WPGraphQL\WPSchema $schema The schema object for the root query
* @param string $operation_name The name of the operation
* @param string $request The request that GraphQL executed
* @param array|null $variables Variables to passed to your GraphQL request
*/
$filtered_result = apply_filters( 'graphql_request_results', $result, self::get_schema(), $operation_name, $request, $variables );
/**
* Run an action after the result has been filtered, as the response is being returned.
* This is a good place for debug tools to hook in to log things, etc.
*
* @param array $filtered_result The filtered_result of the GraphQL request
* @param array $result The result of your GraphQL request
* @param \WPGraphQL\WPSchema $schema The schema object for the root request
* @param string $operation_name The name of the operation
* @param string $request The request that GraphQL executed
* @param array|null $variables Variables to passed to your GraphQL query
*/
do_action( 'graphql_return_response', $filtered_result, $result, self::get_schema(), $operation_name, $request, $variables );
/**
* Reset the global post after execution
*
* This allows for a GraphQL query to be used in the middle of post content, such as in a Shortcode
* without disrupting the flow of the post as the global POST before and after GraphQL execution will be
* the same.
*/
if ( ! empty( $global_post ) ) {
$GLOBALS['post'] = $global_post;
}
/**
* Return the result of the request
*/
return $result->toArray( GRAPHQL_DEBUG );
}
/**
* @param null $request
*
* @return \GraphQL\Server\StandardServer
* @throws \GraphQL\Server\RequestError
*/
public static function server( $request = null ) {
/**
* Initialize the GraphQL Request
*/
self::init_graphql_request();
/**
* Store the global post so it can be reset after GraphQL execution
*
* This allows for a GraphQL query to be used in the middle of post content, such as in a Shortcode
* without disrupting the flow of the post as the global POST before and after GraphQL execution will be
* the same.
*/
$global_post = ! empty( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
/**
* Run an action as soon when do_graphql_request begins.
*/
$helper = new \GraphQL\Server\Helper();
$parsed_request = $helper->parseHttpRequest();
/**
* If the request is a batch request it will come back as an array
*/
if ( is_array( $parsed_request ) ) {
/**
* Loop through the requests in the batch
*/
foreach ( $parsed_request as $request ) {
$query = $request->query;
$operation = $request->operation;
$variables = $request->variables;
/**
* Run an action as soon when do_graphql_request begins.
*
* @param string $request The GraphQL request to be run
* @param string $operation_name The name of the operation
* @param string $variables Variables to be passed to your GraphQL request
*/
do_action( 'do_graphql_request', $query, $operation, $variables );
}
} else {
$query = isset( $parsed_request->query ) ? $parsed_request->query : '';
$operation = isset( $parsed_request->operation ) ? $parsed_request->operation : '';
$variables = isset( $parsed_request->variables ) ? $parsed_request->variables : '';
/**
* Run an action as soon when do_graphql_request begins.
*
* @param string $request The GraphQL request to be run
* @param string $operation_name The name of the operation
* @param string $variables Variables to be passed to your GraphQL request
*/
do_action( 'do_graphql_request', $query, $operation, $variables );
}
$config = new \GraphQL\Server\ServerConfig();
$config
->setDebug( GRAPHQL_DEBUG )
->setSchema( self::get_schema() )
->setContext( self::get_app_context() )
->setQueryBatching( true );
$server = new \GraphQL\Server\StandardServer( $config );
/**
* Reset the global post after execution
*
* This allows for a GraphQL query to be used in the middle of post content, such as in a Shortcode
* without disrupting the flow of the post as the global POST before and after GraphQL execution will be
* the same.
*/
if ( ! empty( $global_post ) ) {
$GLOBALS['post'] = $global_post;
}
return $server;
}
}
endif;
if ( ! function_exists( 'graphql_init' ) ) {
/**
* Function that instantiates the plugins main class
*
* @since 0.0.1
*/
function graphql_init() {
/**
* Return an instance of the action
*/
return \WPGraphQL::instance();
}
}
graphql_init();
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once( 'cli/wp-cli.php' );
}