From fffc333a6e4382a3c74322f375877fff7215369c Mon Sep 17 00:00:00 2001 From: abhiaiyer91 Date: Mon, 11 Jul 2016 14:39:09 -0700 Subject: [PATCH] refactor data reducer --- src/QueryManager.ts | 8 ++++++++ src/actions.ts | 12 ++++++++++++ src/data/store.ts | 19 +++++-------------- src/store.ts | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/QueryManager.ts b/src/QueryManager.ts index 5667d9465a6..bab73e358bb 100644 --- a/src/QueryManager.ts +++ b/src/QueryManager.ts @@ -249,6 +249,7 @@ export class QueryManager { variables, mutationId, fragmentMap: queryFragmentMap, + mutations: this.getApolloState().mutations, }); return this.networkInterface.query(request) @@ -258,6 +259,7 @@ export class QueryManager { result, mutationId, resultBehaviors, + mutations: this.getApolloState().mutations, }); return result; @@ -453,6 +455,7 @@ export class QueryManager { this.store.dispatch({ type: 'APOLLO_QUERY_STOP', queryId, + queries: this.getApolloState().queries, }); }; @@ -624,6 +627,7 @@ export class QueryManager { queryId, requestId, fragmentMap: queryFragmentMap, + queries: this.getApolloState().queries, }); if (! minimizedQuery || returnPartialData) { @@ -636,6 +640,7 @@ export class QueryManager { query: querySS, complete: !! minimizedQuery, queryId, + queries: this.getApolloState().queries }); } @@ -663,6 +668,7 @@ export class QueryManager { result, queryId, requestId, + queries: this.getApolloState().queries }); this.removeFetchQueryPromise(requestId); @@ -696,6 +702,7 @@ export class QueryManager { error, queryId, requestId, + queries: this.getApolloState().queries, }); this.removeFetchQueryPromise(requestId); @@ -740,6 +747,7 @@ export class QueryManager { this.store.dispatch({ type: 'APOLLO_QUERY_STOP', queryId, + queries: this.getApolloState().queries, }); } diff --git a/src/actions.ts b/src/actions.ts index ac83e9cd4fc..1bdc8943b44 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -4,8 +4,13 @@ import { import { SelectionSetWithRoot, + QueryStore, } from './queries/store'; +import { + MutationStore, +} from './mutations/store'; + import { MutationBehavior, } from './data/mutationResults'; @@ -15,6 +20,7 @@ import { FragmentMap } from './queries/getFromAST'; export interface QueryResultAction { type: 'APOLLO_QUERY_RESULT'; result: GraphQLResult; + queries: QueryStore; queryId: string; requestId: number; } @@ -27,6 +33,7 @@ export interface QueryErrorAction { type: 'APOLLO_QUERY_ERROR'; error: Error; queryId: string; + queries: QueryStore; requestId: number; } @@ -45,6 +52,7 @@ export interface QueryInitAction { returnPartialData: boolean; queryId: string; requestId: number; + queries: QueryStore; fragmentMap: FragmentMap; } @@ -56,6 +64,7 @@ export interface QueryResultClientAction { type: 'APOLLO_QUERY_RESULT_CLIENT'; result: GraphQLResult; complete: boolean; + queries: QueryStore; queryId: string; } @@ -66,6 +75,7 @@ export function isQueryResultClientAction(action: ApolloAction): action is Query export interface QueryStopAction { type: 'APOLLO_QUERY_STOP'; queryId: string; + queries: QueryStore; } export function isQueryStopAction(action: ApolloAction): action is QueryStopAction { @@ -79,6 +89,7 @@ export interface MutationInitAction { variables: Object; mutationId: string; fragmentMap: FragmentMap; + mutations: MutationStore; } export function isMutationInitAction(action: ApolloAction): action is MutationInitAction { @@ -90,6 +101,7 @@ export interface MutationResultAction { result: GraphQLResult; mutationId: string; resultBehaviors?: MutationBehavior[]; + mutations: MutationStore; } export function isMutationResultAction(action: ApolloAction): action is MutationResultAction { diff --git a/src/data/store.ts b/src/data/store.ts index 0f7bc85ce2f..8c2bbaf053e 100644 --- a/src/data/store.ts +++ b/src/data/store.ts @@ -11,14 +11,6 @@ import { import assign = require('lodash.assign'); -import { - QueryStore, -} from '../queries/store'; - -import { - MutationStore, -} from '../mutations/store'; - import { ApolloReducerConfig, } from '../store'; @@ -46,25 +38,24 @@ export type StoreValue = number | string | string[]; export function data( previousState: NormalizedCache = {}, action: ApolloAction, - queries: QueryStore, - mutations: MutationStore, config: ApolloReducerConfig ): NormalizedCache { + if (isQueryResultAction(action)) { - if (!queries[action.queryId]) { + if (!action.queries[action.queryId]) { return previousState; } // Ignore results from old requests // XXX this means that if you have a refetch interval which is shorter than your roundtrip time, // your query will be in the loading state forever! - if (action.requestId < queries[action.queryId].lastRequestId) { + if (action.requestId < action.queries[action.queryId].lastRequestId) { return previousState; } // XXX handle partial result due to errors if (! graphQLResultHasError(action.result)) { - const queryStoreValue = queries[action.queryId]; + const queryStoreValue = action.queries[action.queryId]; // XXX use immutablejs instead of cloning const clonedState = assign({}, previousState) as NormalizedCache; @@ -84,7 +75,7 @@ export function data( } else if (isMutationResultAction(action)) { // Incorporate the result from this mutation into the store if (!action.result.errors) { - const queryStoreValue = mutations[action.mutationId]; + const queryStoreValue = action.mutations[action.mutationId]; // XXX use immutablejs instead of cloning const clonedState = assign({}, previousState) as NormalizedCache; diff --git a/src/store.ts b/src/store.ts index 71cc9f4f698..1c17d392913 100644 --- a/src/store.ts +++ b/src/store.ts @@ -65,7 +65,7 @@ export function createApolloReducer(config: ApolloReducerConfig): Function { // Note that we are passing the queries into this, because it reads them to associate // the query ID in the result with the actual query - data: data(state.data, action, state.queries, state.mutations, config), + data: data(state.data, action, config), }; return newState;