import type { PluginFunction, Types } from "@graphql-codegen/plugin-helpers"; import type { NamingConvention } from "@graphql-codegen/visitor-plugin-common"; import { convertFactory } from "@graphql-codegen/visitor-plugin-common"; import type { GraphQLInputType, GraphQLNamedType, GraphQLOutputType, GraphQLSchema, } from "graphql"; import { getNamedType, getNullableType, isEnumType, isInputObjectType, isInterfaceType, isListType, isNonNullType, isObjectType, isScalarType, isSpecifiedScalarType, isUnionType, } from "graphql"; export interface GrafastTypesPluginConfig { grafastModule?: string; overridesFile?: string; namingConvention?: NamingConvention; } class GrafastGenerator { private schema: GraphQLSchema; private config: GrafastTypesPluginConfig; private types: ReadonlyArray; private convertName: ReturnType; constructor( schema: GraphQLSchema, _documents: Types.DocumentFile[], config: GrafastTypesPluginConfig, ) { this.schema = schema; this.config = config; this.types = this.schema .toConfig() .types.filter( (t) => !t.name.startsWith("__") && !isSpecifiedScalarType(t), ); this.convertName = convertFactory({ namingConvention: config.namingConvention, }); } private _get( type: GraphQLNamedType, property: "nullable" | "source" | "specifier", fallback = "Step", ) { return `Get<${JSON.stringify(type.name)}, ${JSON.stringify(property)}, ${fallback}>`; } private nullable(type: GraphQLNamedType) { return this._get(type, "nullable"); } private source(type: GraphQLNamedType) { return this._get( type, "source", `NonNullStep<${this._get(type, "nullable")}>`, ); } private specifier(type: GraphQLNamedType) { return this._get(type, "specifier", `StepData<${this.source(type)}>`); } private expect(type: GraphQLOutputType): string { if (isNonNullType(type)) { return `NonNullStep<${this.expect(type.ofType)}>`; } else if (isListType(type)) { return `Get<${JSON.stringify(getNamedType(type.ofType).name)}, "list", ListOfStep<${this.expect(type.ofType)}>>`; } else { return this.nullable(type); } } private getInterfacePlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isInterfaceType(type)) continue; lines.push(`\ ${type.name}?: InterfacePlan< ${this.specifier(type)}, ${this.source(type)} >;`); } return lines; } private getUnionPlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isUnionType(type)) continue; lines.push(`\ ${type.name}?: UnionPlan< ${this.specifier(type)}, ${this.source(type)} >;`); } return lines; } private getScalarPlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isScalarType(type) && !isSpecifiedScalarType(type)) continue; lines.push(` ${type.name}?: ScalarPlan;`); } return lines; } private getEnumPlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isEnumType(type)) continue; lines.push(` ${type.name}?: Omit & { values?: { ${type .getValues() .map((v) => ` ${v.name}?: EnumValueInput;`) .join("\n")} } };`); } return lines; } private getObjectPlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isObjectType(type)) continue; lines.push(` ${type.name}?: Omit, 'plans'> & { plans?: { ${Object.entries(type.getFields()) .map( ([fieldName, fieldSpec]) => ` ${fieldName}?: FieldPlan<${this.source(type)}, ${fieldSpec.args.length > 0 ? `${type.name}${this.convertName(fieldName)}Args` : `NoArguments`}, ${this.expect(fieldSpec.type)}>;`, ) .join("\n")} } };`); } return lines; } private getInputType(t: GraphQLInputType): string { const nn = isNonNullType(t); const nullable = getNullableType(t); const inner = isListType(nullable) ? `ReadonlyArray<${this.getInputType(nullable.ofType)}>` : isScalarType(nullable) ? `Scalars['${nullable.name}']` : this.convertName(nullable.name); if (nn) { return inner; } else { return `Maybe<${inner}>`; } } private getInputObjectPlans(): string[] { const lines: string[] = []; for (const type of this.types) { if (!isInputObjectType(type)) continue; lines.push(` ${type.name}?: Omit & { plans?: { ${Object.entries(type.getFields()) .map( ([fieldName, field]) => ` ${fieldName}?: InputFieldPlan;`, ) .join("\n")} } };`); } return lines; } public generate(): string { return ( [ "// Generated GraphQL SDK (auto-generated – do not edit)", "", `import type { EnumPlan, EnumValueInput, FieldPlan, InputFieldPlan, GrafastSchemaConfig, InputObjectPlan, InterfacePlan, ObjectPlan, ScalarPlan, Step, UnionPlan, StepRepresentingList } from '${this.config.grafastModule ?? "grafast"}';`, `import { makeGrafastSchema } from '${this.config.grafastModule ?? "grafast"}';`, this.config.overridesFile ? `import type { Overrides } from '${this.config.overridesFile ?? "./grafastTypeOverrides.ts"}';` : `// Provide 'overridesFile' via your config to specify your own overrides. type Overrides = {}`, "", `\ type NoArguments = Record; type NonNullStep = TStep & Step ? NonNullable : any>; type ListOfStep = StepRepresentingList ? U : any, TStep>; type StepData = TStep extends Step ? U : never; type Get< TTypeName extends string, TProp extends string, TFallback = any, > = TTypeName extends keyof Overrides ? TProp extends keyof Overrides[TTypeName] ? NonNullable : TFallback : TFallback;`, "", `export interface TypedGrafastSchemaSpec extends Omit {`, ` objects?: {`, ...this.getObjectPlans(), ` }`, ` interfaces?: {`, ...this.getInterfacePlans(), ` }`, ` unions?: {`, ...this.getUnionPlans(), ` }`, ` inputObjects?: {`, ...this.getInputObjectPlans(), ` }`, ` scalars?: {`, ...this.getScalarPlans(), ` }`, ` enums?: {`, ...this.getEnumPlans(), ` }`, `};`, "", `export function typedMakeGrafastSchema(spec: TypedGrafastSchemaSpec) { return makeGrafastSchema(spec); }`, ].join("\n") + "\n" ); } } export const plugin: PluginFunction = ( schema, documents, config = {}, ) => { const generator = new GrafastGenerator(schema, documents, config); return generator.generate(); };