Skip to content

Commit

Permalink
Rename mutation result actions to mutation behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashko Stubailo committed Jun 29, 2016
1 parent 1346132 commit db0a113
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 71 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Expect active development and potentially significant breaking changes in the `0

### vNEXT

- **Add mutation result handling to Apollo Client.** This is done by passing an `applyResult` option to
- **Add mutation result handling to Apollo Client.** This is done by passing an `resultBehaviors` option to
`client.mutate`, with an array of "Mutation Result Actions". You can attach any number of result
actions to each mutation. These result actions are attached to the `MUTATION_RESULT` redux action
that is dispatched when the query result arrives from the store, and are handled by special
"Mutation Result Reducers". These are special because they get a whole bunch of GraphQL-specific
information in the arguments, and are all called synchronously when the result of a mutation
arrives. In this version, Apollo Client ships with a set of default mutation result actions/reducers
including `ARRAY_INSERT`, `DELETE`, and `ARRAY_DELETE`, but you can add any custom ones you want
by passing the new `mutationResultReducers` option to the `ApolloClient` constructor. The previous
by passing the new `mutationBehaviorReducers` option to the `ApolloClient` constructor. The previous
default functionality of merging all mutation results into the store is preserved.
[PR #320](https://github.com/apollostack/apollo-client/pull/320)
[Read the design in depth in Issue #317](https://github.com/apollostack/apollo-client/issues/317)
Expand Down
8 changes: 4 additions & 4 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
} from './data/diffAgainstStore';

import {
MutationApplyResultAction,
MutationBehavior,
} from './data/mutationResults';

import {
Expand Down Expand Up @@ -177,11 +177,11 @@ export class QueryManager {
public mutate({
mutation,
variables,
applyResult,
resultBehaviors,
}: {
mutation: Document,
variables?: Object,
applyResult?: MutationApplyResultAction[],
resultBehaviors?: MutationBehavior[],
}): Promise<GraphQLResult> {
const mutationId = this.generateQueryId();

Expand Down Expand Up @@ -218,7 +218,7 @@ export class QueryManager {
type: 'APOLLO_MUTATION_RESULT',
result,
mutationId,
applyResult,
resultBehaviors,
});

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from './queries/store';

import {
MutationApplyResultAction,
MutationBehavior,
} from './data/mutationResults';

import { FragmentMap } from './queries/getFromAST';
Expand Down Expand Up @@ -89,7 +89,7 @@ export interface MutationResultAction {
type: 'APOLLO_MUTATION_RESULT';
result: GraphQLResult;
mutationId: string;
applyResult?: MutationApplyResultAction[];
resultBehaviors?: MutationBehavior[];
}

export function isMutationResultAction(action: ApolloAction): action is MutationResultAction {
Expand Down
58 changes: 29 additions & 29 deletions src/data/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ import {
writeSelectionSetToStore,
} from './writeToStore';

// Mutation result action types, these can be used in the `applyResult` argument to client.mutate
// Mutation behavior types, these can be used in the `resultBehaviors` argument to client.mutate

export type MutationApplyResultAction =
MutationArrayInsertAction |
MutationArrayDeleteAction |
MutationDeleteAction;
export type MutationBehavior =
MutationArrayInsertBehavior |
MutationArrayDeleteBehavior |
MutationDeleteBehavior;

export type MutationArrayInsertAction = {
export type MutationArrayInsertBehavior = {
type: 'ARRAY_INSERT';
resultPath: StorePath;
storePath: StorePath;
where: ArrayInsertWhere;
}

export type MutationDeleteAction = {
export type MutationDeleteBehavior = {
type: 'DELETE';
dataId: string;
}

export type MutationArrayDeleteAction = {
export type MutationArrayDeleteBehavior = {
type: 'ARRAY_DELETE';
storePath: StorePath;
dataId: string;
Expand All @@ -60,36 +60,36 @@ export type ArrayInsertWhere =
'APPEND';

// These are the generic arguments passed into the mutation result reducers
// The `action` field is specific to each reducer
export type MutationResultReducerArgs = {
action: MutationApplyResultAction;
// The `behavior` field is specific to each reducer
export type MutationBehaviorReducerArgs = {
behavior: MutationBehavior;
result: GraphQLResult;
variables: any;
fragmentMap: FragmentMap;
selectionSet: SelectionSet;
config: ApolloReducerConfig;
}

export type MutationResultReducerMap = {
[type: string]: MutationResultReducer;
export type MutationBehaviorReducerMap = {
[type: string]: MutationBehaviorReducer;
}

export type MutationResultReducer = (state: NormalizedCache, args: MutationResultReducerArgs) => NormalizedCache;
export type MutationBehaviorReducer = (state: NormalizedCache, args: MutationBehaviorReducerArgs) => NormalizedCache;

// Reducer for ARRAY_INSERT action
// Reducer for ARRAY_INSERT behavior
function mutationResultArrayInsertReducer(state: NormalizedCache, {
action,
behavior,
result,
variables,
fragmentMap,
selectionSet,
config,
}: MutationResultReducerArgs): NormalizedCache {
}: MutationBehaviorReducerArgs): NormalizedCache {
const {
resultPath,
storePath,
where,
} = action as MutationArrayInsertAction;
} = behavior as MutationArrayInsertBehavior;

// Step 1: get selection set and result for resultPath
const scopedSelectionSet = scopeSelectionSetToResultPath({
Expand Down Expand Up @@ -148,13 +148,13 @@ function generateMutationResultDataId() {
return `ARRAY_INSERT-gen-id-${currId}`;
}

// Reducer for 'DELETE' action
// Reducer for 'DELETE' behavior
function mutationResultDeleteReducer(state: NormalizedCache, {
action,
}: MutationResultReducerArgs): NormalizedCache {
behavior,
}: MutationBehaviorReducerArgs): NormalizedCache {
const {
dataId,
} = action as MutationDeleteAction;
} = behavior as MutationDeleteBehavior;

// Delete the object
delete state[dataId];
Expand Down Expand Up @@ -201,14 +201,14 @@ function cleanArray(arr, dataId) {
}
}

// Reducer for 'ARRAY_DELETE' action
// Reducer for 'ARRAY_DELETE' behavior
function mutationResultArrayDeleteReducer(state: NormalizedCache, {
action,
}: MutationResultReducerArgs): NormalizedCache {
behavior,
}: MutationBehaviorReducerArgs): NormalizedCache {
const {
dataId,
storePath,
} = action as MutationArrayDeleteAction;
} = behavior as MutationArrayDeleteBehavior;

const dataIdOfObj = storePath.shift();
const clonedObj = cloneDeep(state[dataIdOfObj]);
Expand All @@ -224,9 +224,9 @@ function mutationResultArrayDeleteReducer(state: NormalizedCache, {
}) as NormalizedCache;
}

// Combines all of the default reducers into a map based on the action type they accept
// The action type is used to pick the right reducer when evaluating the result of the mutation
export const defaultMutationResultReducers: { [type: string]: MutationResultReducer } = {
// Combines all of the default reducers into a map based on the behavior type they accept
// The behavior type is used to pick the right reducer when evaluating the result of the mutation
export const defaultMutationBehaviorReducers: { [type: string]: MutationBehaviorReducer } = {
'ARRAY_INSERT': mutationResultArrayInsertReducer,
'DELETE': mutationResultDeleteReducer,
'ARRAY_DELETE': mutationResultArrayDeleteReducer,
Expand Down
22 changes: 11 additions & 11 deletions src/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
} from './storeUtils';

import {
defaultMutationResultReducers,
MutationResultReducerArgs,
defaultMutationBehaviorReducers,
MutationBehaviorReducerArgs,
} from './mutationResults';

export interface NormalizedCache {
Expand Down Expand Up @@ -98,23 +98,23 @@ export function data(
fragmentMap: queryStoreValue.fragmentMap,
});

if (action.applyResult) {
action.applyResult.forEach((applyResultAction) => {
const args: MutationResultReducerArgs = {
action: applyResultAction,
if (action.resultBehaviors) {
action.resultBehaviors.forEach((behavior) => {
const args: MutationBehaviorReducerArgs = {
behavior,
result: action.result,
variables: queryStoreValue.variables,
fragmentMap: queryStoreValue.fragmentMap,
selectionSet: queryStoreValue.mutation.selectionSet,
config,
};

if (defaultMutationResultReducers[applyResultAction.type]) {
newState = defaultMutationResultReducers[applyResultAction.type](newState, args);
} else if (config.mutationResultReducers[applyResultAction.type]) {
newState = config.mutationResultReducers[applyResultAction.type](newState, args);
if (defaultMutationBehaviorReducers[behavior.type]) {
newState = defaultMutationBehaviorReducers[behavior.type](newState, args);
} else if (config.mutationBehaviorReducers[behavior.type]) {
newState = config.mutationBehaviorReducers[behavior.type](newState, args);
} else {
throw new Error(`No mutation result reducer defined for type ${applyResultAction.type}`);
throw new Error(`No mutation result reducer defined for type ${behavior.type}`);
}
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import {
} from './queries/queryTransform';

import {
MutationApplyResultAction,
MutationResultReducerMap,
MutationBehavior,
MutationBehaviorReducerMap,
} from './data/mutationResults';

import isUndefined = require('lodash.isundefined');
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class ApolloClient {
shouldBatch = false,
ssrMode = false,
ssrForceFetchDelay = 0,
mutationResultReducers = {} as MutationResultReducerMap,
mutationBehaviorReducers = {} as MutationBehaviorReducerMap,
}: {
networkInterface?: NetworkInterface,
reduxRootKey?: string,
Expand All @@ -89,7 +89,7 @@ export default class ApolloClient {
shouldBatch?: boolean,
ssrMode?: boolean,
ssrForceFetchDelay?: number
mutationResultReducers?: MutationResultReducerMap,
mutationBehaviorReducers?: MutationBehaviorReducerMap,
} = {}) {
this.reduxRootKey = reduxRootKey ? reduxRootKey : 'apollo';
this.initialState = initialState ? initialState : {};
Expand All @@ -106,7 +106,7 @@ export default class ApolloClient {

this.reducerConfig = {
dataIdFromObject,
mutationResultReducers,
mutationBehaviorReducers,
};
}

Expand All @@ -128,7 +128,7 @@ export default class ApolloClient {

public mutate = (options: {
mutation: Document,
applyResult?: MutationApplyResultAction[],
resultBehaviors?: MutationBehavior[],
variables?: Object,
}): Promise<GraphQLResult> => {
this.initStore();
Expand Down
4 changes: 2 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from './data/extensions';

import {
MutationResultReducerMap,
MutationBehaviorReducerMap,
} from './data/mutationResults';

export interface Store {
Expand Down Expand Up @@ -109,5 +109,5 @@ export function createApolloStore({

export interface ApolloReducerConfig {
dataIdFromObject?: IdGetter;
mutationResultReducers?: MutationResultReducerMap;
mutationBehaviorReducers?: MutationBehaviorReducerMap;
}
Loading

0 comments on commit db0a113

Please sign in to comment.