UNPKG

@pothos/plugin-directives

Version:

Directive plugin for Pothos, enables using graphql-tools based directives with Pothos

203 lines (202 loc) 8.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { PothosDirectivesPlugin: function() { return PothosDirectivesPlugin; }, default: function() { return _default; } }); require("./global-types"); const _core = /*#__PURE__*/ _interop_require_wildcard(require("@pothos/core")); const _mockast = /*#__PURE__*/ _interop_require_default(require("./mock-ast")); _export_star(require("./types"), exports); function _export_star(from, to) { Object.keys(from).forEach(function(k) { if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { Object.defineProperty(to, k, { enumerable: true, get: function() { return from[k]; } }); } }); return from; } function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const pluginName = 'directives'; const _default = pluginName; class PothosDirectivesPlugin extends _core.BasePlugin { onOutputFieldConfig(fieldConfig) { var _fieldConfig_extensions, _fieldConfig_extensions1; const options = fieldConfig.pothosOptions; if (!options.directives && !((_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.directives)) { return fieldConfig; } return { ...fieldConfig, extensions: { ...fieldConfig.extensions, directives: this.normalizeDirectives(this.mergeDirectives((_fieldConfig_extensions1 = fieldConfig.extensions) === null || _fieldConfig_extensions1 === void 0 ? void 0 : _fieldConfig_extensions1.directives, options.directives)) } }; } onInputFieldConfig(fieldConfig) { var _fieldConfig_extensions, _fieldConfig_extensions1; const options = fieldConfig.pothosOptions; if (!options.directives && !((_fieldConfig_extensions = fieldConfig.extensions) === null || _fieldConfig_extensions === void 0 ? void 0 : _fieldConfig_extensions.directives)) { return fieldConfig; } return { ...fieldConfig, extensions: { ...fieldConfig.extensions, directives: this.normalizeDirectives(this.mergeDirectives((_fieldConfig_extensions1 = fieldConfig.extensions) === null || _fieldConfig_extensions1 === void 0 ? void 0 : _fieldConfig_extensions1.directives, options.directives)) } }; } onEnumValueConfig(valueConfig) { var _valueConfig_extensions, _valueConfig_extensions1; const options = valueConfig.pothosOptions; if (!options.directives && !((_valueConfig_extensions = valueConfig.extensions) === null || _valueConfig_extensions === void 0 ? void 0 : _valueConfig_extensions.directives)) { return valueConfig; } return { ...valueConfig, extensions: { ...valueConfig.extensions, directives: this.normalizeDirectives(this.mergeDirectives((_valueConfig_extensions1 = valueConfig.extensions) === null || _valueConfig_extensions1 === void 0 ? void 0 : _valueConfig_extensions1.directives, options.directives)) } }; } onTypeConfig(typeConfig) { var _typeConfig_extensions, _typeConfig_extensions1; const options = typeConfig.pothosOptions; if (!options.directives && !((_typeConfig_extensions = typeConfig.extensions) === null || _typeConfig_extensions === void 0 ? void 0 : _typeConfig_extensions.directives)) { return typeConfig; } return { ...typeConfig, extensions: { ...typeConfig.extensions, directives: this.normalizeDirectives(this.mergeDirectives((_typeConfig_extensions1 = typeConfig.extensions) === null || _typeConfig_extensions1 === void 0 ? void 0 : _typeConfig_extensions1.directives, options.directives)) } }; } afterBuild(schema) { var _schema_extensions; var _schema_extensions_directives; schema.extensions = { ...schema.extensions, directives: this.normalizeDirectives(this.mergeDirectives((_schema_extensions_directives = (_schema_extensions = schema.extensions) === null || _schema_extensions === void 0 ? void 0 : _schema_extensions.directives) !== null && _schema_extensions_directives !== void 0 ? _schema_extensions_directives : {}, this.options.schemaDirectives)) }; (0, _mockast.default)(schema); return schema; } mergeDirectives(left, right) { if (!(left && right)) { return left || right; } return [ ...Array.isArray(left) ? left : Object.keys(left).map((name)=>({ name, args: left[name] })), ...Array.isArray(right) ? right : Object.keys(right).map((name)=>({ name, args: right[name] })) ]; } normalizeDirectives(directives) { var _this_builder_options_directives; if ((_this_builder_options_directives = this.builder.options.directives) === null || _this_builder_options_directives === void 0 ? void 0 : _this_builder_options_directives.useGraphQLToolsUnorderedDirectives) { if (!Array.isArray(directives)) { return directives; } return directives.reduce((obj, directive)=>{ if (obj[directive.name]) { var _directive_args; obj[directive.name].push((_directive_args = directive.args) !== null && _directive_args !== void 0 ? _directive_args : {}); } else { var _directive_args1; obj[directive.name] = [ (_directive_args1 = directive.args) !== null && _directive_args1 !== void 0 ? _directive_args1 : {} ]; } return obj; }, {}); } if (Array.isArray(directives)) { return directives; } return Object.keys(directives).map((name)=>({ name, args: directives[name] })); } } _core.default.registerPlugin(pluginName, PothosDirectivesPlugin, { v3: (options)=>({ useGraphQLToolsUnorderedDirectives: undefined, directives: { useGraphQLToolsUnorderedDirectives: options.useGraphQLToolsUnorderedDirectives } }) }); //# sourceMappingURL=index.js.map