Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query alias batching issue #493

Merged
merged 4 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- Fixed issue with alias names in batched queries. [PR #493](https://github.com/apollostack/apollo-client/pull/493) and [Issue #490](https://github.com/apollostack/apollo-client/issues).

### v0.4.9

Expand Down
6 changes: 5 additions & 1 deletion src/batching/queryMerging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import {
Request,
} from '../networkInterface';

import {
resultKeyNameFromField,
} from '../data/storeUtils';

import assign = require('lodash.assign');
import cloneDeep = require('lodash.clonedeep');

Expand Down Expand Up @@ -96,7 +100,7 @@ export function unpackMergedResult(result: GraphQLResult,
const childRequestIndex = mergeInfo.requestIndex;
const fieldMap = fieldMaps[childRequestIndex];
const field = fieldMap[mergeInfo.fieldIndex];
data[field.name.value] = result.data[dataKey];
data[resultKeyNameFromField(field)] = result.data[dataKey];

if (resultArray[childRequestIndex]) {
assign(resultArray[childRequestIndex].data, data);
Expand Down
50 changes: 50 additions & 0 deletions test/queryMerging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,54 @@ describe('Query merging', () => {
const mergedRequest = mergeRequests([{query: query1}, {query: query2}]);
assert.equal(print(mergedRequest.query), print(expQuery));
});

it('should be able to correctly merge queries with aliases', () => {
const query = gql`
query firstQuery {
someAlias: author {
firstName
lastName
}
}`;
const secondQuery = gql`
query secondQuery {
person {
name
}
}`;
const expQuery = gql`
query ___composed {
___firstQuery___requestIndex_0___fieldIndex_0: author {
firstName
lastName
}

___secondQuery___requestIndex_1___fieldIndex_0: person {
name
}
}`;
const firstResult = {
someAlias: {
firstName: 'John',
lastName: 'Smith',
},
};
const secondResult = {
person: {
name: 'Jane Smith',
},
};
const composedResult = {
___firstQuery___requestIndex_0___fieldIndex_0: firstResult.someAlias,
___secondQuery___requestIndex_1___fieldIndex_0: secondResult.person,
};
const requests = [{ query }, { query: secondQuery }];
const mergedRequest = mergeRequests(requests);
assert.equal(print(mergedRequest.query), print(expQuery));

const unpackedResults = unpackMergedResult({ data: composedResult }, requests);
assert.equal(unpackedResults.length, 2);
assert.deepEqual(unpackedResults[0], { data: firstResult });
assert.deepEqual(unpackedResults[1], { data: secondResult });
});
});