1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Admin\MenuPages; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\General\EndpointHelpers; |
8
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
9
|
|
|
use GraphQLAPI\GraphQLAPI\Admin\MenuPages\AbstractMenuPage; |
10
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
11
|
|
|
use GraphQLAPI\GraphQLAPI\Clients\AdminGraphiQLWithExplorerClient; |
12
|
|
|
use GraphQLAPI\GraphQLAPI\Admin\MenuPages\EnqueueReactMenuPageTrait; |
13
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ClientFunctionalityModuleResolver; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* GraphiQL page |
17
|
|
|
*/ |
18
|
|
|
class GraphiQLMenuPage extends AbstractMenuPage |
19
|
|
|
{ |
20
|
|
|
use EnqueueReactMenuPageTrait; |
21
|
|
|
use GraphQLAPIMenuPageTrait; |
22
|
|
|
|
23
|
|
|
protected function useGraphiQLExplorer(): bool |
24
|
|
|
{ |
25
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
26
|
|
|
return $moduleRegistry->isModuleEnabled(ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getGraphiQLWithExplorerClientHTML(): string |
30
|
|
|
{ |
31
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
32
|
|
|
/** |
33
|
|
|
* @var AdminGraphiQLWithExplorerClient |
34
|
|
|
*/ |
35
|
|
|
$client = $instanceManager->getInstance(AdminGraphiQLWithExplorerClient::class); |
36
|
|
|
return $client->getClientHTML(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function print(): void |
40
|
|
|
{ |
41
|
|
|
if (!$this->useGraphiQLExplorer()) { |
42
|
|
|
?> |
43
|
|
|
<div id="graphiql" class="graphiql-client"> |
44
|
|
|
<p> |
45
|
|
|
<?php echo __('Loading...', 'graphql-api') ?> |
46
|
|
|
<!--span class="spinner is-active" style="float: none;"></span--> |
47
|
|
|
</p> |
48
|
|
|
</div> |
49
|
|
|
<?php |
50
|
|
|
} else { |
51
|
|
|
$htmlContent = $this->getGraphiQLWithExplorerClientHTML(); |
52
|
|
|
// Extract the HTML inside <body> |
53
|
|
|
$matches = []; |
54
|
|
|
preg_match('/<body>(.*?)<\/body>/', $htmlContent, $matches); |
55
|
|
|
echo $matches[1]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Override, because this is the default page, so it is invoked |
61
|
|
|
* with the menu slug wp-admin/admin.php?page=graphql_api, |
62
|
|
|
* and not the menu page slug wp-admin/admin.php?page=graphql_api_graphiql |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getScreenID(): string |
67
|
|
|
{ |
68
|
|
|
return $this->getMenuName(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getMenuPageSlug(): string |
72
|
|
|
{ |
73
|
|
|
return 'graphiql'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Enqueue the required assets and initialize the localized scripts |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function enqueueAssets(): void |
82
|
|
|
{ |
83
|
|
|
parent::enqueueAssets(); |
84
|
|
|
|
85
|
|
|
$useGraphiQLExplorer = $this->useGraphiQLExplorer(); |
86
|
|
|
|
87
|
|
|
// CSS |
88
|
|
|
\wp_enqueue_style( |
89
|
|
|
'graphql-api-graphiql-client', |
90
|
|
|
\GRAPHQL_API_URL . 'assets/css/graphiql-client.css', |
91
|
|
|
array(), |
92
|
|
|
\GRAPHQL_API_VERSION |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
// JS: execute them all in the footer |
96
|
|
|
$this->enqueueReactAssets(true); |
97
|
|
|
|
98
|
|
|
if (!$useGraphiQLExplorer) { |
99
|
|
|
\wp_enqueue_style( |
100
|
|
|
'graphql-api-graphiql', |
101
|
|
|
\GRAPHQL_API_URL . 'assets/css/vendors/graphiql.min.css', |
102
|
|
|
array(), |
103
|
|
|
\GRAPHQL_API_VERSION |
104
|
|
|
); |
105
|
|
|
\wp_enqueue_script( |
106
|
|
|
'graphql-api-graphiql', |
107
|
|
|
\GRAPHQL_API_URL . 'assets/js/vendors/graphiql.min.js', |
108
|
|
|
array('graphql-api-react-dom'), |
109
|
|
|
\GRAPHQL_API_VERSION, |
110
|
|
|
true |
111
|
|
|
); |
112
|
|
|
\wp_enqueue_script( |
113
|
|
|
'graphql-api-graphiql-client', |
114
|
|
|
\GRAPHQL_API_URL . 'assets/js/graphiql-client.js', |
115
|
|
|
array('graphql-api-graphiql'), |
116
|
|
|
\GRAPHQL_API_VERSION, |
117
|
|
|
true |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
// Load data into the script |
121
|
|
|
\wp_localize_script( |
122
|
|
|
'graphql-api-graphiql-client', |
123
|
|
|
'graphQLByPoPGraphiQLSettings', |
124
|
|
|
array( |
125
|
|
|
'nonce' => \wp_create_nonce('wp_rest'), |
126
|
|
|
'endpoint' => EndpointHelpers::getAdminGraphQLEndpoint(), |
127
|
|
|
'defaultQuery' => $this->getDefaultQuery(), |
128
|
|
|
'response' => $this->getResponse(), |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} else { |
132
|
|
|
// Print the HTML from the Client |
133
|
|
|
$htmlContent = $this->getGraphiQLWithExplorerClientHTML(); |
134
|
|
|
// Extract the JS/CSS assets from the <head> |
135
|
|
|
$matches = []; |
136
|
|
|
preg_match('/<head>(.*?)<\/head>/', $htmlContent, $matches); |
137
|
|
|
$headHTMLContent = $matches[1]; |
138
|
|
|
preg_match_all('/<link[^>]+href="([^">]+)"/', $headHTMLContent, $matches); |
139
|
|
|
$cssFileURLs = $matches[1]; |
140
|
|
|
foreach ($cssFileURLs as $index => $cssFileURL) { |
141
|
|
|
\wp_enqueue_style( |
142
|
|
|
'graphql-api-graphiql-with-explorer-'.$index, |
143
|
|
|
$cssFileURL, |
144
|
|
|
array(), |
145
|
|
|
\GRAPHQL_API_VERSION |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
preg_match_all('/<script[^>]+src="([^">]+)"/', $headHTMLContent, $matches); |
149
|
|
|
$jsFileURLs = $matches[1]; |
150
|
|
|
foreach ($jsFileURLs as $index => $jsFileURL) { |
151
|
|
|
\wp_enqueue_script( |
152
|
|
|
'graphql-api-graphiql-with-explorer-'.$index, |
153
|
|
|
$jsFileURL, |
154
|
|
|
array(), |
155
|
|
|
\GRAPHQL_API_VERSION, |
156
|
|
|
true |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Override styles for the admin, so load last |
161
|
|
|
\wp_enqueue_style( |
162
|
|
|
'graphql-api-graphiql-with-explorer-client', |
163
|
|
|
\GRAPHQL_API_URL . 'assets/css/graphiql-with-explorer-client.css', |
164
|
|
|
array(), |
165
|
|
|
\GRAPHQL_API_VERSION |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function getResponse(): string |
171
|
|
|
{ |
172
|
|
|
return ''; |
173
|
|
|
// return \__('Click the "Execute Query" button, or press Ctrl+Enter (Command+Enter in Mac)', 'graphql-api'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function getDefaultQuery(): string |
177
|
|
|
{ |
178
|
|
|
return <<<EOT |
179
|
|
|
# Welcome to GraphiQL |
180
|
|
|
# |
181
|
|
|
# GraphiQL is an in-browser tool for writing, validating, and |
182
|
|
|
# testing GraphQL queries. |
183
|
|
|
# |
184
|
|
|
# Type queries into this side of the screen, and you will see intelligent |
185
|
|
|
# typeaheads aware of the current GraphQL type schema and live syntax and |
186
|
|
|
# validation errors highlighted within the text. |
187
|
|
|
# |
188
|
|
|
# GraphQL queries typically start with a "{" character. Lines that starts |
189
|
|
|
# with a # are ignored. |
190
|
|
|
# |
191
|
|
|
# An example GraphQL query might look like: |
192
|
|
|
# |
193
|
|
|
# { |
194
|
|
|
# field(arg: "value") { |
195
|
|
|
# subField |
196
|
|
|
# } |
197
|
|
|
# } |
198
|
|
|
# |
199
|
|
|
# Run the query (at any moment): |
200
|
|
|
# |
201
|
|
|
# Ctrl-Enter (or press the play button above) |
202
|
|
|
# |
203
|
|
|
|
204
|
|
|
query { |
205
|
|
|
posts(limit:3) { |
206
|
|
|
id |
207
|
|
|
title |
208
|
|
|
date(format:"d/m/Y") |
209
|
|
|
url |
210
|
|
|
author { |
211
|
|
|
id |
212
|
|
|
name |
213
|
|
|
url |
214
|
|
|
} |
215
|
|
|
tags { |
216
|
|
|
name |
217
|
|
|
} |
218
|
|
|
featuredImage { |
219
|
|
|
src |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
EOT; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|