Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to ClientRequest so we have better control #115

Merged
merged 2 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 48 additions & 22 deletions app/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import _ from 'lodash';
import uuid from 'uuid';
import React from 'react';
import ReactDOM from 'react-dom';
import fetch from 'isomorphic-fetch';
import GraphiQL from 'graphiql/dist';
import Modal from 'react-modal/lib/index';
import { request as httpRequest } from 'http';
import { request as httpsRequest } from 'https';

Modal.setAppElement(document.getElementById('react-root'));

Expand Down Expand Up @@ -160,14 +161,14 @@ export default class App extends React.Component {
span.textContent = text;
const selection = window.getSelection();
document.body.appendChild(span);

const range = document.createRange();
selection.removeAllRanges();
range.selectNode(span);
selection.addRange(range);

document.execCommand('copy');

selection.removeAllRanges();
span.remove();
}
Expand All @@ -183,7 +184,8 @@ export default class App extends React.Component {

graphQLFetcher = (graphQLParams) => {
const defaultHeaders = {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': 'graphiql-app'
};

const error = {
Expand All @@ -208,31 +210,55 @@ export default class App extends React.Component {
});
}


const requestHeaders = Object.assign({}, defaultHeaders, headers);
const url = new URL(endpoint);

if (method == "get") {
var url = endpoint;
if (typeof graphQLParams['variables'] === "undefined"){
graphQLParams['variables'] = "{}";
}

url += url.indexOf('?') == -1 ? "?" : "&";
const query = encodeURIComponent(graphQLParams['query']);
const variables = encodeURIComponent(JSON.stringify(graphQLParams['variables']));

return fetch(url + "query=" + encodeURIComponent(graphQLParams['query']) + "&variables=" + encodeURIComponent(JSON.stringify(graphQLParams['variables'])), {
method: method,
credentials: 'include',
headers: Object.assign({}, defaultHeaders, headers),
body: null
}).then(response => response.json())
.catch(reason => error);
url.search = `query=${query}&variables=${variables}`;
}
return fetch(endpoint, {
method: method,
credentials: 'include',
headers: Object.assign({}, defaultHeaders, headers),
body: JSON.stringify(graphQLParams)
}).then(response => response.json())
.catch(reason => error);

const requestOptions = {
method,
protocol: url.protocol,
hostname: url.hostname,
port: url.port,
path: url.pathname + url.search,
headers: requestHeaders,
};

const request = url.protocol === 'https:' ? httpsRequest(requestOptions) : httpRequest(requestOptions);

return new Promise((resolve, reject) => {
request.on('response', response => {
const chunks = [];
response.on('data', data => {
chunks.push(Buffer.from(data));
});
response.on('end', end => {
const data = Buffer.concat(chunks).toString();
if (response.statusCode >= 400) {
reject(data);
} else {
resolve(JSON.parse(data));
}
});
});

request.on('error', reject);

if (method == "get") {
request.end();
} else {
request.end(JSON.stringify(graphQLParams));
}
})
}

handleChange(field, eOrKey, e) {
Expand Down
140 changes: 57 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@
"dependencies": {
"graphiql": "^0.11.11",
"graphql": "^0.13.0",
"isomorphic-fetch": "^2.1.1",
"lodash": "^3.10.1",
"mousetrap": "^1.5.3",
"object-assign": "^4.0.1",
"prop-types": "^15.6.0",
"radium": "^0.14.1",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-modal": "^2.0.2",
"uuid": "^3.1.0",
"prop-types": "^15.6.0"
"uuid": "^3.1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

},
"devDependencies": {
"asar": "^0.13.0",
Expand Down