Skip to content

Add support for @apollo/client v4#85

Closed
MasterOdin wants to merge 9 commits intomasterfrom
apollo-4
Closed

Add support for @apollo/client v4#85
MasterOdin wants to merge 9 commits intomasterfrom
apollo-4

Conversation

@MasterOdin
Copy link
Collaborator

@MasterOdin MasterOdin commented Nov 30, 2025

Closes #82

PR adds support for @apollo/client@^4 in the library.

The biggest change necessary for it was moving from compiling the library for es5 to es2015 as @apollo/client now exposes a proper es2015 class, and so errors under es5. Luckily @apollo/client@3 happily works with either. While this is a breaking change, per https://node.green/, anyone using node 6+ is compatible with it, where node 6 was released 9 years ago and hit end of life 7 years ago. At this point, I think anyone still using such an old version of node is also probably not bothering to update their libraries either.

Secondarily, @apollo/client@4 reworked a number of imports, and so to work around this I added a new types.ts file, which uses conditional types to hopefully provide downstream users useful type information. Given I'm bridging two different versions, this necessitated adding a bunch of @ts-ignore to the file due to missing types on v3 and v4 to get it working for testing. Unfortunately, building the library against @apollo/client@3 will throw a bunch of type errors still:

type errors
node_modules/apollo-link-timeout/lib/types/types.d.ts:2:25 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

2 export type Operation = ApolloLink.Operation extends never ? import('@apollo/client/core').Operation : ApolloLink.Operation;
                          ~~~~~~~~~~

node_modules/apollo-link-timeout/lib/types/types.d.ts:2:104 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

2 export type Operation = ApolloLink.Operation extends never ? import('@apollo/client/core').Operation : ApolloLink.Operation;
                                                                                                         ~~~~~~~~~~

node_modules/apollo-link-timeout/lib/types/types.d.ts:3:24 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

3 export type NextLink = ApolloLink.ForwardFunction extends never ? import('@apollo/client/core').NextLink : ApolloLink.ForwardFunction;
                         ~~~~~~~~~~

node_modules/apollo-link-timeout/lib/types/types.d.ts:3:108 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

3 export type NextLink = ApolloLink.ForwardFunction extends never ? import('@apollo/client/core').NextLink : ApolloLink.ForwardFunction;
                                                                                                             ~~~~~~~~~~

node_modules/apollo-link-timeout/lib/types/types.d.ts:4:36 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

4 export type FetchResult<T = any> = ApolloLink.Result extends never ? import('@apollo/client/core').FetchResult<T> : ApolloLink.Result<T>;
                                     ~~~~~~~~~~

node_modules/apollo-link-timeout/lib/types/types.d.ts:4:117 - error TS2702: 'ApolloLink' only refers to a type, but is being used as a namespace here.

4 export type FetchResult<T = any> = ApolloLink.Result extends never ? import('@apollo/client/core').FetchResult<T> : ApolloLink.Result<T>;
                                                                                                                      ~~~~~~~~~~

These errors can be avoided by ensuring you have set "skipLibCheck": true to your tsconfig.json (which is set by default on a fresh tsc --init run) so I think that's acceptable, and I've added a note about it.

Given this, probably makes sense to have this be another major release. We could also just drop support for @apollo/client@3 on the new major version, and only support @apollo/client@4, and then keep the 5.x line alive minimally for any new backwards compatible bugfixes or features that might exist as per the stats on https://www.npmjs.com/package/@apollo/client?activeTab=versions, @apollo/client@3 is still installed more in the past 7 days.

Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>

strategy:
matrix:
node-version: [12.x, 14.x, 20.x]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apollo/client@4 uses null chainging (added in node 14) and ||= operator (added in node 15), so have to drop those versions from testing for it.

}
return execute.length === 3
// @ts-ignore
? execute(link, operation, { client: apolloClientInstance })
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

execute in @apollo/client@4 requires passing the client that initiating the execute, so this wrapper acts as a shim around that to support both 3 and 4.

"graphql-tag": "^2.12.6",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"rxjs": "^7.8.2",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New peer dependency for @apollo/client@4.

@MasterOdin
Copy link
Collaborator Author

Closing this in favor of #86. While having the types.ts file is neat in terms of not relying directly on deprecated types for v4, it ends up with too many weird caveats and @ts-ignore failures for comfort.

@MasterOdin MasterOdin closed this Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support @apollo/client version 4

1 participant