Skip to content

Commit

Permalink
Add script to update the formatError implementation (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair authored Jan 7, 2025
1 parent 71a7f32 commit 2d54a11
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/v8/update-graphql-format-error.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 2d54a11

Please sign in to comment.