Skip to content

Commit

Permalink
switch to ClientRequest so we have better control (#115)
Browse files Browse the repository at this point in the history
* switch to ClientRequest so we have better control

* use the https library to support secure and insecure requests
  • Loading branch information
craig-day authored and gjtorikian committed Mar 2, 2018
1 parent c0523e4 commit a7787d2
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 108 deletions.
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"
},
"devDependencies": {
"asar": "^0.13.0",
Expand Down

0 comments on commit a7787d2

Please sign in to comment.