Skip to content

Commit

Permalink
Improve context types (#10402)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Jan 20, 2023
1 parent 8f79bb2 commit 0b07aa9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-kings-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Improve context types
2 changes: 1 addition & 1 deletion src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IsStrictlyAny } from '../utilities';

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

export type DefaultContext = Record<string, any>;
export interface DefaultContext extends Record<string, any> {};

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

Expand Down Expand Up @@ -140,7 +140,7 @@ export type ApolloQueryResult<T> = {
*/
errors?: ReadonlyArray<GraphQLError>;
/**
* 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.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface QueryOptions<TVariables = OperationVariables, TData = any> {
/**
* Context to be passed to link execution chain
*/
context?: any;
context?: DefaultContext;

/**
* Specifies the {@link FetchPolicy} to be used for this query
Expand Down Expand Up @@ -159,7 +159,7 @@ export interface NextFetchPolicyContext<TData, TVariables> {
export interface FetchMoreQueryOptions<TVariables, TData = any> {
query?: DocumentNode | TypedDocumentNode<TData, TVariables>;
variables?: Partial<TVariables>;
context?: any;
context?: DefaultContext;
}

export type UpdateQueryFn<
Expand Down
5 changes: 3 additions & 2 deletions src/link/context/index.ts
Original file line number Diff line number Diff line change
@@ -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> | any;
prevContext: DefaultContext,
) => Promise<DefaultContext> | DefaultContext;

export function setContext(setter: ContextSetter): ApolloLink {
return new ApolloLink((operation: Operation, forward: NextLink) => {
Expand Down
9 changes: 5 additions & 4 deletions src/link/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DocumentNode, ExecutionResult, GraphQLError } from "graphql";
import { DefaultContext } from "../../core";
export { DocumentNode };

import { Observable } from "../../utilities";
Expand Down Expand Up @@ -60,7 +61,7 @@ export interface GraphQLRequest {
query: DocumentNode;
variables?: Record<string, any>;
operationName?: string;
context?: Record<string, any>;
context?: DefaultContext;
extensions?: Record<string, any>;
}

Expand All @@ -69,13 +70,13 @@ export interface Operation {
variables: Record<string, any>;
operationName: string;
extensions: Record<string, any>;
setContext: (context: Record<string, any>) => Record<string, any>;
getContext: () => Record<string, any>;
setContext: (context: DefaultContext) => DefaultContext;
getContext: () => DefaultContext;
}

export interface SingleExecutionResult<
TData = Record<string, any>,
TContext = Record<string, any>,
TContext = DefaultContext,
TExtensions = Record<string, any>
> extends ExecutionResult<TData, TExtensions> {
data?: Data<TData>;
Expand Down

0 comments on commit 0b07aa9

Please sign in to comment.