Skip to content

Instantly share code, notes, and snippets.

@gynekolog
Last active August 7, 2024 09:34
Show Gist options
  • Save gynekolog/5c84f6af87bcf2a1d4b623fe44a57dc0 to your computer and use it in GitHub Desktop.
Save gynekolog/5c84f6af87bcf2a1d4b623fe44a57dc0 to your computer and use it in GitHub Desktop.
Usage: jscodeshift -t ./remove-apollo-suspense-queries.ts ./your-file.ts
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