apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

Fix operation.setContext() type #11756

Closed henryqdineen closed 6 months ago

henryqdineen commented 7 months ago

I noticed couple issues with the Operation.setContext() types that I try to fix in this PR:

netlify[bot] commented 7 months ago

Deploy Preview for apollo-client-docs ready!

Name Link
Latest commit d2c81dc281ecbec65201f951ab4fde2ede7b47d1
Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/66294b43876f0b0009bc2bb9
Deploy Preview https://deploy-preview-11756--apollo-client-docs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

changeset-bot[bot] commented 7 months ago

🦋 Changeset detected

Latest commit: d2c81dc281ecbec65201f951ab4fde2ede7b47d1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | -------------- | ----- | | @apollo/client | Patch |

Not sure what this means? Click here to learn what changesets are.

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/henryqdineen/apollo-client/new/hqd-fix-set-context-type?filename=.changeset/fast-points-sip.md&value=---%0A%22%40apollo%2Fclient%22%3A%20patch%0A---%0A%0AFix%20operation.setContext()%20type%0A)

phryneas commented 7 months ago

While we're at it, could you please also add these adjustments?

--- a/src/link/utils/createOperation.ts
+++ b/src/link/utils/createOperation.ts
@@ -5,14 +5,14 @@ export function createOperation(
   operation: GraphQLRequest
 ): Operation {
   let context = { ...starting };
-  const setContext = (next: any) => {
+  const setContext: Operation["setContext"] = (next) => {
     if (typeof next === "function") {
       context = { ...context, ...next(context) };
     } else {
       context = { ...context, ...next };
     }
   };
-  const getContext = () => ({ ...context });
+  const getContext: Operation["getContext"] = () => ({ ...context });

   Object.defineProperty(operation, "setContext", {
     enumerable: false,
henryqdineen commented 7 months ago

Just made the adjustments. Thanks! No rush.