Skip to content
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

feat: add useBackgroundQuery and useReadQuery Suspense hooks #10755

Merged
merged 40 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4438ab7
feat: add useBackgroundQuery and useReadQuery hooks
alessbell Apr 10, 2023
76a3193
chore: adds tests for context and canonizeResults options
alessbell May 8, 2023
ba2a712
chore: adds experimental label to useReadyQuery, updates exports snap…
alessbell May 8, 2023
65a42ab
fix: typo in import statement
alessbell May 8, 2023
6838b60
chore: adds integration test using startTransition
alessbell May 8, 2023
bb38e1d
wip: refetch
alessbell May 8, 2023
c18a839
chore: adds opts tests for client and suspenseCache
alessbell May 8, 2023
64d134f
chore: refactor and import shared code from useSuspenseQuery
alessbell May 9, 2023
b072f6d
chore: adds refetch test cases
alessbell May 9, 2023
e9b2b1c
Merge branch 'release-3.8' into issue-8694--useBackgroundQuery
alessbell May 9, 2023
5db73d4
chore: increase bundlesize limit to 35.22KB (was 35.20KB)
alessbell May 9, 2023
70785dd
chore: adds more refetch tests
alessbell May 10, 2023
fd4b613
chore(tests): adds refetch startTransition test
alessbell May 10, 2023
ebed28c
chore(tests): adds fetchMore tests
alessbell May 10, 2023
9712f84
chore(tests): adds test for cache-and-network + NetworkStatus.loading
alessbell May 10, 2023
e907ce1
chore: rename subscription to queryRef
alessbell May 11, 2023
21900ee
chore: refactor fetchMore tests
alessbell May 11, 2023
855c141
chore(tests): adds fetchMore with startTransition test
alessbell May 11, 2023
7360983
chore(types): narrow options type and add type tests
alessbell May 11, 2023
f46b7bc
chore: rename QuerySubscription to QueryReference
alessbell May 11, 2023
4670583
chore: remove test comments
alessbell May 11, 2023
49cdbb0
chore: adds changeset
alessbell May 11, 2023
1d996fe
chore: refactor fetchMore tests, finish renaming subscription > queryRef
alessbell May 12, 2023
7cd2303
chore: update src/react/hooks/index.ts export statements and update s…
alessbell May 12, 2023
e473774
address review comments, export types from src/react/hooks.index.ts
alessbell May 12, 2023
5b1d3ee
remove generic arguments where return types can be inferred in useBac…
alessbell May 12, 2023
397727e
remove unused variables from integration test
alessbell May 12, 2023
82a9bae
flatten test blocks
alessbell May 12, 2023
9b6bc9e
stash setVersion on queryRef, fixes failing integration tests
alessbell May 12, 2023
1bec2e0
chore: return rerender function
alessbell May 12, 2023
f04e82f
chore: merge release-3.8 branch and fix conflicts
alessbell May 15, 2023
e10c32f
chore: bump bundlesize to 35.25kb (was 35.22kb)
alessbell May 15, 2023
1f22c38
chore: fix type imports
alessbell May 15, 2023
f4393f8
chore: return tuple from useBackgroundQuery
alessbell May 15, 2023
76a2ee8
Merge branch 'release-3.8' into issue-8694--useBackgroundQuery
alessbell May 15, 2023
56083fb
chore: revert changes to useSuspenseQuery.test.tsx made in 5b1d3ee
alessbell May 15, 2023
930e41c
fix: reverse inferred/explicit use of generics in useSuspenseQuery te…
alessbell May 15, 2023
8869fe0
Merge branch 'release-3.8' into issue-8694--useBackgroundQuery
alessbell May 15, 2023
82fa16e
Merge branch 'release-3.8' into issue-8694--useBackgroundQuery
alessbell May 15, 2023
46ddb18
chore: bump size check to 36.84kb (was 36.68kb) and add new hooks to …
alessbell May 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-onions-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': minor
---

