@giraphql/plugin-dataloader
Version:
A GiraphQL plugin for attaching dataloader to object types
104 lines • 4.42 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = __importDefault(require("@giraphql/core"));
const interface_1 = require("./refs/interface");
const object_1 = require("./refs/object");
const union_1 = require("./refs/union");
const _1 = require(".");
const schemaBuilderProto = core_1.default.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 = (0, _1.dataloaderGetter)(loaderOptions, load, toKey, sort);
const ref = new object_1.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 = (0, _1.dataloaderGetter)(loaderOptions, load, toKey, sort);
const ref = new interface_1.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 = (0, _1.dataloaderGetter)(loaderOptions, load, toKey, sort);
const ref = new union_1.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 = (0, _1.dataloaderGetter)(loaderOptions, load, toKey, sort);
const ref = new object_1.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
;