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

Fix tree-shaking issues related to the useSuspenseQuery file #11622

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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/empty-ladybugs-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix tree-shaking issues related to the useSuspenseQuery file
9 changes: 9 additions & 0 deletions src/react/hooks/internal/toApolloError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ApolloQueryResult } from "../../../core/index.js";
import { ApolloError } from "../../../core/index.js";
import { isNonEmptyArray } from "../../../utilities/index.js";

export function toApolloError(result: ApolloQueryResult<any>) {
return isNonEmptyArray(result.errors) ?
new ApolloError({ graphQLErrors: result.errors })
: result.error;
}
63 changes: 63 additions & 0 deletions src/react/hooks/internal/useWatchQueryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type {
ApolloClient,
DocumentNode,
OperationVariables,
TypedDocumentNode,
WatchQueryOptions,
} from "../../../core/index.js";
import { useDeepMemo } from "./index.js";
import type { SkipToken } from "../constants.js";
import { skipToken } from "../constants.js";
import { validateOptions } from "../useSuspenseQuery.js";
import type { SuspenseQueryHookOptions } from "../../types/types.js";

export function useWatchQueryOptions<
TData,
TVariables extends OperationVariables,
>({
client,
query,
options,
}: UseWatchQueryOptionsHookOptions<TData, TVariables>): WatchQueryOptions<
TVariables,
TData
> {
return useDeepMemo<WatchQueryOptions<TVariables, TData>>(() => {
if (options === skipToken) {
return { query, fetchPolicy: "standby" };
}

const fetchPolicy =
options.fetchPolicy ||
client.defaultOptions.watchQuery?.fetchPolicy ||
"cache-first";

const watchQueryOptions = {
...options,
fetchPolicy,
query,
notifyOnNetworkStatusChange: false,
nextFetchPolicy: void 0,
};

if (__DEV__) {
validateOptions(watchQueryOptions);
}

// Assign the updated fetch policy after our validation since `standby` is
// not a supported fetch policy on its own without the use of `skip`.
if (options.skip) {
watchQueryOptions.fetchPolicy = "standby";
}

return watchQueryOptions;
}, [client, options, query]);
}
export interface UseWatchQueryOptionsHookOptions<
TData,
TVariables extends OperationVariables,
> {
client: ApolloClient<unknown>;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
options: SkipToken | SuspenseQueryHookOptions<TData, TVariables>;
}
2 changes: 1 addition & 1 deletion src/react/hooks/useBackgroundQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import type { CacheKey, QueryReference } from "../internal/index.js";
import type { BackgroundQueryHookOptions, NoInfer } from "../types/types.js";
import { __use, makeHookWrappable } from "./internal/index.js";
import { useWatchQueryOptions } from "./useSuspenseQuery.js";
import { useWatchQueryOptions } from "./internal/useWatchQueryOptions.js";
import type { FetchMoreFunction, RefetchFunction } from "./useSuspenseQuery.js";
import { canonicalStringify } from "../../cache/index.js";
import type { DeepPartial } from "../../utilities/index.js";
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useLoadableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import type { CacheKey, QueryReference } from "../internal/index.js";
import type { LoadableQueryHookOptions } from "../types/types.js";
import { __use, useRenderGuard } from "./internal/index.js";
import { useWatchQueryOptions } from "./useSuspenseQuery.js";
import { useWatchQueryOptions } from "./internal/useWatchQueryOptions.js";
import type { FetchMoreFunction, RefetchFunction } from "./useSuspenseQuery.js";
import { canonicalStringify } from "../../cache/index.js";
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useReadQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../internal/index.js";
import type { QueryReference } from "../internal/index.js";
import { __use, makeHookWrappable } from "./internal/index.js";
import { toApolloError } from "./useSuspenseQuery.js";
import { toApolloError } from "./internal/toApolloError.js";
import { useSyncExternalStore } from "./useSyncExternalStore.js";
import type { ApolloError } from "../../errors/index.js";
import type { NetworkStatus } from "../../core/index.js";
Expand Down
69 changes: 6 additions & 63 deletions src/react/hooks/useSuspenseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import type {
WatchQueryFetchPolicy,
FetchMoreQueryOptions,
WatchQueryOptions,
ApolloError,
} from "../../core/index.js";
import { ApolloError, NetworkStatus } from "../../core/index.js";
import { NetworkStatus } from "../../core/index.js";
import type { DeepPartial } from "../../utilities/index.js";
import { isNonEmptyArray } from "../../utilities/index.js";
import { useApolloClient } from "./useApolloClient.js";
import { DocumentType, verifyDocumentType } from "../parser/index.js";
import type {
SuspenseQueryHookOptions,
ObservableQueryFields,
NoInfer,
} from "../types/types.js";
import { __use, useDeepMemo, makeHookWrappable } from "./internal/index.js";
import { __use, makeHookWrappable } from "./internal/index.js";
import { getSuspenseCache } from "../internal/index.js";
import { canonicalStringify } from "../../cache/index.js";
import { skipToken } from "./constants.js";
import type { SkipToken } from "./constants.js";
import type { CacheKey, QueryKey } from "../internal/index.js";
import { toApolloError } from "./internal/toApolloError.js";
import { useWatchQueryOptions } from "./internal/useWatchQueryOptions.js";

export interface UseSuspenseQueryResult<
TData = unknown,
Expand Down Expand Up @@ -290,7 +291,7 @@ const wrapped = /*#__PURE__*/ makeHookWrappable(
// @ts-expect-error Cannot assign to 'useSuspenseQuery' because it is a function.ts(2630)
useSuspenseQuery = wrapped;

function validateOptions(options: WatchQueryOptions) {
export function validateOptions(options: WatchQueryOptions) {
const { query, fetchPolicy, returnPartialData } = options;

verifyDocumentType(query, DocumentType.Query);
Expand Down Expand Up @@ -325,61 +326,3 @@ function validatePartialDataReturn(
);
}
}

export function toApolloError(result: ApolloQueryResult<any>) {
return isNonEmptyArray(result.errors) ?
new ApolloError({ graphQLErrors: result.errors })
: result.error;
}

interface UseWatchQueryOptionsHookOptions<
TData,
TVariables extends OperationVariables,
> {
client: ApolloClient<unknown>;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
options: SkipToken | SuspenseQueryHookOptions<TData, TVariables>;
}

export function useWatchQueryOptions<
TData,
TVariables extends OperationVariables,
>({
client,
query,
options,
}: UseWatchQueryOptionsHookOptions<TData, TVariables>): WatchQueryOptions<
TVariables,
TData
> {
return useDeepMemo<WatchQueryOptions<TVariables, TData>>(() => {
if (options === skipToken) {
return { query, fetchPolicy: "standby" };
}

const fetchPolicy =
options.fetchPolicy ||
client.defaultOptions.watchQuery?.fetchPolicy ||
"cache-first";

const watchQueryOptions = {
...options,
fetchPolicy,
query,
notifyOnNetworkStatusChange: false,
nextFetchPolicy: void 0,
};

if (__DEV__) {
validateOptions(watchQueryOptions);
}

// Assign the updated fetch policy after our validation since `standby` is
// not a supported fetch policy on its own without the use of `skip`.
if (options.skip) {
watchQueryOptions.fetchPolicy = "standby";
}

return watchQueryOptions;
}, [client, options, query]);
}
Loading