Skip to content

Commit

Permalink
Rename mutation reobserveQuery callback to onQueryUpdated.
Browse files Browse the repository at this point in the history
Previously, options.onDirty (for cache.batch) and options.reobserveQuery
(for mutations) had no obvious relationship, even though one is
implemented in terms of the other.

Now, cache.batch takes an onWatchUpdated callback and mutations take
onQueryUpdated, which I think better represents the similarity (as well as
the crucial difference in argument types) between the callbacks.

Also, the options.onQueryUpdated callback is typically triggered by the
mutation's options.update function, so the "onQueryUpdated" terminology
more accurately reflects the source of the updates.
  • Loading branch information
benjamn committed May 12, 2021
1 parent a13657d commit a205504
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
ApolloQueryResult,
OperationVariables,
MutationUpdaterFunction,
ReobserveQueryCallback,
OnQueryUpdated,
} from './types';
import { LocalState } from './LocalState';

Expand Down Expand Up @@ -139,7 +139,7 @@ export class QueryManager<TStore> {
refetchQueries = [],
awaitRefetchQueries = false,
update: updateWithProxyFn,
reobserveQuery,
onQueryUpdated,
errorPolicy = 'none',
fetchPolicy,
context,
Expand Down Expand Up @@ -236,7 +236,7 @@ export class QueryManager<TStore> {
context,
updateQueries,
update: updateWithProxyFn,
reobserveQuery,
onQueryUpdated,
});
} catch (e) {
// Likewise, throwing an error from the asyncMap mapping function
Expand Down Expand Up @@ -311,7 +311,7 @@ export class QueryManager<TStore> {
context?: TContext;
updateQueries: UpdateQueries<TData>;
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
reobserveQuery?: ReobserveQueryCallback;
onQueryUpdated?: OnQueryUpdated;
},
cache = this.cache,
): Promise<void> {
Expand Down Expand Up @@ -382,11 +382,11 @@ export class QueryManager<TStore> {
// Write the final mutation.result to the root layer of the cache.
optimistic: false,

onWatchUpdated: mutation.reobserveQuery && ((watch, diff) => {
onWatchUpdated: mutation.onQueryUpdated && ((watch, diff) => {
if (watch.watcher instanceof QueryInfo) {
const oq = watch.watcher.observableQuery;
if (oq) {
reobserveResults.push(mutation.reobserveQuery!(oq, diff));
reobserveResults.push(mutation.onQueryUpdated!(oq, diff));
// Prevent the normal cache broadcast of this result.
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5227,7 +5227,7 @@ describe('QueryManager', () => {
});
});

describe('reobserveQuery', () => {
describe('onQueryUpdated', () => {
const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
Expand Down Expand Up @@ -5316,7 +5316,7 @@ describe('QueryManager', () => {
});
},

reobserveQuery(obsQuery) {
onQueryUpdated(obsQuery) {
expect(obsQuery.options.query).toBe(query);
return obsQuery.refetch().then(async () => {
// Wait a bit to make sure the mutation really awaited the
Expand Down Expand Up @@ -5374,7 +5374,7 @@ describe('QueryManager', () => {
});
},

reobserveQuery(obsQuery) {
onQueryUpdated(obsQuery) {
expect(obsQuery.options.query).toBe(query);
return obsQuery.refetch();
},
Expand Down Expand Up @@ -5415,7 +5415,7 @@ describe('QueryManager', () => {
cache.evict({ fieldName: "author" });
},

reobserveQuery(obsQuery) {
onQueryUpdated(obsQuery) {
expect(obsQuery.options.query).toBe(query);
return obsQuery.reobserve({
fetchPolicy: "network-only",
Expand Down
2 changes: 1 addition & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type DefaultContext = Record<string, any>;

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

export type ReobserveQueryCallback = (
export type OnQueryUpdated = (
observableQuery: ObservableQuery,
diff: Cache.DiffResult<any>,
) => void | Promise<any>;
Expand Down
4 changes: 2 additions & 2 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PureQueryOptions,
OperationVariables,
MutationUpdaterFunction,
ReobserveQueryCallback,
OnQueryUpdated,
} from './types';
import { ApolloCache } from '../cache';

Expand Down Expand Up @@ -260,7 +260,7 @@ export interface MutationBaseOptions<
* A function that will be called for each ObservableQuery affected by
* this mutation, after the mutation has completed.
*/
reobserveQuery?: ReobserveQueryCallback;
onQueryUpdated?: OnQueryUpdated;

/**
* Specifies the {@link ErrorPolicy} to be used for this operation
Expand Down
20 changes: 10 additions & 10 deletions src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ describe('useMutation Hook', () => {
});

describe('refetching queries', () => {
itAsync('can pass reobserveQuery to useMutation', (resolve, reject) => {
itAsync('can pass onQueryUpdated to useMutation', (resolve, reject) => {
interface TData {
todoCount: number;
}
Expand Down Expand Up @@ -791,17 +791,17 @@ describe('useMutation Hook', () => {
}).setOnError(reject),
});

// The goal of this test is to make sure reobserveQuery gets called as
// The goal of this test is to make sure onQueryUpdated gets called as
// part of the createTodo mutation, so we use this reobservePromise to
// await the calling of reobserveQuery.
interface ReobserveResults {
// await the calling of onQueryUpdated.
interface OnQueryUpdatedResults {
obsQuery: ObservableQuery;
diff: Cache.DiffResult<TData>;
result: ApolloQueryResult<TData>;
}
let reobserveResolve: (results: ReobserveResults) => any;
const reobservePromise = new Promise<ReobserveResults>(resolve => {
reobserveResolve = resolve;
let resolveOnUpdate: (results: OnQueryUpdatedResults) => any;
const onUpdatePromise = new Promise<OnQueryUpdatedResults>(resolve => {
resolveOnUpdate = resolve;
});
let finishedReobserving = false;

Expand Down Expand Up @@ -838,10 +838,10 @@ describe('useMutation Hook', () => {
act(() => {
createTodo({
variables,
reobserveQuery(obsQuery, diff) {
onQueryUpdated(obsQuery, diff) {
return obsQuery.reobserve().then(result => {
finishedReobserving = true;
reobserveResolve({ obsQuery, diff, result });
resolveOnUpdate({ obsQuery, diff, result });
});
},
});
Expand Down Expand Up @@ -888,7 +888,7 @@ describe('useMutation Hook', () => {
</ApolloProvider>
);

return reobservePromise.then(results => {
return onUpdatePromise.then(results => {
expect(finishedReobserving).toBe(true);

expect(results.diff).toEqual({
Expand Down
6 changes: 3 additions & 3 deletions src/react/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ObservableQuery,
OperationVariables,
PureQueryOptions,
ReobserveQueryCallback,
OnQueryUpdated,
WatchQueryFetchPolicy,
WatchQueryOptions,
} from '../../core';
Expand Down Expand Up @@ -154,7 +154,7 @@ export interface BaseMutationOptions<
awaitRefetchQueries?: boolean;
errorPolicy?: ErrorPolicy;
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
reobserveQuery?: ReobserveQueryCallback;
onQueryUpdated?: OnQueryUpdated;
client?: ApolloClient<object>;
notifyOnNetworkStatusChange?: boolean;
context?: TContext;
Expand All @@ -175,7 +175,7 @@ export interface MutationFunctionOptions<
refetchQueries?: Array<string | PureQueryOptions> | RefetchQueriesFunction;
awaitRefetchQueries?: boolean;
update?: MutationUpdaterFunction<TData, TVariables, TContext, TCache>;
reobserveQuery?: ReobserveQueryCallback;
onQueryUpdated?: OnQueryUpdated;
context?: TContext;
fetchPolicy?: WatchQueryFetchPolicy;
}
Expand Down

0 comments on commit a205504

Please sign in to comment.