Skip to content

Commit 0b07aa9

Browse files
Hugodbyalessbelljerelmiller
authored
Improve context types (apollographql#10402)
* Allow declaration merging for DefaultContext * Use DefaultContext instead of any * Fix test * Add changeset Co-authored-by: Alessia Bellisario <alessia@apollographql.com> Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
1 parent 8f79bb2 commit 0b07aa9

6 files changed

Lines changed: 18 additions & 11 deletions

File tree

.changeset/spotty-kings-unite.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+
Improve context types

src/__tests__/ApolloClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2245,7 +2245,7 @@ describe('ApolloClient', () => {
22452245
query: {kind: Kind.DOCUMENT, definitions: []},
22462246
variables: {foo: 'bar'},
22472247
errorPolicy: 'none',
2248-
context: null,
2248+
context: undefined,
22492249
fetchPolicy: 'cache-first',
22502250
pollInterval: 100,
22512251
notifyOnNetworkStatusChange: true,

src/core/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { IsStrictlyAny } from '../utilities';
1313

1414
export { TypedDocumentNode } from '@graphql-typed-document-node/core';
1515

16-
export type DefaultContext = Record<string, any>;
16+
export interface DefaultContext extends Record<string, any> {};
1717

1818
export type QueryListener = (queryInfo: QueryInfo) => void;
1919

@@ -140,7 +140,7 @@ export type ApolloQueryResult<T> = {
140140
*/
141141
errors?: ReadonlyArray<GraphQLError>;
142142
/**
143-
* The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual `client.query` calls.
143+
* The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual `client.query` calls.
144144
* This will contain both a NetworkError field and any GraphQLErrors.
145145
* See https://www.apollographql.com/docs/react/data/error-handling/ for more information.
146146
*/

src/core/watchQueryOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface QueryOptions<TVariables = OperationVariables, TData = any> {
7373
/**
7474
* Context to be passed to link execution chain
7575
*/
76-
context?: any;
76+
context?: DefaultContext;
7777

7878
/**
7979
* Specifies the {@link FetchPolicy} to be used for this query
@@ -159,7 +159,7 @@ export interface NextFetchPolicyContext<TData, TVariables> {
159159
export interface FetchMoreQueryOptions<TVariables, TData = any> {
160160
query?: DocumentNode | TypedDocumentNode<TData, TVariables>;
161161
variables?: Partial<TVariables>;
162-
context?: any;
162+
context?: DefaultContext;
163163
}
164164

165165
export type UpdateQueryFn<

src/link/context/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { ApolloLink, Operation, GraphQLRequest, NextLink } from '../core';
22
import { Observable, ObservableSubscription } from '../../utilities';
3+
import { DefaultContext } from '../../core';
34

45
export type ContextSetter = (
56
operation: GraphQLRequest,
6-
prevContext: any,
7-
) => Promise<any> | any;
7+
prevContext: DefaultContext,
8+
) => Promise<DefaultContext> | DefaultContext;
89

910
export function setContext(setter: ContextSetter): ApolloLink {
1011
return new ApolloLink((operation: Operation, forward: NextLink) => {

src/link/core/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DocumentNode, ExecutionResult, GraphQLError } from "graphql";
2+
import { DefaultContext } from "../../core";
23
export { DocumentNode };
34

45
import { Observable } from "../../utilities";
@@ -60,7 +61,7 @@ export interface GraphQLRequest {
6061
query: DocumentNode;
6162
variables?: Record<string, any>;
6263
operationName?: string;
63-
context?: Record<string, any>;
64+
context?: DefaultContext;
6465
extensions?: Record<string, any>;
6566
}
6667

@@ -69,13 +70,13 @@ export interface Operation {
6970
variables: Record<string, any>;
7071
operationName: string;
7172
extensions: Record<string, any>;
72-
setContext: (context: Record<string, any>) => Record<string, any>;
73-
getContext: () => Record<string, any>;
73+
setContext: (context: DefaultContext) => DefaultContext;
74+
getContext: () => DefaultContext;
7475
}
7576

7677
export interface SingleExecutionResult<
7778
TData = Record<string, any>,
78-
TContext = Record<string, any>,
79+
TContext = DefaultContext,
7980
TExtensions = Record<string, any>
8081
> extends ExecutionResult<TData, TExtensions> {
8182
data?: Data<TData>;

0 commit comments

Comments
 (0)