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

Improve context types #10402

Merged
merged 8 commits into from
Jan 20, 2023
Merged
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/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