|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | /**
|
3 |
| -* Plugin Name: WordPress GraphQL |
4 |
| -* Plugin URI: http://www.mohiohio.com/ |
5 |
| -* Description: GraphQL for WordPress |
6 |
| -* Version: 0.3.0 |
7 |
| -* Author: Tim Field |
8 |
| -* Author URI: http://www.mohiohio.com/ |
9 |
| -* License: GPL-3 |
10 |
| -*/ |
| 4 | + * Plugin Name: WordPress GraphQL |
| 5 | + * Plugin URI: http://www.mohiohio.com/ |
| 6 | + * Description: GraphQL for WordPress |
| 7 | + * Version: 0.3.0 |
| 8 | + * Author: Tim Field |
| 9 | + * Author URI: http://www.mohiohio.com/ |
| 10 | + * License: GPL-3 |
| 11 | + */ |
| 12 | + |
11 | 13 | namespace Mohiohio\GraphQLWP;
|
12 | 14 |
|
13 | 15 | use GraphQL\GraphQL;
|
14 | 16 | use Mohiohio\WordPress\Router;
|
15 | 17 |
|
16 | 18 | const ENDPOINT = '/graphql/';
|
17 | 19 |
|
18 |
| -if(file_exists(__DIR__.'/vendor')) { |
| 20 | +if (file_exists(__DIR__ . '/vendor')) { |
19 | 21 | // echo 'autoloading vendor';
|
20 |
| - require __DIR__.'/vendor/autoload.php'; |
| 22 | + require __DIR__ . '/vendor/autoload.php'; |
21 | 23 | }
|
22 | 24 |
|
23 | 25 | Router::routes([
|
24 | 26 |
|
25 |
| - ENDPOINT => function() { |
| 27 | + ENDPOINT => function () { |
26 | 28 |
|
27 | 29 | header('Access-Control-Allow-Origin: *');
|
28 | 30 | header('Access-Control-Allow-Headers: content-type');
|
|
35 | 37 | $rawBody = file_get_contents('php://input');
|
36 | 38 |
|
37 | 39 | try {
|
38 |
| - $data = json_decode($rawBody, true); |
| 40 | + $data = json_decode($rawBody, true); |
39 | 41 | } catch (\Exception $exception) {
|
40 |
| - jsonResponse(['errors' => ['message' => 'Decoding body failed. Be sure to send valid json request.']]); |
| 42 | + jsonResponse(['errors' => ['message' => 'Decoding body failed. Be sure to send valid json request.']]); |
41 | 43 | }
|
42 | 44 |
|
43 | 45 | // Decoded response is still empty
|
|
51 | 53 | $requestString = isset($data['query']) ? $data['query'] : null;
|
52 | 54 | $operationName = isset($data['operation']) ? $data['operation'] : null;
|
53 | 55 | $variableValues = isset($data['variables']) ?
|
54 |
| - ( is_array($data['variables']) ? |
| 56 | + (is_array($data['variables']) ? |
55 | 57 | $data['variables'] :
|
56 |
| - json_decode($data['variables'],true) ) : |
| 58 | + json_decode($data['variables'], true)) : |
57 | 59 | null;
|
58 | 60 |
|
59 |
| - if($requestString) { |
| 61 | + if ($requestString) { |
60 | 62 | try {
|
61 | 63 | do_action('graphql-wp/before-execute', $requestString);
|
62 | 64 | // Define your schema:
|
63 | 65 | $schema = Schema::build();
|
64 | 66 | $result = GraphQL::execute(
|
65 | 67 | $schema,
|
66 | 68 | $requestString,
|
67 |
| - /* $rootValue */ null, |
68 |
| - /* $contextValue */ null, |
| 69 | + /* $rootValue */ |
| 70 | + null, |
| 71 | + /* $contextValue */ |
| 72 | + null, |
69 | 73 | $variableValues,
|
70 | 74 | $operationName
|
71 | 75 | );
|
|
81 | 85 | jsonResponse($result);
|
82 | 86 | }
|
83 | 87 | 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']]);
|
| 88 | + }, |
| 89 | + |
| 90 | + '/graphiql/' => function () { |
| 91 | + // todo check login level |
| 92 | + if (current_user_can('administrator')) { |
| 93 | + include __DIR__ . '/graphiql.html'; |
| 94 | + } else { |
| 95 | + header("HTTP/1.1 401 Unauthorized"); |
| 96 | + } |
84 | 97 | }
|
| 98 | + |
85 | 99 | ]);
|
86 | 100 |
|
87 | 101 | /**
|
88 | 102 | * Sends a json object to the client
|
89 | 103 | * @param array $resp response object
|
90 | 104 | * @return [type] [description]
|
91 | 105 | */
|
92 |
| -function jsonResponse(array $resp) { |
93 |
| - try { |
94 |
| - $jsonResponse = json_encode($resp); |
95 |
| - } catch(\Exception $exception) { |
96 |
| - jsonResponse(['errors' => ['message' => 'Failed to encode to JSON the response.']]); |
97 |
| - } |
98 |
| - |
99 |
| - echo $jsonResponse; |
100 |
| - exit; |
| 106 | +function jsonResponse(array $resp) |
| 107 | +{ |
| 108 | + try { |
| 109 | + $jsonResponse = json_encode($resp); |
| 110 | + } catch (\Exception $exception) { |
| 111 | + jsonResponse(['errors' => ['message' => 'Failed to encode to JSON the response.']]); |
| 112 | + } |
| 113 | + |
| 114 | + echo $jsonResponse; |
| 115 | + exit; |
101 | 116 | }
|
102 | 117 |
|
103 | 118 | /**
|
104 | 119 | * Log a message to the SAPi (terminal) (only when WP_DEBUG is set to true)
|
105 | 120 | * @param string $message The message to log to terminal
|
106 | 121 | * @return [type] [description]
|
107 | 122 | */
|
108 |
| -function log($message) { |
| 123 | +function log($message) |
| 124 | +{ |
109 | 125 | if (!WP_DEBUG) {
|
110 |
| - return; |
| 126 | + return; |
111 | 127 | }
|
112 | 128 | $function_args = func_get_args();
|
113 | 129 | // The first is a simple string message, the others should be var_exported
|
114 | 130 | array_shift($function_args);
|
115 | 131 |
|
116 |
| - foreach($function_args as $argument) { |
| 132 | + foreach ($function_args as $argument) { |
117 | 133 | $message .= ' ' . var_export($argument, true);
|
118 | 134 | }
|
119 | 135 |
|
120 | 136 | // send to sapi
|
121 | 137 | error_log($message, 4);
|
122 |
| - |
123 | 138 | }
|
0 commit comments