Skip to content

Commit d85c9b9

Browse files
authored
Add helper type QueryRef.ForQuery<TypedDocumentNode> (#13012)
* Add helper type `QueryRef.ForQuery<TypedDocumentNode>` * chores
1 parent bfcd16e commit d85c9b9

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

.api-reports/api-report-react_internal.api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import type { ApolloClient } from '@apollo/client';
88
import type { DataState } from '@apollo/client';
99
import type { DecoratedPromise } from '@apollo/client/utilities/internal';
1010
import type { DocumentNode } from 'graphql';
11+
import type { DocumentTypeDecoration } from '@graphql-typed-document-node/core';
1112
import type { InternalTypes } from '@apollo/client/react';
1213
import type { MaybeMasked } from '@apollo/client/masking';
1314
import type { MaybeMasked as MaybeMasked_2 } from '@apollo/client';
1415
import type { ObservableQuery } from '@apollo/client';
1516
import type { OperationVariables } from '@apollo/client';
17+
import type { ResultOf } from '@graphql-typed-document-node/core';
18+
import type { VariablesOf } from '@graphql-typed-document-node/core';
1619

1720
// Warning: (ae-forgotten-export) The symbol "WrappedQueryRef" needs to be exported by the entry point index.d.ts
1821
//
@@ -186,6 +189,12 @@ export interface QueryRef<TData = unknown, TVariables extends OperationVariables
186189
};
187190
}
188191

192+
// @public (undocumented)
193+
export namespace QueryRef {
194+
// (undocumented)
195+
export type ForQuery<Document extends DocumentTypeDecoration<any, any>, TStates extends DataState<ResultOf<Document>>["dataState"] = "complete" | "streaming"> = QueryRef<ResultOf<Document>, VariablesOf<Document>, TStates>;
196+
}
197+
189198
// @public (undocumented)
190199
type QueryRefPromise<TData, TStates extends DataState<TData>["dataState"]> = DecoratedPromise<ObservableQuery.Result<MaybeMasked<TData>, TStates>>;
191200

.changeset/rare-pigs-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Add helper type `QueryRef.ForQuery<TypedDocumentNode>`

src/react/internal/cache/QueryReference.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import type {
2+
DocumentTypeDecoration,
3+
ResultOf,
4+
VariablesOf,
5+
} from "@graphql-typed-document-node/core";
16
import { equal } from "@wry/equality";
27
import type { Subscription } from "rxjs";
38
import { filter } from "rxjs";
@@ -48,6 +53,15 @@ export interface QueryRef<
4853
[QUERY_REF_BRAND]?(variables: TVariables): { data: TData; states: TStates };
4954
}
5055

56+
export declare namespace QueryRef {
57+
export type ForQuery<
58+
Document extends DocumentTypeDecoration<any, any>,
59+
TStates extends DataState<ResultOf<Document>>["dataState"] =
60+
| "complete"
61+
| "streaming",
62+
> = QueryRef<ResultOf<Document>, VariablesOf<Document>, TStates>;
63+
}
64+
5165
/**
5266
* @internal
5367
* For usage in internal helpers only.

src/react/internal/cache/__tests__/QueryReference.test.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { expectTypeOf } from "expect-type";
12
import React from "react";
23
import { of } from "rxjs";
34

4-
import type { DataState, OperationVariables } from "@apollo/client";
5+
import type {
6+
DataState,
7+
OperationVariables,
8+
TypedDocumentNode,
9+
} from "@apollo/client";
510
import { ApolloClient, ApolloLink, InMemoryCache } from "@apollo/client";
611
import { InternalQueryReference } from "@apollo/client/react/internal";
712
import { setupSimpleCase } from "@apollo/client/testing/internal";
@@ -240,4 +245,28 @@ test.skip("type tests", () => {
240245
</>;
241246
}
242247
});
248+
249+
test("QueryRef.ForQuery", () => {
250+
const ANY: any = {};
251+
252+
interface Data {
253+
foo: string;
254+
}
255+
type Vars = {
256+
bar: string;
257+
};
258+
const query: TypedDocumentNode<Data, Vars> = ANY;
259+
260+
expectTypeOf<QueryRef.ForQuery<typeof query>>().toEqualTypeOf<
261+
QueryRef<Data, Vars>
262+
>();
263+
264+
expectTypeOf<QueryRef.ForQuery<typeof query, "complete">>().toEqualTypeOf<
265+
QueryRef<Data, Vars, "complete">
266+
>();
267+
268+
expectTypeOf<
269+
QueryRef.ForQuery<typeof query, "empty" | "partial" | "complete">
270+
>().toEqualTypeOf<QueryRef<Data, Vars, "empty" | "partial" | "complete">>();
271+
});
243272
});

0 commit comments

Comments
 (0)