|
| 1 | +import { |
| 2 | + type Enum, |
| 3 | + type Model, |
| 4 | + type Namespace, |
| 5 | + type Operation, |
| 6 | + type Program, |
| 7 | + type Scalar, |
| 8 | +} from "@typespec/compiler"; |
| 9 | +import { $ } from "@typespec/compiler/typekit"; |
| 10 | +import { |
| 11 | + MutationEngine, |
| 12 | + SimpleInterfaceMutation, |
| 13 | + SimpleIntrinsicMutation, |
| 14 | + SimpleLiteralMutation, |
| 15 | + SimpleUnionMutation, |
| 16 | + SimpleUnionVariantMutation, |
| 17 | +} from "@typespec/mutator-framework"; |
| 18 | +import { |
| 19 | + GraphQLEnumMemberMutation, |
| 20 | + GraphQLEnumMutation, |
| 21 | + GraphQLModelMutation, |
| 22 | + GraphQLModelPropertyMutation, |
| 23 | + GraphQLOperationMutation, |
| 24 | + GraphQLScalarMutation, |
| 25 | +} from "./mutations/index.js"; |
| 26 | +import { GraphQLMutationOptions } from "./options.js"; |
| 27 | + |
| 28 | +/** |
| 29 | + * Registry configuration for the GraphQL mutation engine. |
| 30 | + * Maps TypeSpec type kinds to their corresponding GraphQL mutation classes. |
| 31 | + */ |
| 32 | +const graphqlMutationRegistry = { |
| 33 | + // Custom GraphQL mutations for types we need to transform |
| 34 | + Enum: GraphQLEnumMutation, |
| 35 | + EnumMember: GraphQLEnumMemberMutation, |
| 36 | + Model: GraphQLModelMutation, |
| 37 | + ModelProperty: GraphQLModelPropertyMutation, |
| 38 | + Operation: GraphQLOperationMutation, |
| 39 | + Scalar: GraphQLScalarMutation, |
| 40 | + // Use Simple* classes from mutator-framework for types we don't customize |
| 41 | + Interface: SimpleInterfaceMutation, |
| 42 | + Union: SimpleUnionMutation, |
| 43 | + UnionVariant: SimpleUnionVariantMutation, |
| 44 | + String: SimpleLiteralMutation, |
| 45 | + Number: SimpleLiteralMutation, |
| 46 | + Boolean: SimpleLiteralMutation, |
| 47 | + Intrinsic: SimpleIntrinsicMutation, |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * GraphQL mutation engine that applies GraphQL-specific transformations |
| 52 | + * to TypeSpec types, such as name sanitization. |
| 53 | + */ |
| 54 | +export class GraphQLMutationEngine { |
| 55 | + /** |
| 56 | + * The underlying mutation engine configured with GraphQL-specific mutation classes. |
| 57 | + * Type is inferred from graphqlMutationRegistry to avoid complex generic constraints. |
| 58 | + */ |
| 59 | + private engine; |
| 60 | + |
| 61 | + constructor(program: Program, _namespace: Namespace) { |
| 62 | + const tk = $(program); |
| 63 | + this.engine = new MutationEngine(tk, graphqlMutationRegistry); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Mutate a model, applying GraphQL name sanitization. |
| 68 | + */ |
| 69 | + mutateModel(model: Model): GraphQLModelMutation { |
| 70 | + const mutation = this.engine.mutate(model, new GraphQLMutationOptions()); |
| 71 | + // Cast needed: engine returns base Mutation type, but registry ensures GraphQLModelMutation |
| 72 | + return mutation as unknown as GraphQLModelMutation; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Mutate an enum, applying GraphQL name sanitization. |
| 77 | + */ |
| 78 | + mutateEnum(enumType: Enum): GraphQLEnumMutation { |
| 79 | + const mutation = this.engine.mutate(enumType, new GraphQLMutationOptions()); |
| 80 | + // Cast needed: engine returns base Mutation type, but registry ensures GraphQLEnumMutation |
| 81 | + return mutation as unknown as GraphQLEnumMutation; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Mutate an operation, applying GraphQL name sanitization. |
| 86 | + */ |
| 87 | + mutateOperation(operation: Operation): GraphQLOperationMutation { |
| 88 | + const mutation = this.engine.mutate(operation, new GraphQLMutationOptions()); |
| 89 | + // Cast needed: engine returns base Mutation type, but registry ensures GraphQLOperationMutation |
| 90 | + return mutation as unknown as GraphQLOperationMutation; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Mutate a scalar, applying GraphQL name sanitization. |
| 95 | + */ |
| 96 | + mutateScalar(scalar: Scalar): GraphQLScalarMutation { |
| 97 | + const mutation = this.engine.mutate(scalar, new GraphQLMutationOptions()); |
| 98 | + // Cast needed: engine returns base Mutation type, but registry ensures GraphQLScalarMutation |
| 99 | + return mutation as unknown as GraphQLScalarMutation; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Creates a GraphQL mutation engine for the given program and namespace. |
| 105 | + */ |
| 106 | +export function createGraphQLMutationEngine( |
| 107 | + program: Program, |
| 108 | + namespace: Namespace, |
| 109 | +): GraphQLMutationEngine { |
| 110 | + return new GraphQLMutationEngine(program, namespace); |
| 111 | +} |
| 112 | + |
0 commit comments