Skip to content

Commit 5b7d97a

Browse files
committed
feat: add playground
1 parent fbcbe06 commit 5b7d97a

File tree

4 files changed

+89
-66
lines changed

4 files changed

+89
-66
lines changed

plugin.php

+35-22
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ function VFA_simulate_as_not_rest($is_rest_api_request)
3737
add_action('wp_ajax_vf_update', 'VFA_vuefront_admin_action_update');
3838
add_action('wp_ajax_vf_turn_off', 'VFA_vuefront_admin_action_turn_off');
3939
add_action('wp_ajax_vf_information', 'VFA_vuefront_admin_action_vf_information');
40-
register_activation_hook (__FILE__, 'VFA_install');
41-
add_action( 'plugins_loaded', 'VFA_update_db_check' );
40+
register_activation_hook(__FILE__, 'VFA_install');
41+
42+
add_action('plugins_loaded', 'VFA_update_db_check');
4243

4344
function VFA_vuefront_admin_styles()
4445
{
@@ -73,24 +74,27 @@ function VFA_vuefront_rmdir($dir)
7374
}
7475
}
7576

76-
function VFA_vuefront_admin_action_settings() {
77+
function VFA_vuefront_admin_action_settings()
78+
{
7779
$vfSetting = get_option('vuefront-settings') ? get_option('vuefront-settings') : array();
7880
echo json_encode(
79-
$vfSetting
80-
, JSON_FORCE_OBJECT);
81+
$vfSetting,
82+
JSON_FORCE_OBJECT
83+
);
8184

8285
wp_die();
8386
}
8487

85-
function VFA_vuefront_admin_action_settings_edit() {
86-
$vfSetting = json_decode(stripslashes(html_entity_decode($_POST['setting'], ENT_QUOTES, 'UTF-8')), true);
87-
update_option('vuefront-settings', $vfSetting);
88+
function VFA_vuefront_admin_action_settings_edit()
89+
{
90+
$vfSetting = json_decode(stripslashes(html_entity_decode($_POST['setting'], ENT_QUOTES, 'UTF-8')), true);
91+
update_option('vuefront-settings', $vfSetting);
8892

89-
echo json_encode(
90-
array('success' => 'success')
91-
);
93+
echo json_encode(
94+
array('success' => 'success')
95+
);
9296

93-
wp_die();
97+
wp_die();
9498
}
9599

96100
function VFA_vuefront_admin_action_vf_information()
@@ -152,33 +156,36 @@ function VFA_vuefront_admin_action_apps_create()
152156
wp_die();
153157
}
154158

155-
function VFA_install () {
156-
global $wpdb;
159+
function VFA_install()
160+
{
161+
global $wpdb;
157162

158-
$table_name = $wpdb->prefix . "vuefront_url";
163+
$table_name = $wpdb->prefix . "vuefront_url";
159164

160-
$charset_collate = $wpdb->get_charset_collate();
165+
$charset_collate = $wpdb->get_charset_collate();
161166

162-
$sql = 'CREATE TABLE IF NOT EXISTS `' . $table_name . '` (
167+
$sql = 'CREATE TABLE IF NOT EXISTS `' . $table_name . '` (
163168
`id_url` int(11) unsigned NOT NULL AUTO_INCREMENT,
164169
`id` varchar( 255 ) NOT NULL,
165170
`type` varchar(64) NOT NULL,
166171
`url` varchar(255) NOT NULL,
167172
PRIMARY KEY (`id_url`)
168173
) '.$charset_collate;
169174

170-
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
171-
dbDelta( $sql );
175+
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
176+
dbDelta($sql);
172177
}
173178

