@giraphql/plugin-simple-objects
Version:
A GiraphQL plugin for defining objects and interfaces without ts definitions for those types
52 lines (51 loc) • 1.94 kB
JavaScript
import './global-types.js';
import SchemaBuilder, { BasePlugin, InterfaceRef, ObjectRef, } from '@giraphql/core';
const pluginName = "simpleObjects";
export default pluginName;
export class GiraphQLSimpleObjectsPlugin extends BasePlugin {
}
SchemaBuilder.registerPlugin(pluginName, GiraphQLSimpleObjectsPlugin);
const proto = SchemaBuilder.prototype;
proto.simpleObject = function simpleObject(name, options) {
const ref = new ObjectRef(name);
if (options.fields) {
const originalFields = options.fields;
// eslint-disable-next-line no-param-reassign
options.fields = (t) => {
const fields = originalFields(t);
Object.keys(fields).forEach((key) => {
this.configStore.onFieldUse(fields[key], (config) => {
if (config.kind === "Object") {
// eslint-disable-next-line no-param-reassign
config.resolve = (parent) => parent[key];
}
});
});
return fields;
};
}
this.objectType(ref, options);
return ref;
};
proto.simpleInterface = function simpleInterface(name, options) {
const ref = new InterfaceRef(name);
if (options.fields) {
const originalFields = options.fields;
// eslint-disable-next-line no-param-reassign
options.fields = (t) => {
const fields = originalFields(t);
Object.keys(fields).forEach((key) => {
this.configStore.onFieldUse(fields[key], (config) => {
if (config.kind === "Interface") {
// eslint-disable-next-line no-param-reassign
config.resolve = (parent) => parent[key];
}
});
});
return fields;
};
}
this.interfaceType(ref, options);
return ref;
};
//# sourceMappingURL=index.js.map