diff --git a/.changeset/spotty-kings-unite.md b/.changeset/spotty-kings-unite.md new file mode 100644 index 00000000000..c9e25288ea3 --- /dev/null +++ b/.changeset/spotty-kings-unite.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Improve context types diff --git a/src/__tests__/ApolloClient.ts b/src/__tests__/ApolloClient.ts index 13aa7420031..da74a7bfc5b 100644 --- a/src/__tests__/ApolloClient.ts +++ b/src/__tests__/ApolloClient.ts @@ -2245,7 +2245,7 @@ describe('ApolloClient', () => { query: {kind: Kind.DOCUMENT, definitions: []}, variables: {foo: 'bar'}, errorPolicy: 'none', - context: null, + context: undefined, fetchPolicy: 'cache-first', pollInterval: 100, notifyOnNetworkStatusChange: true, diff --git a/src/core/types.ts b/src/core/types.ts index 9d34bd4050b..df10376341a 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -13,7 +13,7 @@ import { IsStrictlyAny } from '../utilities'; export { TypedDocumentNode } from '@graphql-typed-document-node/core'; -export type DefaultContext = Record; +export interface DefaultContext extends Record {}; export type QueryListener = (queryInfo: QueryInfo) => void; @@ -140,7 +140,7 @@ export type ApolloQueryResult = { */ errors?: ReadonlyArray; /** - * The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual `client.query` calls. + * The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual `client.query` calls. * This will contain both a NetworkError field and any GraphQLErrors. * See https://www.apollographql.com/docs/react/data/error-handling/ for more information. */ diff --git a/src/core/watchQueryOptions.ts b/src/core/watchQueryOptions.ts index 676dc7253de..e96a3fdd329 100644 --- a/src/core/watchQueryOptions.ts +++ b/src/core/watchQueryOptions.ts @@ -73,7 +73,7 @@ export interface QueryOptions { /** * Context to be passed to link execution chain */ - context?: any; + context?: DefaultContext; /** * Specifies the {@link FetchPolicy} to be used for this query @@ -159,7 +159,7 @@ export interface NextFetchPolicyContext { export interface FetchMoreQueryOptions { query?: DocumentNode | TypedDocumentNode; variables?: Partial; - context?: any; + context?: DefaultContext; } export type UpdateQueryFn< diff --git a/src/link/context/index.ts b/src/link/context/index.ts index 8ae9b42d93e..973f8adcb71 100644 --- a/src/link/context/index.ts +++ b/src/link/context/index.ts @@ -1,10 +1,11 @@ import { ApolloLink, Operation, GraphQLRequest, NextLink } from '../core'; import { Observable, ObservableSubscription } from '../../utilities'; +import { DefaultContext } from '../../core'; export type ContextSetter = ( operation: GraphQLRequest, - prevContext: any, -) => Promise | any; + prevContext: DefaultContext, +) => Promise | DefaultContext; export function setContext(setter: ContextSetter): ApolloLink { return new ApolloLink((operation: Operation, forward: NextLink) => { diff --git a/src/link/core/types.ts b/src/link/core/types.ts index a81038da00a..4454e4c6625 100644 --- a/src/link/core/types.ts +++ b/src/link/core/types.ts @@ -1,4 +1,5 @@ import { DocumentNode, ExecutionResult, GraphQLError } from "graphql"; +import { DefaultContext } from "../../core"; export { DocumentNode }; import { Observable } from "../../utilities"; @@ -60,7 +61,7 @@ export interface GraphQLRequest { query: DocumentNode; variables?: Record; operationName?: string; - context?: Record; + context?: DefaultContext; extensions?: Record; } @@ -69,13 +70,13 @@ export interface Operation { variables: Record; operationName: string; extensions: Record; - setContext: (context: Record) => Record; - getContext: () => Record; + setContext: (context: DefaultContext) => DefaultContext; + getContext: () => DefaultContext; } export interface SingleExecutionResult< TData = Record, - TContext = Record, + TContext = DefaultContext, TExtensions = Record > extends ExecutionResult { data?: Data;