import './global-types.js'; import SchemaBuilder, { type ArgumentRef, type EnumRef, type EnumValueConfigMap, type InputFieldRef, InputListRef, type InputType, type InputTypeParam, ListRef, MutationFieldBuilder, ObjectFieldBuilder, type ObjectParam, type ObjectRef, type OutputType, PothosSchemaError, QueryFieldBuilder, type RootName, type SchemaTypes, SubscriptionFieldBuilder, type TypeParam, } from '@pothos/core'; import { defaultFieldResolver, type GraphQLEnumType, type GraphQLInputObjectType, type GraphQLInputType, type GraphQLInterfaceType, type GraphQLObjectType, type GraphQLOutputType, type GraphQLType, type GraphQLUnionType, isListType, isNonNullType, } from 'graphql'; import type { AddGraphQLEnumTypeOptions, AddGraphQLInputTypeOptions, AddGraphQLInterfaceTypeOptions, AddGraphQLObjectFieldsShape, AddGraphQLObjectTypeOptions, AddGraphQLUnionTypeOptions, EnumValuesWithShape, } from './types.js'; import { addReferencedType, importedRootKinds, importedTypes, preserveInputDefault, } from './utils.js'; const proto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder; function resolveNullableOutputRef( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLOutputType, ): OutputType { if (isNonNullType(type)) { throw new Error('Expected a nullable type'); } if (isListType(type)) { const nullable = !isNonNullType(type.ofType); const listType = nullable ? type.ofType : type.ofType.ofType; return new ListRef(resolveNullableOutputRef(builder, listType), nullable); } addReferencedType(builder, type); return type.name as OutputType; } function resolveNullableInputRef( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLType, ): InputTypeParam { if (isNonNullType(type)) { throw new Error('Expected a nullable type'); } if (isListType(type)) { const required = isNonNullType(type.ofType); const listType = required ? type.ofType.ofType : type.ofType; return new InputListRef(resolveNullableInputRef(builder, listType), required); } addReferencedType(builder, type); return type.name as InputType; } function resolveOutputType( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLOutputType, ): { type: TypeParam; nullable: boolean } { const isNullable = !isNonNullType(type); const nonNullable = isNonNullType(type) ? type.ofType : type; const typeRef = resolveNullableOutputRef(builder, nonNullable); return { type: typeRef, nullable: isNullable, }; } function resolveInputType( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLInputType, ): { type: InputTypeParam; required: boolean } { const isNullable = !isNonNullType(type); const nonNullable = isNonNullType(type) ? type.ofType : type; const typeRef = resolveNullableInputRef(builder, nonNullable); return { type: typeRef, required: !isNullable, }; } const defaultRootNames: RootName[] = ['Query', 'Mutation', 'Subscription']; function inferRootKind(name: string) { return defaultRootNames.find((rootName) => rootName === name); } function resolveObjectFields( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLObjectType, fields?: AddGraphQLObjectFieldsShape, ) { return (t: PothosSchemaTypes.ObjectFieldBuilder) => { const existingFields = type.getFields(); const newFields = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; for (const [fieldName, field] of Object.entries(existingFields)) { if (Object.hasOwn(newFields, fieldName) && newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } const args: Record> = {}; for (const { name, ...arg } of field.args) { const input = resolveInputType(builder, arg.type); args[name] = preserveInputDefault( t.arg({ ...input, description: arg.description ?? undefined, deprecationReason: arg.deprecationReason ?? undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, astNode: arg.astNode ?? undefined, }), arg, ); } combinedFields[fieldName] = t.field({ ...resolveOutputType(builder, field.type), args, description: field.description ?? undefined, deprecationReason: field.deprecationReason ?? undefined, extensions: field.extensions, astNode: field.astNode ?? undefined, resolve: (field.resolve ?? defaultFieldResolver) as never, ...(field.subscribe ? { subscribe: field.subscribe } : {}), }); } return combinedFields as {}; }; } export function mergeGraphQLObjectFields( builder: PothosSchemaTypes.SchemaBuilder, type: GraphQLObjectType, rootKind: RootName, ) { const target = builder as never as PothosSchemaTypes.SchemaBuilder; const fields = resolveObjectFields(target, type); const { kind } = target.configStore.getTypeConfig(type.name); if ((kind === 'Query' || kind === 'Mutation' || kind === 'Subscription') && kind !== rootKind) { throw new PothosSchemaError( `Can not merge the imported ${rootKind} root ${type.name} into the ${kind} root with the same name`, ); } switch (kind) { case 'Query': target.configStore.addFieldDefaults(type.name as never, () => fields(new QueryFieldBuilder(target) as never), ); break; case 'Mutation': target.configStore.addFieldDefaults(type.name as never, () => fields(new MutationFieldBuilder(target) as never), ); break; case 'Subscription': target.configStore.addFieldDefaults(type.name as never, () => fields(new SubscriptionFieldBuilder(target) as never), ); break; case 'Object': target.configStore.addFieldDefaults(type.name as never, () => fields(new ObjectFieldBuilder(target)), ); break; default: throw new PothosSchemaError( `Can not merge the imported root ${type.name} into the ${kind} type with the same name`, ); } } proto.addGraphQLObject = function addGraphQLObject( type: GraphQLObjectType, { fields, extensions, rootKind, ...options }: AddGraphQLObjectTypeOptions = {}, ) { const name = options.name ?? type.name; const typeOptions = { ...options, description: type.description ?? undefined, isTypeOf: type.isTypeOf as never, extensions: { ...type.extensions, ...extensions }, astNode: type.astNode ?? undefined, interfaces: () => type.getInterfaces().map((i) => resolveNullableOutputRef(this, i)) as [], fields: resolveObjectFields(this, type, fields), }; const root = rootKind === undefined ? inferRootKind(type.name) : (rootKind ?? undefined); if (name === type.name) { importedTypes(this).add(type); } if (rootKind !== undefined) { importedRootKinds(this).set(name, rootKind); } switch (root) { case 'Query': this.queryType({ ...typeOptions, name } as never); return name as never; case 'Mutation': this.mutationType({ ...typeOptions, name } as never); return name as never; case 'Subscription': this.subscriptionType({ ...typeOptions, name } as never); return name as never; default: return this.objectRef(name).implement(typeOptions as never); } }; proto.addGraphQLInterface = function addGraphQLInterface( type: GraphQLInterfaceType, { fields, extensions, ...options }: AddGraphQLInterfaceTypeOptions = {}, ) { const ref = this.interfaceRef(options?.name ?? type.name); ref.implement({ ...options, description: type.description ?? undefined, resolveType: type.resolveType as never, extensions: { ...type.extensions, ...extensions }, astNode: type.astNode ?? undefined, interfaces: () => type.getInterfaces().map((i) => resolveNullableOutputRef(this, i)) as [], fields: (t) => { const existingFields = type.getFields(); const newFields = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; for (const [fieldName, field] of Object.entries(existingFields)) { if (Object.hasOwn(newFields, fieldName) && newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } const args: Record> = {}; for (const { name, ...arg } of field.args) { args[name] = preserveInputDefault( t.arg({ ...resolveInputType(this, arg.type), description: arg.description ?? undefined, deprecationReason: arg.deprecationReason ?? undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, astNode: arg.astNode ?? undefined, }), arg, ); } combinedFields[fieldName] = t.field({ ...resolveOutputType(this, field.type), args, description: field.description ?? undefined, deprecationReason: field.deprecationReason ?? undefined, resolve: field.resolve as never, extensions: field.extensions, astNode: field.astNode ?? undefined, }); } return combinedFields as {}; }, }); return ref; }; proto.addGraphQLUnion = function addGraphQLUnion( type: GraphQLUnionType, { types, extensions, ...options }: AddGraphQLUnionTypeOptions> = {}, ) { return this.unionType, Shape>(options?.name ?? type.name, { ...options, description: type.description ?? undefined, resolveType: type.resolveType as never, extensions: { ...type.extensions, ...extensions }, astNode: type.astNode ?? undefined, types: types ?? (type.getTypes().map((t) => resolveNullableOutputRef(this, t)) as []), }); }; proto.addGraphQLEnum = function addGraphQLEnum( type: GraphQLEnumType, { values, extensions, ...options }: AddGraphQLEnumTypeOptions> = {}, ) { const newValues = values ?? type.getValues().reduce>((acc, value) => { acc[value.name] = { value: value.value as never, description: value.description ?? undefined, deprecationReason: value.deprecationReason ?? undefined, extensions: value.extensions, astNode: value.astNode ?? undefined, }; return acc; }, {}); const ref: EnumRef = this.enumType< never, EnumValuesWithShape >( (options?.name ?? type.name) as never, { ...options, description: type.description ?? undefined, extensions: { ...type.extensions, ...extensions }, astNode: type.astNode ?? undefined, values: newValues, } as never, ); return ref; }; proto.addGraphQLInput = function addGraphQLInput( type: GraphQLInputObjectType, { name = type.name, fields, extensions, ...options }: AddGraphQLInputTypeOptions = {}, ) { const ref = this.inputRef(name); return ref.implement({ ...options, description: type.description ?? undefined, extensions: { ...type.extensions, ...extensions }, astNode: type.astNode ?? undefined, isOneOf: type.isOneOf, fields: (t) => { const existingFields = type.getFields(); const newFields: Record | null> = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; for (const [fieldName, field] of Object.entries(existingFields)) { if (Object.hasOwn(newFields, fieldName) && newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } continue; } combinedFields[fieldName] = preserveInputDefault( t.field({ ...resolveInputType(this, field.type), description: field.description ?? undefined, deprecationReason: field.deprecationReason ?? undefined, defaultValue: field.defaultValue, extensions: field.extensions, astNode: field.astNode ?? undefined, }), field, ); } return combinedFields as never; }, }) as never; };