Skip to content

Commit

Permalink
🚫 remove FetchQL use custom implementation of GraphQL client
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 28, 2017
1 parent ba508e4 commit cc95cca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"express": "^4.14.1",
"express-graphql": "github:apis-guru/express-graphql#rootValue_dist",
"faker": "^4.1.0",
"fetchql": "^2.0.1",
"graphql": "github:apis-guru/graphql-js#directives-fork-dist",
"lodash": "^4.17.4",
"node-fetch": "^1.6.3",
Expand Down
73 changes: 42 additions & 31 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
global['fetch'] = require('node-fetch');

import {Response} from 'node-fetch';
import fetch from 'node-fetch';
import {Headers} from 'node-fetch';
import * as set from 'lodash/set.js';
import * as FetchQL from 'fetchql';

import {
Kind,
parse,
Expand All @@ -27,28 +26,9 @@ type RequestInfo = {
};

export function proxyMiddleware(url, headers) {
const remoteServer = new FetchQL({url, headers});
const errorPrefix = `Can't get introspection from ${url}:\n`;

return remoteServer.query({query: introspectionQuery})
.catch(errors => {
if (!Array.isArray(errors))
return {errors: [errors]};
if (errors[0].stack instanceof Response) {
const errResponce = errors[0].stack;
return errResponce.text().then(body => { throw Error(
errorPrefix +`${errResponce.status} ${errResponce.statusText}\n${body}`
)});
}
return {errors};
})
.then(introspection => {
if (introspection.errors) {
throw Error(
errorPrefix + JSON.stringify(introspection.errors, null, 2)
);
}
const remoteServer = requestFactory(url, headers);

return getIntrospection().then(introspection => {
const introspectionSchema = buildClientSchema(introspection.data);
const introspectionIDL = printSchema(introspectionSchema);

Expand All @@ -66,11 +46,7 @@ export function proxyMiddleware(url, headers) {
const variables = info.variables;
const operationName = info.operationName;

return remoteServer.query({
query,
variables,
operationName,
}).catch(errors => ({errors}))
return remoteServer(query, variables, operationName)
.then(response => {
const rootValue = response.data;
const [globalErrors, localErrors] = splitErrors(response.errors);
Expand All @@ -84,7 +60,19 @@ export function proxyMiddleware(url, headers) {
},
};
}];
})
});

function getIntrospection() {
return remoteServer(introspectionQuery)
.then(introspection => {
if (introspection.errors)
throw Error(JSON.stringify(introspection.errors, null, 2));
return introspection;
})
.catch(error => {
throw Error(`Can't get introspection from ${url}:\n${error.message}`);
})
}
}

function splitErrors(errors) {
Expand Down Expand Up @@ -160,3 +148,26 @@ function stripQuery(schema, queryAST, extensionFields) {

return print(changedAST);
}

function requestFactory(url, headersObj) {
return (query, variables?, operationName?) => {
return fetch(url, {
method: 'POST',
headers: new Headers({
"content-type": 'application/json',
...headersObj,
}),
body: JSON.stringify({
operationName,
query,
variables,
})
}).then(responce => {
if (responce.ok)
return responce.json();
return responce.text().then(body => {
throw Error(`${responce.status} ${responce.statusText}\n${body}`);
});
});
}
}

0 comments on commit cc95cca

Please sign in to comment.