@omnigraph/odata
Version:
36 lines (35 loc) • 1.58 kB
JavaScript
import { parseResolveInfo } from 'graphql-parse-resolve-info';
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';
import { prepareSearchParams } from '../utils/prepareSearchParams.js';
export function createSingletonResolver({ endpoint, singletonName, dataloaderFactory, expandNavProps, headersFactory, }) {
return function singletonResolver(root, args, context, info) {
const url = new URL(endpoint);
url.href = urljoin(url.href, '/' + singletonName);
const parsedInfoFragment = parseResolveInfo(info);
const searchParams = prepareSearchParams({
fragment: parsedInfoFragment,
schema: info.schema,
expandNavProps,
});
searchParams?.forEach((value, key) => {
url.searchParams.set(key, value);
});
const urlString = getUrlString(url);
const method = 'GET';
const request = new Request(urlString, {
method,
headers: headersFactory({
root,
args,
context,
info,
env: process.env,
}, method),
});
return handleMaybePromise(() => dataloaderFactory(context).load(request), response => handleMaybePromise(() => response.text(), responseText => handleResponseText(responseText, urlString, info)));
};
}