UNPKG

@pothos/plugin-add-graphql

Version:

A Pothos plugin for adding existing GraphQL types to a Pothos schema

299 lines (298 loc) 14.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("./global-types"); const _core = /*#__PURE__*/ _interop_require_wildcard(require("@pothos/core")); const _graphql = require("graphql"); const _utils = require("./utils"); 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 proto = _core.default.prototype; function resolveNullableOutputRef(builder, type) { if ((0, _graphql.isNonNullType)(type)) { throw new Error('Expected a nullable type'); } if ((0, _graphql.isListType)(type)) { const nullable = !(0, _graphql.isNonNullType)(type.ofType); const listType = nullable ? type.ofType : type.ofType.ofType; return new _core.ListRef(resolveNullableOutputRef(builder, listType), nullable); } (0, _utils.addReferencedType)(builder, type); return type.name; } function resolveNullableInputRef(builder, type) { if ((0, _graphql.isNonNullType)(type)) { throw new Error('Expected a nullable type'); } if ((0, _graphql.isListType)(type)) { const required = (0, _graphql.isNonNullType)(type.ofType); const listType = required ? type.ofType.ofType : type.ofType; return new _core.InputListRef(resolveNullableInputRef(builder, listType), required); } (0, _utils.addReferencedType)(builder, type); return type.name; } function resolveOutputType(builder, type) { const isNullable = !(0, _graphql.isNonNullType)(type); const nonNullable = (0, _graphql.isNonNullType)(type) ? type.ofType : type; const typeRef = resolveNullableOutputRef(builder, nonNullable); return { type: typeRef, nullable: isNullable }; } function resolveInputType(builder, type) { const isNullable = !(0, _graphql.isNonNullType)(type); const nonNullable = (0, _graphql.isNonNullType)(type) ? type.ofType : type; const typeRef = resolveNullableInputRef(builder, nonNullable); return { type: typeRef, required: !isNullable }; } proto.addGraphQLObject = function addGraphQLObject(type, { fields, extensions, ...options } = {}) { var _type_description, _type_astNode; const typeOptions = { ...options, description: (_type_description = type.description) !== null && _type_description !== void 0 ? _type_description : undefined, isTypeOf: type.isTypeOf, extensions: { ...type.extensions, ...extensions }, astNode: (_type_astNode = type.astNode) !== null && _type_astNode !== void 0 ? _type_astNode : undefined, interfaces: ()=>type.getInterfaces().map((i)=>resolveNullableOutputRef(this, i)), fields: (t)=>{ const existingFields = type.getFields(); var _fields; const newFields = (_fields = fields === null || fields === void 0 ? void 0 : fields(t)) !== null && _fields !== void 0 ? _fields : {}; const combinedFields = { ...newFields }; for (const [fieldName, field] of Object.entries(existingFields)){ if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } const args = {}; for (const { name, ...arg } of field.args){ const input = resolveInputType(this, arg.type); var _arg_description, _arg_deprecationReason, _arg_astNode; args[name] = t.arg({ ...input, description: (_arg_description = arg.description) !== null && _arg_description !== void 0 ? _arg_description : undefined, deprecationReason: (_arg_deprecationReason = arg.deprecationReason) !== null && _arg_deprecationReason !== void 0 ? _arg_deprecationReason : undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, astNode: (_arg_astNode = arg.astNode) !== null && _arg_astNode !== void 0 ? _arg_astNode : undefined }); } var _field_description, _field_deprecationReason, _field_astNode, _field_resolve; combinedFields[fieldName] = t.field({ ...resolveOutputType(this, field.type), args, description: (_field_description = field.description) !== null && _field_description !== void 0 ? _field_description : undefined, deprecationReason: (_field_deprecationReason = field.deprecationReason) !== null && _field_deprecationReason !== void 0 ? _field_deprecationReason : undefined, extensions: field.extensions, astNode: (_field_astNode = field.astNode) !== null && _field_astNode !== void 0 ? _field_astNode : undefined, resolve: (_field_resolve = field.resolve) !== null && _field_resolve !== void 0 ? _field_resolve : _graphql.defaultFieldResolver, ...field.subscribe ? { subscribe: field.subscribe } : {} }); } return combinedFields; } }; switch(type.name){ case 'Query': this.queryType(typeOptions); return 'Query'; case 'Mutation': this.mutationType(typeOptions); return 'Mutation'; case 'Subscription': this.subscriptionType(typeOptions); return 'Subscription'; default: var _options_name; return this.objectRef((_options_name = options === null || options === void 0 ? void 0 : options.name) !== null && _options_name !== void 0 ? _options_name : type.name).implement(typeOptions); } }; proto.addGraphQLInterface = function addGraphQLInterface(type, { fields, extensions, ...options } = {}) { var _options_name; const ref = this.interfaceRef((_options_name = options === null || options === void 0 ? void 0 : options.name) !== null && _options_name !== void 0 ? _options_name : type.name); var _type_description, _type_astNode; ref.implement({ ...options, description: (_type_description = type.description) !== null && _type_description !== void 0 ? _type_description : undefined, resolveType: type.resolveType, extensions: { ...type.extensions, ...extensions }, astNode: (_type_astNode = type.astNode) !== null && _type_astNode !== void 0 ? _type_astNode : undefined, interfaces: ()=>type.getInterfaces().map((i)=>resolveNullableOutputRef(this, i)), fields: (t)=>{ const existingFields = type.getFields(); var _fields; const newFields = (_fields = fields === null || fields === void 0 ? void 0 : fields(t)) !== null && _fields !== void 0 ? _fields : {}; const combinedFields = { ...newFields }; for (const [fieldName, field] of Object.entries(existingFields)){ if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } const args = {}; for (const { name, ...arg } of field.args){ var _arg_description, _arg_deprecationReason, _arg_astNode; args[name] = t.arg({ ...resolveInputType(this, arg.type), description: (_arg_description = arg.description) !== null && _arg_description !== void 0 ? _arg_description : undefined, deprecationReason: (_arg_deprecationReason = arg.deprecationReason) !== null && _arg_deprecationReason !== void 0 ? _arg_deprecationReason : undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, astNode: (_arg_astNode = arg.astNode) !== null && _arg_astNode !== void 0 ? _arg_astNode : undefined }); } var _field_description, _field_deprecationReason, _field_astNode; combinedFields[fieldName] = t.field({ ...resolveOutputType(this, field.type), args, description: (_field_description = field.description) !== null && _field_description !== void 0 ? _field_description : undefined, deprecationReason: (_field_deprecationReason = field.deprecationReason) !== null && _field_deprecationReason !== void 0 ? _field_deprecationReason : undefined, resolve: field.resolve, extensions: field.extensions, astNode: (_field_astNode = field.astNode) !== null && _field_astNode !== void 0 ? _field_astNode : undefined }); } return combinedFields; } }); return ref; }; proto.addGraphQLUnion = function addGraphQLUnion(type, { types, extensions, ...options } = {}) { var _options_name, _type_description, _type_astNode; return this.unionType((_options_name = options === null || options === void 0 ? void 0 : options.name) !== null && _options_name !== void 0 ? _options_name : type.name, { ...options, description: (_type_description = type.description) !== null && _type_description !== void 0 ? _type_description : undefined, resolveType: type.resolveType, extensions: { ...type.extensions, ...extensions }, astNode: (_type_astNode = type.astNode) !== null && _type_astNode !== void 0 ? _type_astNode : undefined, types: types !== null && types !== void 0 ? types : type.getTypes().map((t)=>resolveNullableOutputRef(this, t)) }); }; proto.addGraphQLEnum = function addGraphQLEnum(type, { values, extensions, ...options } = {}) { const newValues = values !== null && values !== void 0 ? values : type.getValues().reduce((acc, value)=>{ var _value_description, _value_deprecationReason, _value_astNode; acc[value.name] = { value: value.value, description: (_value_description = value.description) !== null && _value_description !== void 0 ? _value_description : undefined, deprecationReason: (_value_deprecationReason = value.deprecationReason) !== null && _value_deprecationReason !== void 0 ? _value_deprecationReason : undefined, extensions: value.extensions, astNode: (_value_astNode = value.astNode) !== null && _value_astNode !== void 0 ? _value_astNode : undefined }; return acc; }, {}); var _options_name, _type_description, _type_astNode; const ref = this.enumType((_options_name = options === null || options === void 0 ? void 0 : options.name) !== null && _options_name !== void 0 ? _options_name : type.name, { ...options, description: (_type_description = type.description) !== null && _type_description !== void 0 ? _type_description : undefined, extensions: { ...type.extensions, ...extensions }, astNode: (_type_astNode = type.astNode) !== null && _type_astNode !== void 0 ? _type_astNode : undefined, values: newValues }); return ref; }; proto.addGraphQLInput = function addGraphQLInput(type, { name = type.name, fields, extensions, ...options } = {}) { const ref = this.inputRef(name); var _type_description, _type_astNode; return ref.implement({ ...options, description: (_type_description = type.description) !== null && _type_description !== void 0 ? _type_description : undefined, extensions: { ...type.extensions, ...extensions }, astNode: (_type_astNode = type.astNode) !== null && _type_astNode !== void 0 ? _type_astNode : undefined, isOneOf: type.isOneOf, fields: (t)=>{ const existingFields = type.getFields(); var _fields; const newFields = (_fields = fields === null || fields === void 0 ? void 0 : fields(t)) !== null && _fields !== void 0 ? _fields : {}; const combinedFields = { ...newFields }; for (const [fieldName, field] of Object.entries(existingFields)){ if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } var _field_description, _field_astNode; combinedFields[fieldName] = t.field({ ...resolveInputType(this, field.type), description: (_field_description = field.description) !== null && _field_description !== void 0 ? _field_description : undefined, defaultValue: field.defaultValue, extensions: field.extensions, astNode: (_field_astNode = field.astNode) !== null && _field_astNode !== void 0 ? _field_astNode : undefined }); } return combinedFields; } }); }; //# sourceMappingURL=schema-builder.js.map