Last active
August 7, 2024 09:34
-
-
Save gynekolog/5c84f6af87bcf2a1d4b623fe44a57dc0 to your computer and use it in GitHub Desktop.
Usage: jscodeshift -t ./remove-apollo-suspense-queries.ts ./your-file.ts
This file contains hidden or 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
import type { API, FileInfo } from "jscodeshift"; | |
// @graphql-codegen/typescript-react-apollo package generates SuspenseQuery functions that are not supported in used apollo versions. | |
// The codegen package does not provide an option to disable this feature, so we need to remove the generated SuspenseQuery functions manually. | |
export default function transform(file: FileInfo, api: API): string | undefined { | |
const j = api.jscodeshift.withParser("ts"); | |
const root = j(file.source); | |
// Find and comment out all function declarations that end with "SuspenseQuery" | |
root | |
.find(j.ExportNamedDeclaration) | |
.filter(({ node: { declaration } }) => | |
Boolean(declaration?.type === "FunctionDeclaration" && declaration.id?.name.endsWith("SuspenseQuery")) | |
) | |
.forEach(path => j(path).remove()); | |
// Find and comment out all type alias declarations that contain "SuspenseQuery" | |
root | |
.find(j.ExportNamedDeclaration) | |
.filter(({ node: { declaration } }) => | |
Boolean(declaration?.type === "TSTypeAliasDeclaration" && declaration.id.name.includes("SuspenseQuery")) | |
) | |
.forEach(path => j(path).remove()); | |
return root.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment