-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow QueryManager to intercept hook functionality #11617
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c518f31
Allow Apollo Client instance to intercept hook functionality
phryneas 6615778
update api extractor
phryneas 6d573c5
changeset
phryneas 45fc3b3
keep PURE comments when building cjs
phryneas d7934e0
shave a few bytes
phryneas ce8e6e7
Workaround for `useReadableQuery` without wrapping `Provider`.
phryneas bb2c718
update size-limits
phryneas c153a4e
Update src/react/hooks/internal/wrapHook.ts
phryneas 7497c9c
put wrappers on `QueryManager` instead
phryneas 94aa747
add `__NO_SIDE_EFFECTS__` annotation
phryneas 5519a64
swap call order
phryneas c58802e
better tree-shaking approach
phryneas a114bb0
adjust comment
phryneas a274f96
simplify implementation by just calling `wrapHook`
phryneas 57a6c75
adjust comments
phryneas d019058
slight type adjustment
phryneas 682aa45
Merge branch 'main' into pr/wrapHooks-2
phryneas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> | ||
phryneas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't strictly need this here anymore in this specific case, but it would be good to have this in our bundling config anyways, so I'll leave it here.