1
1
<?php
2
- use Drupal \graphql_api \Schema ;
3
2
use GraphQL \GraphQL ;
3
+ use GraphQL \Utils \SchemaPrinter ;
4
4
5
5
/**
6
6
* Page callback for /graphql
7
7
*/
8
8
function graphql_api_page_callback () {
9
+ $ _startTime = microtime (TRUE );
9
10
$ is_introspec = FALSE ;
11
+ $ is_batch = FALSE ;
10
12
$ variables = [];
11
13
if (isset ($ _SERVER ['CONTENT_TYPE ' ]) && strpos (strtolower ($ _SERVER ['CONTENT_TYPE ' ]), 'application/json ' ) > -1 ) {
12
14
$ rawBody = file_get_contents ('php://input ' );
@@ -16,52 +18,93 @@ function graphql_api_page_callback() {
16
18
}
17
19
18
20
$ requestString = isset ($ data ['query ' ]) ? $ data ['query ' ] : null ;
19
- $ operationName = isset ($ data ['operation ' ]) ? $ data ['operation ' ] : null ;
21
+ $ operationName = isset ($ data ['operationName ' ]) ? $ data ['operationName ' ] : null ;
20
22
$ variableValues = isset ($ data ['variables ' ]) ? $ data ['variables ' ] : [];
23
+ $ is_mutation = true ;
24
+ if ($ operationName === 'Introspection ' ) {
25
+ $ operationName = null ;
26
+ }
21
27
22
28
try {
23
- if ( empty ( $ requestString )) {
24
- if ( user_access ( ' use graphql_api introspection ' ) ) {
25
- $ requestString = file_get_contents ( __DIR__ . ' /../tests/fixtures/instrospec.graphql ' );
26
- $ is_introspec = TRUE ;
29
+ $ result = [];
30
+ $ is_batched = false ;
31
+ // Define your schema:
32
+ $ _startBuildTime = microtime ( true ) ;
27
33
28
- } else {
29
- throw new Exception (t ("User without permission `use graphql_api introspection` cannot browse the schema " ));
34
+ $ typeLoader = function ($ typeName ) use ($ is_mutation ) {
35
+ $ schemaBuilder = graphql_api ();
36
+ $ type = $ schemaBuilder ->buildType ($ typeName , $ is_mutation );
37
+ if ($ type ) {
38
+ return $ type ;
30
39
}
31
- }
40
+ return null ;
41
+ };
42
+ $ schemaBuilder = graphql_api ();
43
+ $ schema = $ schemaBuilder ->build ([
44
+ 'typeLoader ' => $ typeLoader
45
+ ], $ is_mutation );
32
46
33
- $ is_introspec = FALSE ; // @FIXME
34
- if ($ is_introspec && $ cache = cache_get ('graphql_api_introspec ' ) && empty ($ _GET ['nocache ' ])) {
35
- $ result = $ cache ->data ;
36
- } else {
37
- // Define your schema:
38
- $ schemaBuilder = graphql_api ();
39
- $ schema = $ schemaBuilder ->build ();
47
+ $ _endBuildTime = microtime (true ) - $ _startBuildTime ;
40
48
41
- $ result = GraphQL::execute (
49
+ $ _startQueryTime = microtime (true );
50
+
51
+ // execute single query
52
+ if ($ requestString ) {
53
+ $ result = GraphQL::executeQuery (
42
54
$ schema ,
43
55
$ requestString ,
44
56
/* $rootValue */ null ,
45
57
/* $context */ null , // A custom context that can be used to pass current User object etc to all resolvers.
46
58
$ variableValues ,
47
59
$ operationName
48
- );
49
-
50
- if (!empty ($ schemaBuilder ->errors )) {
51
- $ result ['schema_errors ' ] = $ schemaBuilder ->errors ;
60
+ )->toArray (true );
61
+ } else if (count ($ data ) > 0 && !empty ($ data [0 ]['query ' ]) && !empty ($ data [0 ]['operationName ' ])) {
62
+ $ is_batched = true ;
63
+ foreach ($ data as $ index => $ batch_query ) {
64
+ $ batch_query ['variables ' ] = isset ($ batch_query ['variables ' ]) ? $ batch_query ['variables ' ] : [];
65
+ $ batch_result = GraphQL::executeQuery (
66
+ $ schema ,
67
+ $ batch_query ['query ' ],
68
+ /* $rootValue */ null ,
69
+ /* $context */ null , // A custom context that can be used to pass current User object etc to all resolvers.
70
+ $ batch_query ['variables ' ],
71
+ $ batch_query ['operationName ' ]
72
+ );
73
+ $ result [$ index ] = $ batch_result ->toArray (true );
52
74
}
75
+ }
53
76
54
- if ($ is_introspec ) {
55
- cache_set ('graphql_api_introspec ' , $ result );
56
- }
77
+ $ _endQueryTime = microtime (true ) - $ _startQueryTime ;
78
+
79
+ $ debug = [
80
+ 'schemaBuildTime ' => $ _endBuildTime ,
81
+ 'queryTime ' => $ _endQueryTime ,
82
+ 'totalTime ' => microtime (true ) - $ _startTime ,
83
+ ] + $ schemaBuilder ->getMetrics ();
84
+
85
+ if (!$ is_batched ) {
86
+ $ result ['debug ' ] = $ debug ;
57
87
}
88
+
89
+ if (variable_get ('graphql_debug ' )) {
90
+ watchdog ('GraphQL ' , 'Execute query ' . json_encode ($ data , JSON_PRETTY_PRINT ) . "\nDebug: " . json_encode ($ debug , JSON_PRETTY_PRINT ));
91
+ }
92
+
93
+ drupal_json_output ($ result );
94
+ exit ;
58
95
} catch (Exception $ exception ) {
96
+ dump ($ exception );
97
+ die ();
59
98
$ result = [
60
99
'errors ' => [
61
100
['message ' => $ exception ->getMessage (), 'backtrace ' => debug_backtrace ()]
62
101
]
63
102
];
64
103
}
104
+ if (!empty ($ _GET ['raw ' ])) {
105
+ echo $ result ;
106
+ exit ;
107
+ }
65
108
66
109
drupal_json_output ($ result );
67
110
exit ;
0 commit comments