forked from wp-graphql/wp-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-functions.php
342 lines (304 loc) · 10.8 KB
/
access-functions.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* This file contains access functions for various class methods
*
* @since 0.0.2
*/
/**
* Formats the name of a field so that it plays nice with GraphiQL
*
* @param string $field_name Name of the field
*
* @return string Name of the field
* @since 0.0.2
*/
function graphql_format_field_name( $field_name ) {
$field_name = preg_replace( '/[^A-Za-z0-9]/i', ' ', $field_name );
$field_name = preg_replace( '/[^A-Za-z0-9]/i', '', ucwords( $field_name ) );
$field_name = lcfirst( $field_name );
return $field_name;
}
/**
* Formats the name of a Type so that it plays nice with GraphiQL
*
* @param string $type_name Name of the field
*
* @return string Name of the field
* @since 0.0.2
*/
function graphql_format_type_name( $type_name ) {
$type_name = preg_replace( '/[^A-Za-z0-9]/i', ' ', $type_name );
$type_name = preg_replace( '/[^A-Za-z0-9]/i', '', ucwords( $type_name ) );
$type_name = ucfirst( $type_name );
return $type_name;
}
/**
* Provides a simple way to run a GraphQL query with out posting a request to the endpoint.
*
* @param array $request_data The GraphQL request data (query, variables, operation_name).
*
* @return array
* @since 0.2.0
* @throws Exception
*/
function graphql( $request_data = [] ) {
$request = new \WPGraphQL\Request( $request_data );
return $request->execute();
}
/**
* Previous access function for running GraphQL queries directly. This function will
* eventually be deprecated in favor of `graphql`.
*
* @param string $query The GraphQL query to run
* @param string $operation_name The name of the operation
* @param array $variables Variables to be passed to your GraphQL request
*
* @return array
* @since 0.0.2
* @throws \Exception
*/
function do_graphql_request( $query, $operation_name = '', $variables = [] ) {
return graphql( [
'query' => $query,
'variables' => $variables,
'operation_name' => $operation_name,
] );
}
/**
* Determine when to register types
*
* @return string
*/
function get_graphql_register_action() {
$action = 'graphql_register_types_late';
if ( ! did_action( 'graphql_register_initial_types' ) ) {
$action = 'graphql_register_initial_types';
} elseif ( ! did_action( 'graphql_register_types' ) ) {
$action = 'graphql_register_types';
}
return $action;
}
/**
* Given a type name and interface name, this applies the interface to the Type.
*
* Should be used at the `graphql_register_types` hook.
*
* @param array $interface_names Array of one or more names of the GraphQL Interfaces to apply to
* the GraphQL Types
* @param array $type_names Array of one or more names of the GraphQL Types to apply the
* interfaces to
*
* example:
* The following would register the "MyNewInterface" interface to the Post and Page type in the
* Schema.
*
* register_graphql_interfaces_to_types( [ 'MyNewInterface' ], [ 'Post', 'Page' ] );
*/
function register_graphql_interfaces_to_types( $interface_names, $type_names ) {
if ( is_string( $type_names ) ) {
$type_names = [ $type_names ];
}
if ( is_string( $interface_names ) ) {
$interface_names[] = $interface_names;
}
if ( ! empty( $type_names ) && is_array( $type_names ) && ! empty( $interface_names ) && is_array( $interface_names ) ) {
foreach ( $type_names as $type_name ) {
// Filter the GraphQL Object Type Interface to apply the interface
add_filter( 'graphql_object_type_interfaces', function( $interfaces, $config ) use ( $type_name, $interface_names ) {
$interfaces = is_array( $interfaces ) ? $interfaces : [];
if ( strtolower( $type_name ) === strtolower( $config['name'] ) ) {
$interfaces = array_unique( array_merge( $interfaces, $interface_names ) );
}
return $interfaces;
}, 10, 2 );
}
}
}
/**
* Given a Type Name and a $config array, this adds a Type to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_type( $type_name, $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $config ) {
$type_registry->register_type( $type_name, $config );
}, 10 );
}
/**
* Given a Type Name and a $config array, this adds an Interface Type to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_interface_type( $type_name, $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $config ) {
$type_registry->register_interface_type( $type_name, $config );
}, 10 );
}
/**
* Given a Type Name and a $config array, this adds an ObjectType to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_object_type( $type_name, $config ) {
$config['kind'] = 'object';
register_graphql_type( $type_name, $config );
}
/**
* Given a Type Name and a $config array, this adds an InputType to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_input_type( $type_name, $config ) {
$config['kind'] = 'input';
register_graphql_type( $type_name, $config );
}
/**
* Given a Type Name and a $config array, this adds an UnionType to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_union_type( $type_name, $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $config ) {
$config['kind'] = 'union';
$type_registry->register_type( $type_name, $config );
}, 10 );
}
/**
* Given a Type Name and a $config array, this adds an EnumType to the TypeRegistry
*
* @param string $type_name The name of the Type to register
* @param array $config The Type config
*/
function register_graphql_enum_type( $type_name, $config ) {
$config['kind'] = 'enum';
register_graphql_type( $type_name, $config );
}
/**
* Given a Type Name, Field Name, and a $config array, this adds a Field to a registered Type in
* the TypeRegistry
*
* @param string $type_name The name of the Type to add the field to
* @param string $field_name The name of the Field to add to the Type
* @param array $config The Type config
*/
function register_graphql_field( $type_name, $field_name, $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $field_name, $config ) {
$type_registry->register_field( $type_name, $field_name, $config );
}, 10 );
}
/**
* Given a Type Name and an array of field configs, this adds the fields to the registered type in
* the TypeRegistry
*
* @param string $type_name The name of the Type to add the fields to
* @param array $fields An array of field configs
*/
function register_graphql_fields( $type_name, array $fields ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $fields ) {
$type_registry->register_fields( $type_name, $fields );
}, 10 );
}
/**
* Given a config array for a connection, this registers a connection by creating all appropriate
* fields and types for the connection
*
* @param array $config Array to configure the connection
*/
function register_graphql_connection( array $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $config ) {
$type_registry->register_connection( $config );
}, 10 );
}
/**
* Given a config array for a custom Scalar, this registers a Scalar for use in the Schema
*
* @param string $type_name The name of the Type to register
* @param array $config The config for the scalar type to register
*/
function register_graphql_scalar( $type_name, array $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $config ) {
$type_registry->register_scalar( $type_name, $config );
}, 10 );
}
/**
* Given a Type Name and Field Name, this removes the field from the TypeRegistry
*
* @param string $type_name The name of the Type to remove the field from
* @param string $field_name The name of the field to remove
*/
function deregister_graphql_field( $type_name, $field_name ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $type_name, $field_name ) {
$type_registry->deregister_field( $type_name, $field_name );
}, 10 );
}
/**
* Given a Mutation Name and Config array, this adds a Mutation to the Schema
*
* @param string $mutation_name The name of the Mutation to register
* @param array $config The config for the mutation
*/
function register_graphql_mutation( $mutation_name, $config ) {
add_action( get_graphql_register_action(), function( \WPGraphQL\Registry\TypeRegistry $type_registry ) use ( $mutation_name, $config ) {
$type_registry->register_mutation( $mutation_name, $config );
}, 10 );
}
/**
* Whether a GraphQL request is in action or not. This is determined by the WPGraphQL Request
* class being initiated. True while a request is in action, false after a request completes.
*
* This should be used when a condition needs to be checked for ALL GraphQL requests, such
* as filtering WP_Query for GraphQL requests, for example.
*
* Default false.
*
* @since 0.4.1
* @return bool
*/
function is_graphql_request() {
return WPGraphQL::is_graphql_request();
}
/**
* Whether a GraphQL HTTP request is in action or not. This is determined by
* checking if the request is occurring on the route defined for the GraphQL endpoint.
*
* This conditional should only be used for features that apply to HTTP requests. If you are going
* to apply filters to underlying WordPress core functionality that should affect _all_ GraphQL
* requests, you should use "is_graphql_request" but if you need to apply filters only if the
* GraphQL request is an HTTP request, use this conditional.
*
* Default false.
*
* @since 0.4.1
* @return bool
*/
function is_graphql_http_request() {
return \WPGraphQL\Router::is_graphql_http_request();
}
/**
* Polyfill for PHP versions below 7.3
*
* @return mixed|string|int
*/
if ( ! function_exists( 'array_key_first' ) ) {
function array_key_first( array $array ) {
foreach ( $array as $key => $value ) {
return $key;
}
}
}
/**
* Polyfill for PHP versions below 7.3
*
* @return mixed|string|int
*/
if ( ! function_exists( 'array_key_last' ) ) {
function array_key_last( array $array ) {
end( $array );
return key( $array );
}
}