1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\ConditionalOnEnvironment\Admin\Services\MenuPages; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\General\EndpointHelpers; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* GraphiQL page |
11
|
|
|
*/ |
12
|
|
|
class GraphiQLMenuPage extends AbstractMenuPage |
13
|
|
|
{ |
14
|
|
|
use EnqueueReactMenuPageTrait; |
15
|
|
|
use GraphQLAPIMenuPageTrait; |
16
|
|
|
|
17
|
|
|
public function print(): void |
18
|
|
|
{ |
19
|
|
|
?> |
20
|
|
|
<div id="graphiql" class="graphiql-client"> |
21
|
|
|
<p> |
22
|
|
|
<?php echo __('Loading...', 'graphql-api') ?> |
23
|
|
|
<!--span class="spinner is-active" style="float: none;"></span--> |
24
|
|
|
</p> |
25
|
|
|
</div> |
26
|
|
|
<?php |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Override, because this is the default page, so it is invoked |
31
|
|
|
* with the menu slug wp-admin/admin.php?page=graphql_api, |
32
|
|
|
* and not the menu page slug wp-admin/admin.php?page=graphql_api_graphiql |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getScreenID(): string |
37
|
|
|
{ |
38
|
|
|
return $this->getMenuName(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getMenuPageSlug(): string |
42
|
|
|
{ |
43
|
|
|
return 'graphiql'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Enqueue the required assets and initialize the localized scripts |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function enqueueGraphiQLClientAssets(): void |
52
|
|
|
{ |
53
|
|
|
\wp_enqueue_style( |
54
|
|
|
'graphql-api-graphiql-client', |
55
|
|
|
\GRAPHQL_API_URL . 'assets/css/graphiql-client.css', |
56
|
|
|
array(), |
57
|
|
|
\GRAPHQL_API_VERSION |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Enqueue the required assets and initialize the localized scripts |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
protected function enqueueGraphiQLCustomAssets(): void |
67
|
|
|
{ |
68
|
|
|
// Common settings to both clients (with/out Explorer) |
69
|
|
|
$scriptSettings = array( |
70
|
|
|
'nonce' => \wp_create_nonce('wp_rest'), |
71
|
|
|
'response' => $this->getResponse(), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
\wp_enqueue_style( |
75
|
|
|
'graphql-api-graphiql', |
76
|
|
|
\GRAPHQL_API_URL . 'assets/css/vendors/graphiql.min.css', |
77
|
|
|
array(), |
78
|
|
|
\GRAPHQL_API_VERSION |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
// JS: execute them all in the footer |
82
|
|
|
$this->enqueueReactAssets(true); |
83
|
|
|
|
84
|
|
|
\wp_enqueue_script( |
85
|
|
|
'graphql-api-graphiql', |
86
|
|
|
\GRAPHQL_API_URL . 'assets/js/vendors/graphiql.min.js', |
87
|
|
|
array('graphql-api-react-dom'), |
88
|
|
|
\GRAPHQL_API_VERSION, |
89
|
|
|
true |
90
|
|
|
); |
91
|
|
|
\wp_enqueue_script( |
92
|
|
|
'graphql-api-graphiql-client', |
93
|
|
|
\GRAPHQL_API_URL . 'assets/js/graphiql-client.js', |
94
|
|
|
array('graphql-api-graphiql'), |
95
|
|
|
\GRAPHQL_API_VERSION, |
96
|
|
|
true |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
// Load data into the script |
100
|
|
|
\wp_localize_script( |
101
|
|
|
'graphql-api-graphiql-client', |
102
|
|
|
'graphQLByPoPGraphiQLSettings', |
103
|
|
|
array_merge( |
104
|
|
|
[ |
105
|
|
|
'defaultQuery' => $this->getDefaultQuery(), |
106
|
|
|
'endpoint' => EndpointHelpers::getAdminGraphQLEndpoint(), |
107
|
|
|
], |
108
|
|
|
$scriptSettings |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Enqueue the required assets and initialize the localized scripts |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function enqueueAssets(): void |
119
|
|
|
{ |
120
|
|
|
parent::enqueueAssets(); |
121
|
|
|
|
122
|
|
|
$this->enqueueGraphiQLClientAssets(); |
123
|
|
|
$this->enqueueGraphiQLCustomAssets(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function getResponse(): string |
127
|
|
|
{ |
128
|
|
|
return ''; |
129
|
|
|
// return \__('Click the "Execute Query" button, or press Ctrl+Enter (Command+Enter in Mac)', 'graphql-api'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function getDefaultQuery(): string |
133
|
|
|
{ |
134
|
|
|
return <<<EOT |
135
|
|
|
# Welcome to GraphiQL |
136
|
|
|
# |
137
|
|
|
# GraphiQL is an in-browser tool for writing, validating, and |
138
|
|
|
# testing GraphQL queries. |
139
|
|
|
# |
140
|
|
|
# Type queries into this side of the screen, and you will see intelligent |
141
|
|
|
# typeaheads aware of the current GraphQL type schema and live syntax and |
142
|
|
|
# validation errors highlighted within the text. |
143
|
|
|
# |
144
|
|
|
# GraphQL queries typically start with a "{" character. Lines that starts |
145
|
|
|
# with a # are ignored. |
146
|
|
|
# |
147
|
|
|
# An example GraphQL query might look like: |
148
|
|
|
# |
149
|
|
|
# { |
150
|
|
|
# field(arg: "value") { |
151
|
|
|
# subField |
152
|
|
|
# } |
153
|
|
|
# } |
154
|
|
|
# |
155
|
|
|
# Run the query (at any moment): |
156
|
|
|
# |
157
|
|
|
# Ctrl-Enter (or press the play button above) |
158
|
|
|
# |
159
|
|
|
|
160
|
|
|
query { |
161
|
|
|
posts(limit:3) { |
162
|
|
|
id |
163
|
|
|
title |
164
|
|
|
date(format:"d/m/Y") |
165
|
|
|
url |
166
|
|
|
author { |
167
|
|
|
id |
168
|
|
|
name |
169
|
|
|
url |
170
|
|
|
} |
171
|
|
|
tags { |
172
|
|
|
name |
173
|
|
|
} |
174
|
|
|
featuredImage { |
175
|
|
|
src |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
EOT; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|