@apollo/client-codemod-migrate-3-to-4
v1.0.2
Published
Apollo Client Codemod to migrate from version 3 to version 4
Readme
Using this codemod
You can run this codemod with the following command:
npx @apollo/client-codemod-migrate-3-to-4 --parser ts file1.ts file2.tsif you want to only run a specific codemod:
npx @apollo/client-codemod-migrate-3-to-4 --parser ts file1.ts file2.ts --codemod importsAvailable codemods:
In the order they will be applied per default if you don't manually specify a codemod:
legacyEntryPoints: Moves imports from old legacy entry points like@apollo/client/main.cjsor@apollo/client/react/react.cjsto the new entry points like@apollo/clientor@apollo/client/react.imports: Moves imports that have been moved or renamed in Apollo Client 4. Also moves types into namespace imports where applicable.links: Movessplit,from,concatandemptyonto theApolloLinknamespace, changes funtion link invocations likecreateHttpLink(...)to their class equivalents like (new HttpLink(...)). Does not changesetContext((operation, prevContext) => {})tonew ContextLink((prevContext, operation) => {})- this requires manual intervention, as the order of callback arguments is flipped and this is not reliable codemoddable.removals: Points all imports of values or types that have been removed in Apollo Client 4 to the@apollo/client/v4-migrationentry point. That entry point contains context for each removal, oftentimes with migration instructions.clientSetup: Applies the following steps from the migration guide to your Apollo Client setup code- Moves
uri,headersandcredentialsto thelinkoption and creates a newHttpLinkinstance - Moves
nameandversioninto aclientAwarenessoption - Adds a
localStateoption with a newLocalStateinstance, movesresolvers, and removestypeDefsandfragmentMatcheroptions - Changes the
connectToDevToolsoption todevtools.enabled - Renames
disableNetworkFetchestoprioritizeCacheValues - If
dataMaskingis enabled, adds a template for global type augmentation to re-enable data masking types - Adds the
incrementalHandleroption and adds a template for global type augmentation to accordingly type network responses in custom links - Removes the
TCacheShapetype argument from allApolloClientusages (types and constructor calls)
- Moves
Usage against TypeScript/TSX
There is some syntax overlap between TypeScript and TypeScript-JSX files, so you might need to run the Codemod against both file extensions sepearately to avoid errors:
npx @apollo/client-codemod-migrate-3-to-4 --parser ts --ignore-pattern="*.{tsx,d.ts}" file1.ts file2.ts
npx @apollo/client-codemod-migrate-3-to-4 --parser tsx --ignore-pattern="*.ts" file1.ts file2.tsUsing the Codemod from a specific PR
npx https://pkg.pr.new/apollographql/apollo-client/@apollo/client-codemod-migrate-3-to-4@12733 --parser ts file1.ts file2.ts