Skip to content

Commit ea5a8fb

Browse files
committed
GraphiQL client
1 parent 5f6fcb2 commit ea5a8fb

File tree

2 files changed

+83
-31
lines changed

2 files changed

+83
-31
lines changed

graphiql.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<html>
2+
<head>
3+
<title>GraphiQL-WP</title>
4+
<link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
5+
</head>
6+
<body style="margin: 0;">
7+
<div id="graphiql" style="height: 100vh;"></div>
8+
9+
<script
10+
crossorigin
11+
src="https://unpkg.com/react/umd/react.production.min.js"
12+
></script>
13+
<script
14+
crossorigin
15+
src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"
16+
></script>
17+
<script
18+
crossorigin
19+
src="https://unpkg.com/graphiql/graphiql.min.js"
20+
></script>
21+
22+
<script>
23+
const graphQLFetcher = (graphQLParams) =>
24+
fetch("/graphql", {
25+
method: "post",
26+
headers: { "Content-Type": "application/json" },
27+
body: JSON.stringify(graphQLParams),
28+
})
29+
.then((response) => response.json())
30+
.catch(() => response.text());
31+
ReactDOM.render(
32+
React.createElement(GraphiQL, { fetcher: graphQLFetcher }),
33+
document.getElementById("graphiql")
34+
);
35+
</script>
36+
</body>
37+
</html>

index.php

+46-31
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
<?php
2+
23
/**
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+
1113
namespace Mohiohio\GraphQLWP;
1214

1315
use GraphQL\GraphQL;
1416
use Mohiohio\WordPress\Router;
1517

1618
const ENDPOINT = '/graphql/';
1719

18-
if(file_exists(__DIR__.'/vendor')) {
20+
if (file_exists(__DIR__ . '/vendor')) {
1921
// echo 'autoloading vendor';
20-
require __DIR__.'/vendor/autoload.php';
22+
require __DIR__ . '/vendor/autoload.php';
2123
}
2224

2325
Router::routes([
2426

25-
ENDPOINT => function() {
27+
ENDPOINT => function () {
2628

2729
header('Access-Control-Allow-Origin: *');
2830
header('Access-Control-Allow-Headers: content-type');
@@ -35,9 +37,9 @@
3537
$rawBody = file_get_contents('php://input');
3638

3739
try {
38-
$data = json_decode($rawBody, true);
40+
$data = json_decode($rawBody, true);
3941
} 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.']]);
4143
}
4244

4345
// Decoded response is still empty
@@ -51,21 +53,23 @@
5153
$requestString = isset($data['query']) ? $data['query'] : null;
5254
$operationName = isset($data['operation']) ? $data['operation'] : null;
5355
$variableValues = isset($data['variables']) ?
54-
( is_array($data['variables']) ?
56+
(is_array($data['variables']) ?
5557
$data['variables'] :
56-
json_decode($data['variables'],true) ) :
58+
json_decode($data['variables'], true)) :
5759
null;
5860

59-
if($requestString) {
61+
if ($requestString) {
6062
try {
6163
do_action('graphql-wp/before-execute', $requestString);
6264
// Define your schema:
6365
$schema = Schema::build();
6466
$result = GraphQL::execute(
6567
$schema,
6668
$requestString,
67-
/* $rootValue */ null,
68-
/* $contextValue */ null,
69+
/* $rootValue */
70+
null,
71+
/* $contextValue */
72+
null,
6973
$variableValues,
7074
$operationName
7175
);
@@ -81,43 +85,54 @@
8185
jsonResponse($result);
8286
}
8387
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+
}
8497
}
98+
8599
]);
86100

87101
/**
88102
* Sends a json object to the client
89103
* @param array $resp response object
90104
* @return [type] [description]
91105
*/
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;
101116
}
102117

103118
/**
104119
* Log a message to the SAPi (terminal) (only when WP_DEBUG is set to true)
105120
* @param string $message The message to log to terminal
106121
* @return [type] [description]
107122
*/
108-
function log($message) {
123+
function log($message)
124+
{
109125
if (!WP_DEBUG) {
110-
return;
126+
return;
111127
}
112128
$function_args = func_get_args();
113129
// The first is a simple string message, the others should be var_exported
114130
array_shift($function_args);
115131

116-
foreach($function_args as $argument) {
132+
foreach ($function_args as $argument) {
117133
$message .= ' ' . var_export($argument, true);
118134
}
119135

120136
// send to sapi
121137
error_log($message, 4);
122-
123138
}

0 commit comments

Comments
 (0)