From cc95cca1360c61058618c206bc7908ecb0444c99 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 27 Mar 2017 15:41:24 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=AB=20remove=20FetchQL=20use=20custom?= =?UTF-8?q?=20implementation=20of=20GraphQL=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 - src/proxy.ts | 73 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index fea5d9a..374f634 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/proxy.ts b/src/proxy.ts index 661f4fb..4ccfce8 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -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, @@ -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); @@ -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); @@ -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) { @@ -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}`); + }); + }); + } +}