-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphql_api.module
197 lines (176 loc) · 5.05 KB
/
graphql_api.module
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
<?php
use Drupal\graphql_api\Schema;
use GraphQL\GraphQL;
/**
* @file
*/
define('GRAPHQL_API_PATH', drupal_get_path('module', 'graphql_api'));
/**
* Implements hook_menu().
*
* @return array
*/
function graphql_api_menu() {
$items = [];
$items['graphql'] = [
'page callback' => 'graphql_api_page_callback',
'page arguments' => [],
'access arguments' => ['use graphql_api query'],
'access callback' => 'graphql_api_access_callback',
'file path' => GRAPHQL_API_PATH . '/includes',
'file' => 'graphql_api_page_callback.inc'
];
$items['graphql/graphiql'] = [
'title' => 'GraphiQL',
'page callback' => 'graphql_api_graphiql_callback',
'page arguments' => [],
'access arguments' => ['use graphql_api query'],
'file path' => GRAPHQL_API_PATH . '/includes',
'file' => 'graphql_api_graphiql_callback.inc'
];
$items['admin/config/services/graphql'] = [
'title' => 'GraphQL Config',
'page callback' => 'drupal_get_form',
'page arguments' => ['graphql_api_admin_config'],
'access arguments' => ['administer site configuration'],
'file path' => GRAPHQL_API_PATH . '/includes',
'file' => 'graphql_api.admin.inc',
];
$items['graphql/voyager'] = [
'title' => 'GraphQL Voyager',
'page callback' => 'graphql_api_voyager_callback',
'page arguments' => [],
'access arguments' => ['use graphql_api query'],
'file path' => GRAPHQL_API_PATH . '/includes',
'file' => 'graphql_api_voyager_callback.inc'
];
return $items;
}
function graphql_api_access_callback($perm) {
if (!empty($_SERVER['HTTP_GRAPHQL_TOKEN']) && $_SERVER['HTTP_GRAPHQL_TOKEN'] === variable_get('graphql_token', 'graphql_private_token')) {
return TRUE;
}
return user_access($perm);
}
/**
* Implements hook_permission().
*/
function graphql_api_permission() {
return array(
'use graphql_api query' => array(
'title' => t('Use GraphQL query'),
'description' => t('Perform query on graphql endpoint.'),
),
);
}
/**
* Get GraphQL info
*
* @return array|mixed
*/
function graphql_api_info() {
foreach (module_list() as $module => $module_info) {
module_load_include('inc', $module, $module.'.graphql');
}
$info = module_invoke_all('graphql_api_info');
drupal_alter('graphql_api_info', $info);
return $info;
}
/**
* Get entity field query
*
* @param $entity_type
* @param array $conditions
* @return \SelectQuery
*/
function graphql_api_entity_get_query($entity_type, $conditions = []) {
$info = entity_get_info($entity_type);
$base_table = $info['base table'];
$bundle_key = $info['entity keys']['bundle'];
$id_key = $info['entity keys']['id'];
$props = entity_get_property_info($entity_type);
if (!$info || !$props) {
return FALSE;
}
$all_fields = [];
// get all fields attached to this entity type
if (!empty($props['bundles'])) {
foreach ($props['bundles'] as $bundle => $bundle_info) {
foreach ($bundle_info['properties'] as $field => $field_info) {
$all_fields[] = $field;
}
}
}
$query = db_select($base_table, 'b')
->fields('b', [$id_key]);
$deleted_key = isset($info['entity keys']['deleted']) ? $info['entity keys']['deleted'] : null;
if ($deleted_key && !isset($conditions[$deleted_key])) {
$query->condition($deleted_key, 0);
}
$_f = 0;
foreach ($conditions as $prop => $val) {
if (in_array($prop, $info['schema_fields_sql']['base table'])) {
$query->condition($prop, $val);
}
else if (in_array($prop, $all_fields)) {
$f_alias = 'f'.$_f;
$query->leftJoin('field_data_' . $prop, $f_alias, "{$f_alias}.entity_type='{$entity_type}' AND {$f_alias}.entity_id=b.{$id_key} AND {$f_alias}.deleted=0");
if (is_array($val)) {
foreach ($val as $col => $f_val) {
$query->condition("{$f_alias}.{$prop}_{$col}", $f_val);
}
} else {
$prop_info = field_info_field($prop);
$columns = array_keys($prop_info['columns']);
$column = reset($columns);
$query->condition("{$f_alias}.{$prop}_{$column}", $val);
}
$_f++;
}
}
return $query;
}
/**
* Execute query in file
*/
function graphql_api_query_file($file, $args = [], $options = [], $is_mutation = false) {
$query_task = file_get_contents($file);
$schema = graphql_api();
$schema_build = $schema->build($options, $is_mutation);
$task_form_data = GraphQL::executeQuery(
$schema_build,
$query_task,
null,
null,
$args
);
return $task_form_data;
}
/**
* Get blacklisted entity type
*
* @return array
*/
function graphql_api_get_entity_types_blacklist() {
$whitelist = variable_get('graphql_entity_types', false);
$blacklist = [];
if (!$whitelist) return [];
foreach (entity_get_info() as $entity_type => $entity_info) {
if (!in_array($entity_type, $whitelist)) {
$blacklist[] = $entity_type;
}
}
return $blacklist;
}
/**
* Schema builder singleton
*
* @return array|\Drupal\graphql_api\Schema|mixed
*/
function graphql_api() {
$obj = &drupal_static(__FUNCTION__);
if (empty($obj)) {
$obj = new Schema();
}
return $obj;
}