UNPKG

@jokio/graphql

Version:

High level, pre-configured, GraphQL Server

177 lines (176 loc) 8.26 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var apollo_link_1 = require("apollo-link"); var printer_1 = require("graphql/language/printer"); var node_fetch_1 = require("node-fetch"); var throwServerError = function (response, result, message) { var error = new Error(message); error.response = response; error.statusCode = response.status; error.result = result; throw error; }; var parseAndCheckResponse = function (request) { return function (response) { return response .text() .then(function (bodyText) { try { return JSON.parse(bodyText); } catch (err) { var parseError = err; parseError.response = response; parseError.statusCode = response.status; parseError.bodyText = bodyText; return Promise.reject(parseError); } }) .then(function (result) { if (response.status >= 300) { //Network error throwServerError(response, result, "Response not successful: Received status code " + response.status); } if (!result.hasOwnProperty('data') && !result.hasOwnProperty('errors')) { //Data error throwServerError(response, result, "Server response was missing for query '" + request.operationName + "'."); } return result; }); }; }; var checkFetcher = function (fetcher) { if (fetcher.use) { throw new Error("\n It looks like you're using apollo-fetch! Apollo Link now uses native fetch\n implementation, so apollo-fetch is not needed. If you want to use your existing\n apollo-fetch middleware, please check this guide to upgrade:\n https://github.com/apollographql/apollo-link/blob/master/docs/implementation.md\n "); } }; var warnIfNoFetch = function (fetcher) { if (!fetcher && typeof node_fetch_1.default === 'undefined') { var library = 'unfetch'; if (typeof window === 'undefined') library = 'node-fetch'; throw new Error("fetch is not found globally and no fetcher passed, to fix pass a fetch for\n your environment like https://www.npmjs.com/package/" + library + ".\n For example:\n import fetch from '" + library + "';\n import { createHttpLink } from 'apollo-link-http';\n const link = createHttpLink({ uri: '/graphql', fetch: fetch });\n "); } }; var createSignalIfSupported = function () { if (typeof AbortController === 'undefined') return { controller: false, signal: false }; var controller = new AbortController(); var signal = controller.signal; return { controller: controller, signal: signal }; }; var defaultHttpOptions = { includeQuery: true, includeExtensions: false, }; exports.createHttpLink = function (linkOptions) { if (linkOptions === void 0) { linkOptions = {}; } var uri = linkOptions.uri, fetcher = linkOptions.fetch, includeExtensions = linkOptions.includeExtensions, requestOptions = __rest(linkOptions, ["uri", "fetch", "includeExtensions"]); // dev warnings to ensure fetch is present warnIfNoFetch(fetcher); if (fetcher) checkFetcher(fetcher); // use default global fetch is nothing passed in if (!fetcher) fetcher = node_fetch_1.default; if (!uri) uri = '/graphql'; return new apollo_link_1.ApolloLink(function (operation) { return new apollo_link_1.Observable(function (observer) { var _a = operation.getContext(), headers = _a.headers, credentials = _a.credentials, _b = _a.fetchOptions, fetchOptions = _b === void 0 ? {} : _b, contextURI = _a.uri, _c = _a.http, httpOptions = _c === void 0 ? {} : _c; var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; var http = __assign({}, defaultHttpOptions, httpOptions); var body = { operationName: operationName, variables: variables }; if (includeExtensions || http.includeExtensions) body.extensions = extensions; // not sending the query (i.e persisted queries) if (http.includeQuery) body.query = printer_1.print(query); var serializedBody; try { serializedBody = JSON.stringify(body); } catch (e) { var parseError = new Error("Network request failed. Payload is not serializable: " + e.message); parseError.parseError = e; throw parseError; } var options = fetchOptions; if (requestOptions.fetchOptions) options = __assign({}, requestOptions.fetchOptions, options); var fetcherOptions = __assign({ method: 'POST' }, options, { headers: { // headers are case insensitive (https://stackoverflow.com/a/5259004) accept: '*/*', 'content-type': 'application/json', }, body: serializedBody }); if (requestOptions.credentials) fetcherOptions.credentials = requestOptions.credentials; if (credentials) fetcherOptions.credentials = credentials; if (requestOptions.headers) fetcherOptions.headers = __assign({}, fetcherOptions.headers, requestOptions.headers); if (headers) fetcherOptions.headers = __assign({}, fetcherOptions.headers, headers); var _d = createSignalIfSupported(), controller = _d.controller, signal = _d.signal; if (controller) fetcherOptions.signal = signal; fetcher(contextURI || uri, fetcherOptions) .then(function (response) { operation.setContext({ response: response }); return response; }) .then(parseAndCheckResponse(operation)) .then(function (result) { // we have data and can send it to back up the link chain observer.next(result); observer.complete(); return result; }) .catch(function (err) { // fetch was cancelled so its already been cleaned up in the unsubscribe if (err.name === 'AbortError') return; observer.error(err); }); return function () { // XXX support canceling this request // https://developers.google.com/web/updates/2017/09/abortable-fetch if (controller) controller.abort(); }; }); }); }; var HttpLink = /** @class */ (function (_super) { __extends(HttpLink, _super); function HttpLink(opts) { return _super.call(this, exports.createHttpLink(opts).request) || this; } return HttpLink; }(apollo_link_1.ApolloLink)); exports.HttpLink = HttpLink;