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