Skip to content

Commit

Permalink
Observable query reducers called with correct data
Browse files Browse the repository at this point in the history
  • Loading branch information
armstrjare committed Feb 7, 2017
1 parent 449a96e commit 4232c47
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,14 +877,16 @@ export class QueryManager {

private getExtraReducers(): ApolloReducer[] {
return Object.keys(this.observableQueries).map( obsQueryId => {
const queryOptions = this.observableQueries[obsQueryId].observableQuery.options;
const query = this.observableQueries[obsQueryId].observableQuery;
const queryOptions = query.options;

if (queryOptions.reducer) {
return createStoreReducer(
queryOptions.reducer,
queryOptions.query,
queryOptions.variables || {},
query.variables || {},
this.reducerConfig,
);
);
}
return null as never;
}).filter( reducer => reducer !== null );
Expand Down
60 changes: 60 additions & 0 deletions test/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,66 @@ describe('ObservableQuery', () => {
}
});
});

it('applies query reducers with correct variables', (done) => {
const queryManager = mockQueryManager({
// First we make the query
request: { query, variables },
result: { data: dataOne },
}, {
// Then we make a mutation
request: { query: mutation },
result: { data: mutationData },
}, {
// Then we make another query
request: { query, variables: differentVariables },
result: { data: dataTwo },
}, {
// Then we make another mutation
request: { query: mutation },
result: { data: mutationData },
});


let lastReducerVars: Array<Object> = [];
let lastReducerData: Array<Object> = [];
const observable = queryManager.watchQuery({
query,
variables,
reducer: (previous, action, reducerVars) => {
if (action.type === 'APOLLO_MUTATION_RESULT') {
// We want to track the history of the `variables` the reducer
// is given for the query.
lastReducerData.push(previous);
lastReducerVars.push(reducerVars);
}

return previous;
}
});

// Check that the variables fed into the reducer are correct.
function assertVariables() {
assert.lengthOf(lastReducerVars, 2);
assert.deepEqual(lastReducerVars[0], variables);
assert.deepEqual(lastReducerData[0], dataOne);
assert.deepEqual(lastReducerVars[1], differentVariables);
assert.deepEqual(lastReducerData[1], dataTwo);
done();
}

// Subscribe to the query, then run the mutation, then change the variables, then run another mutation.
let sub = observable.subscribe({});
queryManager.mutate({ mutation }).then(() => {
observable.setVariables(differentVariables);
queryManager.mutate({ mutation }).then(() => {
// We have to get out of the Promise scope here
// because the promises are capturing the assertion errors
// leading to timesouts.
setTimeout(assertVariables, 0);
});
});
});
});
});
});

0 comments on commit 4232c47

Please sign in to comment.