Skip to content

Commit 266b9bb

Browse files
committed
Import internal code:
- Add GraphiQL - GraphQL IDE at /graphqleditor - Fix hook_graphql_api_info (view example at graphql_api.graphql.inc) - Keep field name intact (prev version field name is shortened) - Interface type will include all field of object type
1 parent 602ce41 commit 266b9bb

19 files changed

+58349
-134
lines changed

LICENSE.txt

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
}
88
],
99
"require": {
10-
"webonyx/graphql-php": "dev-master"
10+
"webonyx/graphql-php": "dev-master",
11+
"youshido/graphql": "^1.2"
1112
}
1213
}

css/graphqleditor.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#graphqleditor {
2+
top: 0;
3+
left: 0;
4+
position: fixed;
5+
bottom: 0;
6+
right: 0;
7+
}

graphql_api.info

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ dependencies[] = entity
55
dependencies[] = xautoload
66
dependencies[] = composer_manager
77

8-
files[] = tests/graphql_api.test
8+
files[] = tests/graphql_api.test
9+
; Information added by Drupal.org packaging script on 2016-11-22
10+
version = "7.x-1.x-dev"
11+
core = "7.x"
12+
project = "graphql_api"
13+
datestamp = "1479787191"
14+

graphql_api.module

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
use Drupal\graphql_api\Schema;
3+
use GraphQL\GraphQL;
4+
25
/**
36
* @file
47
*/
@@ -16,13 +19,30 @@ function graphql_api_menu() {
1619
'page callback' => 'graphql_api_page_callback',
1720
'page arguments' => [],
1821
'access arguments' => ['use graphql_api query'],
22+
'access callback' => 'graphql_api_access_callback',
1923
'file path' => GRAPHQL_API_PATH . '/includes',
2024
'file' => 'graphql_api_page_callback.inc'
2125
];
2226

27+
$items['graphqleditor'] = [
28+
'title' => 'GraphiQL',
29+
'page callback' => 'graphql_api_graphqleditor_callback',
30+
'page arguments' => [],
31+
'access arguments' => ['use graphql_api query'],
32+
'file path' => GRAPHQL_API_PATH . '/includes',
33+
'file' => 'graphql_api_graphqleditor_callback.inc'
34+
];
35+
2336
return $items;
2437
}
2538

39+
function graphql_api_access_callback($perm) {
40+
if (!empty($_SERVER['HTTP_GRAPHQL_TOKEN']) && $_SERVER['HTTP_GRAPHQL_TOKEN'] === variable_get('graphql_token', 'graphql_private_token')) {
41+
return TRUE;
42+
}
43+
return user_access($perm);
44+
}
45+
2646
/**
2747
* Implements hook_permission().
2848
*/
@@ -41,16 +61,10 @@ function graphql_api_permission() {
4161
* @return array|mixed
4262
*/
4363
function graphql_api_info() {
44-
$info = [
45-
'types' => []
46-
];
4764
foreach (module_list() as $module => $module_info) {
48-
if (module_load_include('inc', $module, $module.'.graphql')) {
49-
foreach (module_implements('graphql_api_info') as $module) {
50-
$info = array_merge_recursive(module_invoke($module, 'graphql_api_info'), $info);
51-
}
52-
}
65+
module_load_include('inc', $module, $module.'.graphql');
5366
}
67+
$info = module_invoke_all('graphql_api_info');
5468
drupal_alter('graphql_api_info', $info);
5569
return $info;
5670
}
@@ -81,6 +95,12 @@ function graphql_api_entity_get_query($entity_type, $conditions = []) {
8195
}
8296

8397
$query = new EntityFieldQuery();
98+
99+
$deleted_key = isset($info['entity keys']['deleted']) ? $info['entity keys']['deleted'] : null;
100+
if ($deleted_key && !isset($conditions[$deleted_key])) {
101+
$query->propertyCondition($deleted_key, 0);
102+
}
103+
84104
$query->entityCondition('entity_type', $entity_type);
85105
foreach ($conditions as $prop => $val) {
86106
if (in_array($prop, $info['schema_fields_sql']['base table'])) {
@@ -94,4 +114,30 @@ function graphql_api_entity_get_query($entity_type, $conditions = []) {
94114
}
95115

96116
return $query;
117+
}
118+
119+
/**
120+
* Execute query in file
121+
*/
122+
function graphql_api_query_file($file, $args = []) {
123+
$query_task = file_get_contents($file);
124+
$shema = new Schema();
125+
$schema_build = $shema->build();
126+
$task_form_data = GraphQL::execute(
127+
$schema_build,
128+
$query_task,
129+
null,
130+
null,
131+
$args
132+
);
133+
return $task_form_data;
134+
}
135+
136+
/**
137+
* Symfony VarDumper fallback
138+
*/
139+
if (!function_exists('dump')) {
140+
function dump() {
141+
return call_user_func_array('var_dump', func_get_args());
142+
}
97143
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
function graphql_api_graphqleditor_callback() {
3+
drupal_add_js(drupal_get_path('module', 'graphql_api') . '/js/dist/main.bundle.js');
4+
return '<div id="graphqleditor"></div>';
5+
}

includes/graphql_api_page_callback.inc

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use GraphQL\GraphQL;
66
* Page callback for /graphql
77
*/
88
function graphql_api_page_callback() {
9+
$is_introspec = FALSE;
910
$variables = [];
1011
if (isset($_SERVER['CONTENT_TYPE']) && strpos(strtolower($_SERVER['CONTENT_TYPE']), 'application/json') > -1) {
1112
$rawBody = file_get_contents('php://input');
@@ -17,31 +18,40 @@ function graphql_api_page_callback() {
1718
if (!empty($data)) {
1819
if (empty($data['variables'])) {
1920
$data['variables'] = [];
20-
} else {
21-
$data['variables'] = drupal_json_decode($data['variables']);
2221
}
2322
}
2423

2524
if (empty($data['query'])) {
2625
$data['query'] = file_get_contents(__DIR__ . '/../tests/fixtures/instrospec.graphql');
26+
$is_introspec = TRUE;
2727
}
2828

2929
$requestString = isset($data['query']) ? $data['query'] : null;
3030
$operationName = isset($data['operation']) ? $data['operation'] : null;
3131
$variableValues = isset($data['variables']) ? $data['variables'] : null;
3232

33+
$is_introspec = FALSE;
3334
try {
34-
// Define your schema:
35-
$schemaBuilder = new Schema;
36-
$schema = $schemaBuilder->build();
37-
$result = GraphQL::execute(
38-
$schema,
39-
$requestString,
40-
/* $rootValue */ null,
41-
/* $context */ null, // A custom context that can be used to pass current User object etc to all resolvers.
42-
$variableValues,
43-
$operationName
44-
);
35+
if ($is_introspec && $cache = cache_get('graphql_api_introspec')) {
36+
$result = $cache->data;
37+
} else {
38+
// Define your schema:
39+
$schemaBuilder = new Schema;
40+
$schema = $schemaBuilder->build();
41+
42+
$result = GraphQL::execute(
43+
$schema,
44+
$requestString,
45+
/* $rootValue */ null,
46+
/* $context */ null, // A custom context that can be used to pass current User object etc to all resolvers.
47+
$variableValues,
48+
$operationName
49+
);
50+
51+
if ($is_introspec) {
52+
cache_set('graphql_api_introspec', $result);
53+
}
54+
}
4555
} catch (Exception $exception) {
4656
$result = [
4757
'errors' => [

0 commit comments

Comments
 (0)