From d90a14e833662ea023dd797c39e8e9eb587b7388 Mon Sep 17 00:00:00 2001 From: Johannes Obermair Date: Wed, 27 Nov 2024 10:26:02 +0100 Subject: [PATCH] Add script to update the formatError implementation --- src/v8/update-graphql-format-error.ts | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/v8/update-graphql-format-error.ts diff --git a/src/v8/update-graphql-format-error.ts b/src/v8/update-graphql-format-error.ts new file mode 100644 index 0000000..12b15e7 --- /dev/null +++ b/src/v8/update-graphql-format-error.ts @@ -0,0 +1,37 @@ +import { Project, SyntaxKind } from "ts-morph"; + +/** + * From + * + * if (error instanceof ValidationError) { + * return new ValidationError("Invalid request."); + * } + * + * to + * + * if (error.extensions?.code === "GRAPHQL_VALIDATION_FAILED") { + * return new ValidationError("Invalid request."); + * } + */ +export default async function updateGraphQLFormatError() { + const project = new Project({ tsConfigFilePath: "./api/tsconfig.json" }); + + const sourceFile = project.getSourceFile("api/src/app.module.ts"); + + if (!sourceFile) { + throw new Error("app.module.ts not found"); + } + + // Change the import + sourceFile.getImportDeclaration((importDeclaration) => importDeclaration.getModuleSpecifierValue() === "apollo-server-express")?.remove(); + sourceFile.addImportDeclaration({ namedImports: ["ValidationError"], moduleSpecifier: "@nestjs/apollo" }); + + // Update the if statement + sourceFile.getDescendantsOfKind(SyntaxKind.BinaryExpression).forEach((node) => { + if (node.getText() === "error instanceof ValidationError") { + node.replaceWithText(`error.extensions?.code === "GRAPHQL_VALIDATION_FAILED"`); + } + }); + + await sourceFile.save(); +}