174-
function VFA_update_db_check() {
179+
function VFA_update_db_check()
180+
{
175181
global $vfa_db_version;
176-
if ( get_site_option( 'vfa_db_version' ) != $vfa_db_version ) {
182+
if (get_site_option('vfa_db_version') != $vfa_db_version) {
177183
VFA_install();
178184
}
179185
}
180186

181-
function VFA_vuefront_admin_action_apps_edit() {
187+
function VFA_vuefront_admin_action_apps_edit()
188+
{
182189
$setting = get_option('vuefront-apps');
183190

184191
$app = json_decode(stripslashes(html_entity_decode($_POST['app'], ENT_QUOTES, 'UTF-8')), true);
@@ -435,6 +442,7 @@ function VFA_RestApi(WP_REST_Request $request)
435442
$registry = VFA_Start();
436443

437444
$registry->set('request', $request);
445+
$registry->set('response', rest_ensure_response(array()));
438446

439447
$output = $registry->get('load')->resolver('startup/startup');
440448

@@ -464,6 +472,11 @@ function VFA_Callback(WP_REST_Request $request)
464472
'callback' => 'VFA_RestApi',
465473
));
466474

475+
register_rest_route('vuefront/v1', '/graphql', array(
476+
'methods' => 'GET',
477+
'callback' => 'VFA_RestApi',
478+
));
479+
467480
register_rest_route('vuefront/v1', '/callback', array(
468481
'methods' => 'POST',
469482
'callback' => 'VFA_Callback',

resolver/common/account.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ public function customerList($args)
1818
$filter_data['search'] = $args['search'];
1919
}
2020

21-
2221
$filter_data['role'] = get_option('default_role');
2322

24-
$args = wp_parse_args($filter_data);
23+
$args = wp_parse_args($filter_data);
2524

2625
$user_search = new WP_User_Query($args);
2726

resolver/startup/startup.php

+36-30
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,54 @@ class VFA_ResolverStartupStartup extends VFA_Resolver
77
{
88
public function index()
99
{
10-
if ($this->request->get_param('cors')) {
11-
if (! empty($_SERVER['HTTP_ORIGIN'])) {
12-
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
13-
} else {
14-
header('Access-Control-Allow-Origin: *');
10+
if ($this->request->get_method() === 'POST') {
11+
if ($this->request->get_param('cors')) {
12+
if (! empty($_SERVER['HTTP_ORIGIN'])) {
13+
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
14+
} else {
15+
header('Access-Control-Allow-Origin: *');
16+
}
17+
header('Access-Control-Allow-Methods: POST, OPTIONS');
18+
header('Access-Control-Allow-Credentials: true');
19+
header('Access-Control-Allow-Headers: accept,Referer,content-type,x-forwaded-for,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,cache-control,Content-Type,Range,Token,token,Cookie,cookie,content-type');
1520
}
16-
header('Access-Control-Allow-Methods: POST, OPTIONS');
17-
header('Access-Control-Allow-Credentials: true');
18-
header('Access-Control-Allow-Headers: accept,Referer,content-type,x-forwaded-for,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,cache-control,Content-Type,Range,Token,token,Cookie,cookie,content-type');
19-
}
2021

21-
$this->load->model('startup/startup');
22-
$this->load->model('common/vuefront');
22+
$this->load->model('startup/startup');
23+
$this->load->model('common/vuefront');
2324

24-
try {
25-
$resolvers = $this->model_startup_startup->getResolvers();
26-
$files = array(VFA_DIR_PLUGIN . 'schema.graphql');
25+
try {
26+
$resolvers = $this->model_startup_startup->getResolvers();
27+
$files = array(VFA_DIR_PLUGIN . 'schema.graphql');
2728

28-
if ($this->model_common_vuefront->checkAccess()) {
29-
$files[] = VFA_DIR_PLUGIN . 'schemaAdmin.graphql';
30-
}
29+
if ($this->model_common_vuefront->checkAccess()) {
30+
$files[] = VFA_DIR_PLUGIN . 'schemaAdmin.graphql';
31+
}
3132

32-
$sources = array_map('file_get_contents', $files);
33+
$sources = array_map('file_get_contents', $files);
3334

34-
$source = $this->model_common_vuefront->mergeSchemas(($sources));
35-
$schema = BuildSchema::build($source);
36-
$rawInput = file_get_contents('php://input');
37-
$input = json_decode($rawInput, true);
38-
$query = $input['query'];
35+
$source = $this->model_common_vuefront->mergeSchemas(($sources));
36+
$schema = BuildSchema::build($source);
37+
$rawInput = file_get_contents('php://input');
38+
$input = json_decode($rawInput, true);
39+
$query = $input['query'];
3940

40-
$variableValues = isset($input['variables']) ? $input['variables'] : null;
41-
$result = GraphQL::executeQuery($schema, $query, $resolvers, null, $variableValues);
42-
} catch (\Exception $e) {
43-
$result = [
41+
$variableValues = isset($input['variables']) ? $input['variables'] : null;
42+
$result = GraphQL::executeQuery($schema, $query, $resolvers, null, $variableValues);
43+
} catch (\Exception $e) {
44+
$result = [
4445
'error' => [
4546
'message' => $e->getMessage()
4647
]
4748
];
48-
}
49-
49+
}
5050

51-
return $result;
51+
$this->response->set_data($result);
52+
return $this->response;
53+
} else {
54+
header('Content-type: text/html');
55+
echo "<html> <head> <title>Simple GraphiQL Example</title> <link href='https://unpkg.com/graphiql/graphiql.min.css' rel='stylesheet'/> </head> <body style='margin: 0;'> <div id='graphiql' style='height: 100vh;'></div><script crossorigin src='https://unpkg.com/react/umd/react.production.min.js' ></script> <script crossorigin src='https://unpkg.com/react-dom/umd/react-dom.production.min.js' ></script> <script crossorigin src='https://unpkg.com/graphiql/graphiql.min.js' ></script> <script>const fetcher=GraphiQL.createFetcher({url: window.location.href}); ReactDOM.render( React.createElement(GraphiQL,{fetcher: fetcher}), document.getElementById('graphiql'), ); </script> </body> </html>";
56+
exit;
57+
}
5258
}
5359

5460
public function determine_current_user($user)

system/engine/resolver.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
* @property VFA_ModelCommonSeo $model_common_seo
1818
* @property VFA_ModelStoreManufacturer $model_store_manufacturer
1919
* @property WP_REST_Request $request
20+
* @property WP_REST_Response $response
2021
*/
21-
abstract class VFA_Resolver {
22-
protected $registry;
22+
abstract class VFA_Resolver
23+
{
24+
protected $registry;
2325

24-
public function __construct($registry) {
25-
$this->registry = $registry;
26-
}
26+
public function __construct($registry)
27+
{
28+
$this->registry = $registry;
29+
}
2730

28-
public function __get($key) {
29-
return $this->registry->get($key);
30-
}
31+
public function __get($key)
32+
{
33+
return $this->registry->get($key);
34+
}
3135

32-
public function __set($key, $value) {
33-
$this->registry->set($key, $value);
34-
}
35-
}
36+
public function __set($key, $value)
37+
{
38+
$this->registry->set($key, $value);
39+
}
40+
}

0 commit comments

Comments
 (0)