Skip to content

Commit 08318f2

Browse files
committed
Merge internal code
1 parent 79f4de1 commit 08318f2

File tree

2 files changed

+1422
-320
lines changed

2 files changed

+1422
-320
lines changed

includes/graphql_api_page_callback.inc

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2-
use Drupal\graphql_api\Schema;
32
use GraphQL\GraphQL;
3+
use GraphQL\Utils\SchemaPrinter;
44

55
/**
66
* Page callback for /graphql
77
*/
88
function graphql_api_page_callback() {
9+
$_startTime = microtime(TRUE);
910
$is_introspec = FALSE;
11+
$is_batch = FALSE;
1012
$variables = [];
1113
if (isset($_SERVER['CONTENT_TYPE']) && strpos(strtolower($_SERVER['CONTENT_TYPE']), 'application/json') > -1) {
1214
$rawBody = file_get_contents('php://input');
@@ -16,52 +18,93 @@ function graphql_api_page_callback() {
1618
}
1719

1820
$requestString = isset($data['query']) ? $data['query'] : null;
19-
$operationName = isset($data['operation']) ? $data['operation'] : null;
21+
$operationName = isset($data['operationName']) ? $data['operationName'] : null;
2022
$variableValues = isset($data['variables']) ? $data['variables'] : [];
23+
$is_mutation = true;
24+
if ($operationName === 'Introspection') {
25+
$operationName = null;
26+
}
2127

2228
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);
2733

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;
3039
}
31-
}
40+
return null;
41+
};
42+
$schemaBuilder = graphql_api();
43+
$schema = $schemaBuilder->build([
44+
'typeLoader' => $typeLoader
45+
], $is_mutation);
3246

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;
4048

41-
$result = GraphQL::execute(
49+
$_startQueryTime = microtime(true);
50+
51+
// execute single query
52+
if ($requestString) {
53+
$result = GraphQL::executeQuery(
4254
$schema,
4355
$requestString,
4456
/* $rootValue */ null,
4557
/* $context */ null, // A custom context that can be used to pass current User object etc to all resolvers.
4658
$variableValues,
4759
$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);
5274
}
75+
}
5376

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;
5787
}
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;
5895
} catch (Exception $exception) {
96+
dump($exception);
97+
die();
5998
$result = [
6099
'errors' => [
61100
['message' => $exception->getMessage(), 'backtrace' => debug_backtrace()]
62101
]
63102
];
64103
}
104+
if (!empty($_GET['raw'])) {
105+
echo $result;
106+
exit;
107+
}
65108

66109
drupal_json_output($result);
67110
exit;

0 commit comments

Comments
 (0)