@giraphql/plugin-dataloader
Version:
A GiraphQL plugin for attaching dataloader to object types
100 lines (99 loc) • 4.18 kB
JavaScript
import SchemaBuilder from '@giraphql/core';
import { LoadableInterfaceRef } from './refs/interface.js';
import { LoadableObjectRef } from './refs/object.js';
import { LoadableUnionRef } from './refs/union.js';
import { dataloaderGetter } from './index.js';
const schemaBuilderProto = SchemaBuilder.prototype;
schemaBuilderProto.loadableObject = function loadableObject(nameOrRef, { load, toKey, sort, cacheResolved, loaderOptions, ...options }) {
var _a;
const name = typeof nameOrRef === "string"
? nameOrRef
: (_a = options.name) !== null && _a !== void 0 ? _a : nameOrRef.name;
const getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort);
const ref = new LoadableObjectRef(name, getDataloader);
this.objectType(ref, {
...options,
extensions: {
getDataloader,
cacheResolved: typeof cacheResolved === "function" ? cacheResolved : cacheResolved && toKey,
},
});
if (typeof nameOrRef !== "string") {
this.configStore.associateRefWithName(nameOrRef, name);
}
return ref;
};
schemaBuilderProto.loadableInterface = function loadableInterface(nameOrRef, { load, toKey, sort, cacheResolved, loaderOptions, ...options }) {
var _a;
const name = typeof nameOrRef === "string"
? nameOrRef
: (_a = options.name) !== null && _a !== void 0 ? _a : nameOrRef.name;
const getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort);
const ref = new LoadableInterfaceRef(name, getDataloader);
this.interfaceType(ref, {
...options,
extensions: {
getDataloader,
cacheResolved: typeof cacheResolved === "function" ? cacheResolved : cacheResolved && toKey,
},
});
if (typeof nameOrRef !== "string") {
this.configStore.associateRefWithName(nameOrRef, name);
}
return ref;
};
schemaBuilderProto.loadableUnion = function loadableUnion(name, { load, toKey, sort, cacheResolved, loaderOptions, ...options }) {
const getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort);
const ref = new LoadableUnionRef(name, getDataloader);
this.unionType(name, {
...options,
extensions: {
getDataloader,
cacheResolved: typeof cacheResolved === "function" ? cacheResolved : cacheResolved && toKey,
},
});
this.configStore.associateRefWithName(ref, name);
return ref;
};
const TloadableNode = schemaBuilderProto.loadableNode;
schemaBuilderProto.loadableNode = function loadableNode(nameOrRef, { load, toKey, sort, cacheResolved, loaderOptions, ...options }) {
var _a, _b;
if (typeof this
.nodeInterfaceRef !== "function") {
throw new TypeError("builder.loadableNode requires @giraphql/plugin-relay to be installed");
}
const name = typeof nameOrRef === "string"
? nameOrRef
: (_a = options.name) !== null && _a !== void 0 ? _a : nameOrRef.name;
const getDataloader = dataloaderGetter(loaderOptions, load, toKey, sort);
const ref = new LoadableObjectRef(name, getDataloader);
const extendedOptions = {
...options,
interfaces: [
this.nodeInterfaceRef(),
...((_b = options.interfaces) !== null && _b !== void 0 ? _b : []),
],
loadMany: (ids, context) => getDataloader(context).loadMany(ids),
extensions: {
getDataloader,
cacheResolved: typeof cacheResolved === "function" ? cacheResolved : cacheResolved && toKey,
},
};
this.objectType(ref, extendedOptions);
if (typeof nameOrRef !== "string") {
this.configStore.associateRefWithName(nameOrRef, name);
}
this.configStore.onTypeConfig(ref, (nodeConfig) => {
this.objectField(ref, "id", (t) => t.globalID({
...options.id,
nullable: false,
args: {},
resolve: async (parent, args, context, info) => ({
type: nodeConfig.name,
id: await options.id.resolve(parent, args, context, info),
}),
}));
});
return ref;
};
//# sourceMappingURL=schema-builder.js.map