forked from tim-field/graphql-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
119 lines (100 loc) · 3.71 KB
/
index.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
<?php
/**
* Plugin Name: WordPress GraphQL
* Plugin URI: http://www.mohiohio.com/
* Description: GraphQL for WordPress
* Version: 0.3.0
* Author: Tim Field
* Author URI: http://www.mohiohio.com/
* License: GPL-3
*/
namespace Mohiohio\GraphQLWP;
require_once __DIR__."/../../../vendor/autoload.php";
use GraphQL\GraphQL;
use Mohiohio\WordPress\Router;
const ENDPOINT = '/graphql/';
Router::routes([
ENDPOINT => function() {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: content-type');
header('Content-Type: application/json');
$contentTypeIsJson = (isset($_SERVER['HTTP_CONTENT_TYPE']) && $_SERVER['HTTP_CONTENT_TYPE'] == 'application/json')
|| (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] == 'application/json');
log('contentTypeIsJson', $contentTypeIsJson);
if ($contentTypeIsJson) {
$rawBody = file_get_contents('php://input');
try {
$data = json_decode($rawBody, true);
} catch (\Exception $exception) {
jsonResponse(['errors' => ['message' => 'Decoding body failed. Be sure to send valid json request.']]);
}
// Decoded response is still empty
if (strlen($rawBody) > 0 && null === $data) {
jsonResponse(['errors' => ['message' => 'Decoding body failed. Be sure to send valid json request. Check for line feeds in json (replace them with "\n" or remove them)']]);
}
} else {
$data = $_POST;
}
$requestString = isset($data['query']) ? $data['query'] : null;
$operationName = isset($data['operation']) ? $data['operation'] : null;
$variableValues = isset($data['variables']) ?
( is_array($data['variables']) ?
$data['variables'] :
json_decode($data['variables'],true) ) :
null;
if($requestString) {
try {
// Define your schema:
$schema = Schema::build();
$result = GraphQL::execute(
$schema,
$requestString,
/* $rootValue */ null,
$variableValues,
$operationName
);
} catch (\Exception $exception) {
$result = [
'errors' => [
['message' => $exception->getMessage()]
]
];
}
//log('result', $result);
jsonResponse($result);
}
jsonResponse(['errors' => ['message' => 'Wrong query format or empty query. Either send raw query _with_ Content-Type: \'application/json\' header or send query by posting www-form-data with a query="query{}..." parameter']]);
}
]);
/**
* Sends a json object to the client
* @param array $resp response object
* @return [type] [description]
*/
function jsonResponse(array $resp) {
try {
$jsonResponse = json_encode($resp);
} catch(\Exception $exception) {
jsonResponse(['errors' => ['message' => 'Failed to encode to JSON the response.']]);
}
echo $jsonResponse;
exit;
}
/**
* Log a message to the SAPi (terminal) (only when WP_DEBUG is set to true)
* @param string $message The message to log to terminal
* @return [type] [description]
*/
function log($message) {
if (!WP_DEBUG) {
return;
}
$function_args = func_get_args();
// The first is a simple string message, the others should be var_exported
array_shift($function_args);
foreach($function_args as $argument) {
$message .= ' ' . var_export($argument, true);
}
// send to sapi
error_log($message, 4);
}