UNPKG

@graphql-mesh/transport-http

Version:
80 lines (76 loc) 3.46 kB
'use strict'; var crossHelpers = require('@graphql-mesh/cross-helpers'); var stringInterpolation = require('@graphql-mesh/string-interpolation'); var utils = require('@graphql-mesh/utils'); var executorHttp = require('@graphql-tools/executor-http'); var promiseHelpers = require('@whatwg-node/promise-helpers'); var index = { getSubgraphExecutor(payload) { let headersInConfig; if (typeof payload.transportEntry.headers === "string") { headersInConfig = JSON.parse(payload.transportEntry.headers); } if (Array.isArray(payload.transportEntry.headers)) { headersInConfig = Object.fromEntries(payload.transportEntry.headers); } const headersFactory = headersInConfig ? stringInterpolation.getInterpolatedHeadersFactory(headersInConfig) : void 0; const httpExecutor = executorHttp.buildHTTPExecutor({ endpoint: payload.transportEntry.location, headers: headersFactory ? (execReq) => headersFactory({ env: crossHelpers.process.env, root: execReq?.rootValue, context: execReq?.context, info: execReq?.info }) : void 0, ...payload.transportEntry.options, getDisposeReason: payload.getDisposeReason, serviceName: payload.subgraphName, // @ts-expect-error - TODO: Fix this in executor-http fetch: payload.fetch }); if (payload.transportEntry.options && "subscriptions" in payload.transportEntry.options && payload.transportEntry.options.subscriptions != null) { let subscriptionsExecutor = function(execReq) { const subscriptionsKind = payload.transportEntry.options?.subscriptions?.kind || payload.transportEntry.kind; const subscriptionsLocation = payload.transportEntry.options?.subscriptions?.location ? new URL( payload.transportEntry.options.subscriptions.location, payload.transportEntry.location ).toString() : payload.transportEntry.location; return promiseHelpers.handleMaybePromise( () => payload.getTransportExecutor({ ...payload.transportEntry, kind: subscriptionsKind, headers: ( // WebSocket transport should not have any headers by default, // `connectionParams` should be preferred. subscriptionsKind === "ws" ? payload.transportEntry.options?.subscriptions?.headers : payload.transportEntry.options?.subscriptions?.headers ?? payload.transportEntry.headers ), location: subscriptionsLocation, options: { ...payload.transportEntry.options, ...payload.transportEntry.options?.subscriptions?.options } }), (resolvedSubscriptionsExecutor) => { subscriptionsExecutor = resolvedSubscriptionsExecutor; return subscriptionsExecutor(execReq); } ); }; return utils.makeAsyncDisposable( function hybridExecutor(executionRequest) { if (subscriptionsExecutor && executionRequest.operationType === "subscription") { return subscriptionsExecutor(executionRequest); } return httpExecutor(executionRequest); }, () => Promise.all([ utils.dispose(httpExecutor), utils.isDisposable(subscriptionsExecutor) && utils.dispose(subscriptionsExecutor) ]).then(() => { }) ); } return httpExecutor; } }; module.exports = index;