@giraphql/plugin-dataloader
Version:
A GiraphQL plugin for attaching dataloader to object types
22 lines (21 loc) • 921 B
JavaScript
import { RootFieldBuilder, } from '@giraphql/core';
import { rejectErrors } from './util.js';
import { dataloaderGetter } from './index.js';
const fieldBuilderProto = RootFieldBuilder.prototype;
fieldBuilderProto.loadable = function loadable({ load, sort, loaderOptions, resolve, type, ...options }) {
const getLoader = dataloaderGetter(loaderOptions, load, undefined, sort);
return this.field({
...options,
type,
// @ts-expect-error types don't match because this handles both lists and single objects
resolve: async (parent, args, context, info) => {
const ids = await resolve(parent, args, context, info);
const loader = getLoader(context);
if (Array.isArray(type)) {
return rejectErrors(loader.loadMany(ids));
}
return loader.load(ids);
},
});
};
//# sourceMappingURL=field-builder.js.map