-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql_api.module
More file actions
157 lines (139 loc) · 3.67 KB
/
graphql_api.module
File metadata and controls
157 lines (139 loc) · 3.67 KB
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
<?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['graphqleditor'] = [
'title' => 'GraphiQL',
'page callback' => 'graphql_api_graphqleditor_callback',
'page arguments' => [],
'access arguments' => ['use graphql_api query'],
'file path' => GRAPHQL_API_PATH . '/includes',
'file' => 'graphql_api_graphqleditor_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.'),
),
'use graphql_api introspection' => array(
'title' => t('Browse GraphQL schema'),
'description' => t('Perform introspection 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 bool|\EntityFieldQuery
*/
function graphql_api_entity_get_query($entity_type, $conditions = []) {
$info = entity_get_info($entity_type);
$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 = new EntityFieldQuery();
$deleted_key = isset($info['entity keys']['deleted']) ? $info['entity keys']['deleted'] : null;
if ($deleted_key && !isset($conditions[$deleted_key])) {
$query->propertyCondition($deleted_key, 0);
}
$query->entityCondition('entity_type', $entity_type);
foreach ($conditions as $prop => $val) {
if (in_array($prop, $info['schema_fields_sql']['base table'])) {
$query->propertyCondition($prop, $val);
}
else if (in_array($prop, $all_fields)) {
foreach ($val as $col => $f_val) {
$query->fieldCondition($prop, $col, $f_val);
}
}
}
return $query;
}
/**
* Execute query in file
*/
function graphql_api_query_file($file, $args = []) {
$query_task = file_get_contents($file);
$shema = graphql_api();
$schema_build = $shema->build();
$data = GraphQL::execute(
$schema_build,
$query_task,
null,
null,
$args
);
return $data;
}
/**
* Symfony VarDumper fallback
*/
if (!function_exists('dump')) {
function dump() {
return call_user_func_array('var_dump', func_get_args());
}
}
/**
* Get Schema builder
*/
function graphql_api() {
if (!$obj = &drupal_static(__FUNCTION__)) {
$obj = new Schema;
}
return $obj;
}