-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to update the formatError implementation (#38)
- Loading branch information
1 parent
71a7f32
commit 2d54a11
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |