Skip to content

Commit

Permalink
Make TVariables and TContext optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenn Creighton committed Apr 5, 2021
1 parent b52d157 commit 4ca6362
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
39 changes: 25 additions & 14 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ApolloQueryResult,
OperationVariables,
MutationUpdaterFunction,
ReobserveQueryCallback,
} from './types';
import { LocalState } from './LocalState';

Expand Down Expand Up @@ -216,6 +217,10 @@ export class QueryManager<TStore> {
storeResult = result;

if (fetchPolicy !== 'no-cache') {
// Returning the result of markMutationResult here makes the
// mutation await any Promise that markMutationResult returns,
// since we are returning this Promise from the asyncMap mapping
// function.
try {
self.markMutationResult<T, TVariables, TContext>({
mutationId,
Expand Down Expand Up @@ -299,8 +304,9 @@ export class QueryManager<TStore> {
variables?: TVariables;
errorPolicy: ErrorPolicy;
context?: TContext;
updateQueries: UpdateQueries<TData>,
update?: MutationUpdaterFunction<TData, TVariables, TContext>,
updateQueries: UpdateQueries<TData>;
update?: MutationUpdaterFunction<TData, TVariables, TContext>;
reobserveQuery?: ReobserveQueryCallback;
},
cache = this.cache,
): Promise<void> {
Expand Down Expand Up @@ -361,24 +367,29 @@ export class QueryManager<TStore> {
// a write action.
const { update } = mutation;
if (update) {
update(c, mutation.result);
update(c as any, mutation.result, {
context: mutation.context,
variables: mutation.variables,
});
}
},

// Write the final mutation.result to the root layer of the cache.
optimistic: false,

// If the mutation has some writes associated with it then we need to
// apply those writes to the store by running this reducer again with a
// write action.
const { update } = mutation;
if (update) {
update(c as any, mutation.result, {
context: mutation.context,
variables: mutation.variables,
});
}
}, /* non-optimistic transaction: */ null);
onDirty: mutation.reobserveQuery && ((watch, diff) => {
if (watch.watcher instanceof QueryInfo) {
const oq = watch.watcher.observableQuery;
if (oq) {
reobserveResults.push(mutation.reobserveQuery!(oq, diff));
// Prevent the normal cache broadcast of this result.
return false;
}
}
}),
});

return Promise.all(reobserveResults).then(() => void 0);
}

return Promise.resolve();
Expand Down
7 changes: 4 additions & 3 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PureQueryOptions,
OperationVariables,
MutationUpdaterFunction,
ReobserveQueryCallback,
} from './types';

/**
Expand Down Expand Up @@ -265,9 +266,9 @@ export interface MutationBaseOptions<
}

export interface MutationOptions<
T,
TVariables extends OperationVariables,
TContext extends Context,
T = any,
TVariables extends OperationVariables = OperationVariables,
TContext extends Context = Context,
> extends MutationBaseOptions<T, TVariables, TContext> {
/**
* A GraphQL document, often created with `gql` from the `graphql-tag`
Expand Down
5 changes: 4 additions & 1 deletion src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { Observable } from '../../utilities';
import { FetchResult } from '../../link/core';
import { ApolloError } from '../../errors';
import {
Context,
ApolloClient,
ApolloQueryResult,
Context,
ErrorPolicy,
FetchMoreOptions,
FetchMoreQueryOptions,
Expand Down Expand Up @@ -149,6 +150,7 @@ export interface BaseMutationOptions<
awaitRefetchQueries?: boolean;
errorPolicy?: ErrorPolicy;
update?: MutationUpdaterFunction<TData, TVariables, TContext>;
reobserveQuery?: ReobserveQueryCallback;
client?: ApolloClient<object>;
notifyOnNetworkStatusChange?: boolean;
context?: TContext;
Expand All @@ -168,6 +170,7 @@ export interface MutationFunctionOptions<
refetchQueries?: Array<string | PureQueryOptions> | RefetchQueriesFunction;
awaitRefetchQueries?: boolean;
update?: MutationUpdaterFunction<TData, TVariables, TContext>;
reobserveQuery?: ReobserveQueryCallback;
context?: TContext;
fetchPolicy?: WatchQueryFetchPolicy;
}
Expand Down

0 comments on commit 4ca6362

Please sign in to comment.