From 75bd71d960a09661b8fcc61c47e5baca396c7de2 Mon Sep 17 00:00:00 2001 From: Dhaivat Pandya Date: Mon, 1 Aug 2016 13:51:09 -0700 Subject: [PATCH 1/3] fixed issue with the aliased names in query batching --- src/batching/queryMerging.ts | 7 ++++- test/queryMerging.ts | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/batching/queryMerging.ts b/src/batching/queryMerging.ts index c3afb171f2d..12df208ed93 100644 --- a/src/batching/queryMerging.ts +++ b/src/batching/queryMerging.ts @@ -96,7 +96,12 @@ 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]; + + // If this field has been aliased, then we need the alias name + // (as opposed to the field name) as the key of the result + // structure that we return. + const keyName = field.alias ? field.alias.value : field.name.value; + data[keyName] = result.data[dataKey]; if (resultArray[childRequestIndex]) { assign(resultArray[childRequestIndex].data, data); diff --git a/test/queryMerging.ts b/test/queryMerging.ts index 5d12d3b46cd..67d5f03814c 100644 --- a/test/queryMerging.ts +++ b/test/queryMerging.ts @@ -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 }); + }); }); From 8ce2576eedf8d2ace6c8560a4494403a6ed54bcc Mon Sep 17 00:00:00 2001 From: Dhaivat Pandya Date: Mon, 1 Aug 2016 13:57:01 -0700 Subject: [PATCH 2/3] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f990bbe89d..ef1dc5eaf17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 08d5c4678995cb5e8aed1339a12cc9863a415f90 Mon Sep 17 00:00:00 2001 From: Dhaivat Pandya Date: Mon, 1 Aug 2016 15:42:07 -0700 Subject: [PATCH 3/3] moved to resultKeyNameFromField --- src/batching/queryMerging.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/batching/queryMerging.ts b/src/batching/queryMerging.ts index 12df208ed93..73d344695b8 100644 --- a/src/batching/queryMerging.ts +++ b/src/batching/queryMerging.ts @@ -49,6 +49,10 @@ import { Request, } from '../networkInterface'; +import { + resultKeyNameFromField, +} from '../data/storeUtils'; + import assign = require('lodash.assign'); import cloneDeep = require('lodash.clonedeep'); @@ -96,12 +100,7 @@ export function unpackMergedResult(result: GraphQLResult, const childRequestIndex = mergeInfo.requestIndex; const fieldMap = fieldMaps[childRequestIndex]; const field = fieldMap[mergeInfo.fieldIndex]; - - // If this field has been aliased, then we need the alias name - // (as opposed to the field name) as the key of the result - // structure that we return. - const keyName = field.alias ? field.alias.value : field.name.value; - data[keyName] = result.data[dataKey]; + data[resultKeyNameFromField(field)] = result.data[dataKey]; if (resultArray[childRequestIndex]) { assign(resultArray[childRequestIndex].data, data);