deno.land / x / pothos@release-1713397530 / packages / plugin-dataloader / util.ts
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162// @ts-nocheckimport DataLoader, { Options } from 'https://cdn.skypack.dev/dataloader?dts';import { GraphQLResolveInfo } from 'https://cdn.skypack.dev/graphql?dts';import { createContextCache, isThenable, MaybePromise, SchemaTypes } from '../core/index.ts';export function rejectErrors<T>(val: MaybePromise<readonly (Error | T)[]>): MaybePromise<(Promise<T> | T)[]> { if (isThenable(val)) { return val.then(rejectErrors); } return val.map((item) => (item instanceof Error ? Promise.reject(item) : item));}export function loadAndSort<K, V, C, Args = never>(load: (keys: K[], context: C, args: Args) => Promise<readonly (Error | V)[]>, toKey: false | ((val: V) => K) | undefined) { if (!toKey) { return load; } return async (keys: K[], context: C, args: Args) => { const list = await load(keys, context, args); const map = new Map<K, V>(); const results = new Array<V | null>(); for (const val of list) { if (val instanceof Error) { throw val; } if (val != null) { map.set(toKey(val), val); } } for (let i = 0; i < keys.length; i += 1) { results[i] = map.get(keys[i]) ?? null; } return results; };}export function dataloaderGetter<K, V, C>(loaderOptions: Options<K, V, C> | undefined, load: (keys: K[], context: SchemaTypes["Context"]) => Promise<readonly (Error | V)[]>, toKey: ((val: V) => K) | undefined, sort: boolean | ((val: V) => K) | undefined) { const loader = (sort ? loadAndSort(load, typeof sort === "function" ? sort : toKey) : load) as (keys: readonly K[], context: SchemaTypes["Context"]) => Promise<V[]>; return createContextCache((context: object) => new DataLoader<K, V, C>((keys) => loader(keys, context), loaderOptions));}export function pathDataloaderGetter<K, V, C, Args>(loaderOptions: Options<K, V, C> | undefined, load: (keys: K[], context: SchemaTypes["Context"], args: Args) => Promise<readonly (Error | V)[]>, toKey: ((val: V) => K) | undefined, sort: boolean | ((val: V) => K) | undefined, byPath?: boolean) { const cache = createContextCache(() => new Map<string, DataLoader<K, V, C>>()); const loader = (sort ? loadAndSort(load, typeof sort === "function" ? sort : toKey) : load) as (keys: readonly K[], context: SchemaTypes["Context"], args: Args) => Promise<V[]>; return (args: Args, ctx: SchemaTypes["Context"], info: GraphQLResolveInfo) => { const key = byPath ? cacheKey(info.path) : "*"; const map = cache(ctx); if (!map.has(key)) { map.set(key, new DataLoader<K, V, C>((keys) => loader(keys, ctx, args), loaderOptions)); } return map.get(key)!; };}export function cacheKey(path: GraphQLResolveInfo["path"] | undefined) { if (!path) { // Root return "*"; } let key = String(path.key); let current = path.prev; while (current) { key = `${typeof current.key === "number" ? "*" : current.key}.${key}`; current = current.prev; } return key;}
Version Info