Feature: adds `useBackgroundQuery` and `useReadQuery` hooks
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ src/react/hooks/*
!src/react/hooks/internal
!src/react/hooks/useSuspenseCache.ts
!src/react/hooks/useSuspenseQuery.ts
!src/react/hooks/useBackgroundQuery.ts

## Allowed React hook tests
!src/react/hooks/__tests__/
src/react/hooks/__tests__/*
!src/react/hooks/__tests__/useSuspenseQuery.test.tsx
!src/react/hooks/__tests__/useBackgroundQuery.test.tsx
2 changes: 1 addition & 1 deletion config/bundlesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "path";
import { gzipSync } from "zlib";
import bytes from "bytes";

const gzipBundleByteLengthLimit = bytes("35.10KB");
const gzipBundleByteLengthLimit = bytes("35.25KB");
const minFile = join("dist", "apollo-client.min.cjs");
const minPath = join(__dirname, "..", minFile);
const gzipByteLen = gzipSync(readFileSync(minPath)).byteLength;
Expand Down
3 changes: 2 additions & 1 deletion config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const react17TestFileIgnoreList = [
ignoreTSFiles,
// For now, we only support useSuspenseQuery with React 18, so no need to test
// it with React 17
'src/react/hooks/__tests__/useSuspenseQuery.test.tsx'
'src/react/hooks/__tests__/useSuspenseQuery.test.tsx',
'src/react/hooks/__tests__/useBackgroundQuery.test.tsx'
]

const tsStandardConfig = {
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ Array [
"throwServerError",
"toPromise",
"useApolloClient",
"useBackgroundQuery_experimental",
"useFragment_experimental",
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useReadQuery_experimental",
"useSubscription",
"useSuspenseQuery_experimental",
]
Expand Down Expand Up @@ -252,11 +254,13 @@ Array [
"parser",
"resetApolloContext",
"useApolloClient",
"useBackgroundQuery_experimental",
"useFragment_experimental",
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useReadQuery_experimental",
"useSubscription",
"useSuspenseQuery_experimental",
]
Expand Down Expand Up @@ -292,11 +296,13 @@ Array [
exports[`exports of public entry points @apollo/client/react/hooks 1`] = `
Array [
"useApolloClient",
"useBackgroundQuery_experimental",
"useFragment_experimental",
"useLazyQuery",
"useMutation",
"useQuery",
"useReactiveVar",
"useReadQuery_experimental",
"useSubscription",
"useSuspenseQuery_experimental",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ type FetchMoreOptions<TData> = Parameters<
ObservableQuery<TData>['fetchMore']
>[0];

interface QuerySubscriptionOptions {
interface QueryReferenceOptions {
onDispose?: () => void;
autoDisposeTimeoutMs?: number;
}

export class QuerySubscription<TData = unknown> {
export class QueryReference<TData = unknown> {
alessbell marked this conversation as resolved.
Show resolved Hide resolved
public result: ApolloQueryResult<TData>;
public readonly observable: ObservableQuery<TData>;

public version: 'main' | 'network' = 'main';
public promises: {
main: Promise<ApolloQueryResult<TData>>;
network?: Promise<ApolloQueryResult<TData>>;
Expand All @@ -45,7 +46,7 @@ export class QuerySubscription<TData = unknown> {

constructor(
observable: ObservableQuery<TData>,
options: QuerySubscriptionOptions = Object.create(null)
options: QueryReferenceOptions = Object.create(null)
) {
this.listen = this.listen.bind(this);
this.handleNext = this.handleNext.bind(this);
Expand Down Expand Up @@ -83,7 +84,7 @@ export class QuerySubscription<TData = unknown> {
}

// Start a timer that will automatically dispose of the query if the
// suspended resource does not use this subscription in the given time. This
// suspended resource does not use this queryRef in the given time. This
// helps prevent memory leaks when a component has unmounted before the
// query has finished loading.
this.autoDisposeTimeoutId = setTimeout(
Expand Down
16 changes: 8 additions & 8 deletions src/react/cache/SuspenseCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Trie } from '@wry/trie';
import type { ObservableQuery } from '../../core';
import { canUseWeakMap } from '../../utilities';
import { QuerySubscription } from './QuerySubscription';
import { QueryReference } from './QueryReference';

type CacheKey = any[];

Expand All @@ -25,29 +25,29 @@ export class SuspenseCache {
(cacheKey: CacheKey) => cacheKey
);

private subscriptions = new Map<CacheKey, QuerySubscription>();
private queryRefs = new Map<CacheKey, QueryReference>();
private options: SuspenseCacheOptions;

constructor(options: SuspenseCacheOptions = Object.create(null)) {
this.options = options;
}

getSubscription<TData = any>(
getQueryRef<TData = any>(
cacheKey: CacheKey,
createObservable: () => ObservableQuery<TData>
) {
const stableCacheKey = this.cacheKeys.lookupArray(cacheKey);

if (!this.subscriptions.has(stableCacheKey)) {
this.subscriptions.set(
if (!this.queryRefs.has(stableCacheKey)) {
this.queryRefs.set(
stableCacheKey,
new QuerySubscription(createObservable(), {
new QueryReference(createObservable(), {
autoDisposeTimeoutMs: this.options.autoDisposeTimeoutMs,
onDispose: () => this.subscriptions.delete(stableCacheKey),
onDispose: () => this.queryRefs.delete(stableCacheKey),
})
);
}

return this.subscriptions.get(stableCacheKey)! as QuerySubscription<TData>;
return this.queryRefs.get(stableCacheKey)! as QueryReference<TData>;
}
}
Loading