-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow QueryManager to intercept hook functionality (#11617)
* Allow Apollo Client instance to intercept hook functionality * update api extractor * changeset * keep PURE comments when building cjs * shave a few bytes * Workaround for `useReadableQuery` without wrapping `Provider`. * update size-limits * Update src/react/hooks/internal/wrapHook.ts * put wrappers on `QueryManager` instead * add `__NO_SIDE_EFFECTS__` annotation * swap call order * better tree-shaking approach * adjust comment * simplify implementation by just calling `wrapHook` * adjust comments * slight type adjustment
- Loading branch information
Showing
12 changed files
with
565 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Allow Apollo Client instance to intercept hook functionality |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"dist/apollo-client.min.cjs": 39075, | ||
"dist/apollo-client.min.cjs": 39209, | ||
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32584 | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { | ||
useQuery, | ||
useSuspenseQuery, | ||
useBackgroundQuery, | ||
useReadQuery, | ||
useFragment, | ||
} from "../index.js"; | ||
import type { QueryManager } from "../../../core/QueryManager.js"; | ||
import type { ApolloClient } from "../../../core/ApolloClient.js"; | ||
import type { ObservableQuery } from "../../../core/ObservableQuery.js"; | ||
|
||
const wrapperSymbol = Symbol.for("apollo.hook.wrappers"); | ||
|
||
interface WrappableHooks { | ||
useQuery: typeof useQuery; | ||
useSuspenseQuery: typeof useSuspenseQuery; | ||
useBackgroundQuery: typeof useBackgroundQuery; | ||
useReadQuery: typeof useReadQuery; | ||
useFragment: typeof useFragment; | ||
} | ||
|
||
/** | ||
* @internal | ||
* Can be used to correctly type the [Symbol.for("apollo.hook.wrappers")] property of | ||
* `QueryManager`, to override/wrap hook functionality. | ||
*/ | ||
export type HookWrappers = { | ||
[K in keyof WrappableHooks]?: ( | ||
originalHook: WrappableHooks[K] | ||
) => WrappableHooks[K]; | ||
}; | ||
|
||
interface QueryManagerWithWrappers<T> extends QueryManager<T> { | ||
[wrapperSymbol]?: HookWrappers; | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* Makes an Apollo Client hook "wrappable". | ||
* That means that the Apollo Client instance can expose a "wrapper" that will be | ||
* used to wrap the original hook implementation with additional logic. | ||
* @example | ||
* ```tsx | ||
* // this is already done in `@apollo/client` for all wrappable hooks (see `WrappableHooks`) | ||
* // following this pattern | ||
* function useQuery() { | ||
* return wrapHook('useQuery', _useQuery, options.client)(query, options); | ||
* } | ||
* function _useQuery(query, options) { | ||
* // original implementation | ||
* } | ||
* | ||
* // this is what a library like `@apollo/client-react-streaming` would do | ||
* class ApolloClientWithStreaming extends ApolloClient { | ||
* constructor(options) { | ||
* super(options); | ||
* this.queryManager[Symbol.for("apollo.hook.wrappers")] = { | ||
* useQuery: (original) => (query, options) => { | ||
* console.log("useQuery was called with options", options); | ||
* return original(query, options); | ||
* } | ||
* } | ||
* } | ||
* } | ||
* | ||
* // this will now log the options and then call the original `useQuery` | ||
* const client = new ApolloClientWithStreaming({ ... }); | ||
* useQuery(query, { client }); | ||
* ``` | ||
*/ | ||
export function wrapHook<Hook extends (...args: any[]) => any>( | ||
hookName: keyof WrappableHooks, | ||
useHook: Hook, | ||
clientOrObsQuery: ObservableQuery<any> | ApolloClient<any> | ||
): Hook { | ||
const queryManager = ( | ||
clientOrObsQuery as unknown as { | ||
// both `ApolloClient` and `ObservableQuery` have a `queryManager` property | ||
// but they're both `private`, so we have to cast around for a bit here. | ||
queryManager: QueryManagerWithWrappers<any>; | ||
} | ||
)["queryManager"]; | ||
const wrappers = queryManager && queryManager[wrapperSymbol]; | ||
const wrapper: undefined | ((wrap: Hook) => Hook) = | ||
wrappers && (wrappers[hookName] as any); | ||
return wrapper ? wrapper(useHook) : useHook; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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