UNPKG

@omnigraph/odata

Version:
26 lines (25 loc) 1.14 kB
import urljoin from 'url-join'; import { Request } from '@whatwg-node/fetch'; import { handleMaybePromise } from '@whatwg-node/promise-helpers'; import { getUrlString } from '../utils/getUrlString.js'; import { handleResponseText } from '../utils/handleResponseText.js'; export function createUnboundActionResolver({ actionName, dataloaderFactory, headersFactory, endpoint, }) { return function unboundActionResolver(root, args, context, info) { const url = new URL(endpoint); url.href = urljoin(url.href, '/' + actionName); const urlString = getUrlString(url); const method = 'POST'; const request = new Request(urlString, { method, headers: headersFactory({ root, args, context, info, env: process.env, }, method), body: JSON.stringify(args), }); return handleMaybePromise(() => dataloaderFactory(context).load(request), response => handleMaybePromise(() => response.text(), responseText => handleResponseText(responseText, urlString, info))); }; }