Skip to content

Commit 53d287b

Browse files
authored
Merge pull request #66 from dadish/main
Merge main into stable
2 parents 69d96d5 + 9fee3e7 commit 53d287b

File tree

6 files changed

+38
-107
lines changed

6 files changed

+38
-107
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [1.4.0-rc.3](https://github.com/dadish/ProcessGraphQL/compare/v1.4.0-rc.2...v1.4.0-rc.3) (2022-07-29)
2+
3+
4+
### Features
5+
6+
* **graphiql:** add support for request headers ([c0fb4be](https://github.com/dadish/ProcessGraphQL/commit/c0fb4beece26453d46f782fc95df1fa29b476051))
7+
18
# [1.4.0-rc.2](https://github.com/dadish/ProcessGraphQL/compare/v1.4.0-rc.1...v1.4.0-rc.2) (2022-07-27)
29

310

ProcessGraphQL.module

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ProcessGraphQL extends Process implements Module {
1515
{
1616
return array(
1717
'title' => 'GraphQL',
18-
'version' => '1.4.0-rc.2',
18+
'version' => '1.4.0-rc.3',
1919
'summary' => 'GraphQL for ProcessWire.',
2020
'href' => 'https://github.com/dadish/ProcessGraphql',
2121
'singular' => true,
@@ -128,12 +128,10 @@ class ProcessGraphQL extends Process implements Module {
128128
*/
129129
public function setupGraphiQLAssets()
130130
{
131-
$this->config->scripts->add("https://unpkg.com/[email protected]/dist/es6-promise.auto.min.js");
132-
$this->config->scripts->add("https://unpkg.com/[email protected]/dist/fetch.umd.js");
133-
$this->config->scripts->add("https://unpkg.com/[email protected]/umd/react.production.min.js");
134-
$this->config->scripts->add("https://unpkg.com/[email protected]/umd/react-dom.production.min.js");
135-
$this->config->scripts->add('https://unpkg.com/[email protected]/graphiql.min.js');
136-
$this->config->styles->add('https://unpkg.com/[email protected]/graphiql.css');
131+
$this->config->scripts->add("https://unpkg.com/[email protected]/umd/react.production.min.js");
132+
$this->config->scripts->add("https://unpkg.com/[email protected]/umd/react-dom.production.min.js");
133+
$this->config->scripts->add('https://unpkg.com/[email protected]/graphiql.min.js');
134+
$this->config->styles->add('https://unpkg.com/[email protected]/graphiql.css');
137135
$this->config->js($this->className, [
138136
'GraphQLServerUrl' => $this->getGraphQLServerUrl(),
139137
]);

graphiql/partial.php

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php if(!$fullGraphiQL): ?>
1+
<?php if (!$fullGraphiQL): ?>
22
<style>
33
.AdminThemeDefault #graphiql {
44
margin-top: 30px;
@@ -37,95 +37,32 @@
3737
<?php endif; ?>
3838
<div id="graphiql">Loading...</div>
3939
<script>
40-
/**
41-
* The below code is a copy from
42-
* https://github.com/graphql/graphiql/blob/master/packages/graphiql-examples/cdn/index.html
43-
* The only thing that's changed is the graphiql request url and added an X-Requested-With header.
44-
*/
45-
// Parse the search string to get url parameters.
46-
var search = window.location.search;
47-
var parameters = {};
48-
search.substr(1).split('&').forEach(function (entry) {
49-
var eq = entry.indexOf('=');
50-
if (eq >= 0) {
51-
parameters[decodeURIComponent(entry.slice(0, eq))] =
52-
decodeURIComponent(entry.slice(eq + 1));
53-
}
54-
});
55-
// If variables was provided, try to format it.
56-
if (parameters.variables) {
57-
try {
58-
parameters.variables =
59-
JSON.stringify(JSON.parse(parameters.variables), null, 2);
60-
} catch (e) {
61-
// Do nothing, we want to display the invalid JSON as a string, rather
62-
// than present an error.
63-
}
64-
}
65-
// When the query and variables string is edited, update the URL bar so
66-
// that it can be easily shared.
67-
function onEditQuery(newQuery) {
68-
parameters.query = newQuery;
69-
updateURL();
70-
}
71-
function onEditVariables(newVariables) {
72-
parameters.variables = newVariables;
73-
updateURL();
74-
}
75-
function onEditOperationName(newOperationName) {
76-
parameters.operationName = newOperationName;
77-
updateURL();
78-
}
79-
function updateURL() {
80-
var newSearch = '?' + Object.keys(parameters).filter(function (key) {
81-
return Boolean(parameters[key]);
82-
}).map(function (key) {
83-
return encodeURIComponent(key) + '=' +
84-
encodeURIComponent(parameters[key]);
85-
}).join('&');
86-
history.replaceState(null, null, newSearch);
87-
}
88-
// Defines a GraphQL fetcher using the fetch API. You're not required to
89-
// use fetch, and could instead implement graphQLFetcher however you like,
90-
// as long as it returns a Promise or Observable.
91-
function graphQLFetcher(graphQLParams) {
92-
// When working locally, the example expects a GraphQL server at the path /graphql.
93-
// In a PR preview, it connects to the Star Wars API externally.
94-
// Change this to point wherever you host your GraphQL server.
95-
return fetch(config.ProcessGraphQL.GraphQLServerUrl, {
96-
method: 'post',
97-
headers: {
98-
'Accept': 'application/json',
99-
'Content-Type': 'application/json',
100-
'X-Requested-With': 'XMLHttpRequest',
40+
function graphQLFetcher(graphQLParams, options ) {
41+
const headers = options.headers || {};
42+
return fetch(
43+
window.config?.ProcessGraphQL?.GraphQLServerUrl || '',
44+
{
45+
method: 'post',
46+
headers: Object.assign({
47+
Accept: 'application/json',
48+
'Content-Type': 'application/json',
49+
'X-Requested-With': 'XMLHttpRequest',
50+
}, headers),
51+
body: JSON.stringify(graphQLParams),
52+
credentials: 'include',
10153
},
102-
body: JSON.stringify(graphQLParams),
103-
credentials: 'include',
104-
}).then(function (response) {
105-
return response.text();
106-
}).then(function (responseBody) {
107-
try {
108-
return JSON.parse(responseBody);
109-
} catch (error) {
110-
return responseBody;
111-
}
54+
).then(function (response) {
55+
return response.json().catch(function () {
56+
return response.text();
57+
});
11258
});
11359
}
114-
// Render <GraphiQL /> into the body.
115-
// See the README in the top level of this module to learn more about
116-
// how you can customize GraphiQL by providing different values or
117-
// additional child elements.
60+
11861
ReactDOM.render(
11962
React.createElement(GraphiQL, {
12063
fetcher: graphQLFetcher,
121-
query: parameters.query,
122-
variables: parameters.variables,
123-
operationName: parameters.operationName,
124-
onEditQuery: onEditQuery,
125-
onEditVariables: onEditVariables,
12664
defaultVariableEditorOpen: true,
127-
onEditOperationName: onEditOperationName
12865
}),
129-
document.getElementById('graphiql')
66+
document.getElementById('graphiql'),
13067
);
13168
</script>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "processgraphql",
3-
"version": "1.4.0-rc.2",
3+
"version": "1.4.0-rc.3",
44
"description": "GraphQL for ProcessWire",
55
"main": "index.js",
66
"directories": {

test/client.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,17 @@
88

99
use ProcessWire\ProcessWire;
1010

11-
$wireConfig = ProcessWire::buildConfig($pwDir, null, [
12-
"siteDir" => "site-default"
13-
]);
11+
$wireConfig = ProcessWire::buildConfig($pwDir);
1412
$wire = new ProcessWire($wireConfig);
1513

1614
// =====================
1715
// Setup GraphiQL Assets
1816
// =====================
1917
$config = $wire->fuel('config');
2018

21-
$config->js('ProcessGraphQL', [
22-
'GraphQLServerUrl' => 'http://127.0.0.1:8091',
23-
]);
24-
25-
$config->scripts->add("https://unpkg.com/[email protected]/dist/es6-promise.auto.min.js");
26-
$config->scripts->add("https://unpkg.com/[email protected]/dist/fetch.umd.js");
27-
$config->scripts->add("https://unpkg.com/[email protected]/umd/react.production.min.js");
28-
$config->scripts->add("https://unpkg.com/[email protected]/umd/react-dom.production.min.js");
29-
$config->scripts->add('https://unpkg.com/[email protected]/graphiql.min.js');
30-
$config->styles->add('https://unpkg.com/[email protected]/graphiql.css');
19+
$graphql = $wire->fuel('modules')->get('ProcessGraphQL');
20+
$graphql->GraphQLServerUrl = 'http://127.0.0.1:8091';
21+
$graphql->setupGraphiQLAssets();
3122

3223
$filename = realpath($baseDir . $_SERVER['SCRIPT_NAME']);
3324
if ($filename === $baseDir) {

test/server.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
use ProcessWire\ProcessWire;
1313

14-
$wireConfig = ProcessWire::buildConfig($pwDir, null, [
15-
"siteDir" => "site-default"
16-
]);
14+
$wireConfig = ProcessWire::buildConfig($pwDir);
1715
$wire = new ProcessWire($wireConfig);
1816
$modules = $wire->fuel('modules');
1917

0 commit comments

Comments
 (0)