Repository: apollographql/apollo-client

Problem statement:
Renaming queries not working with batching
If I send the following query in a request that is batched with other queries...

```
query getActivity {
  activities: lookActivities(skip: 0, limit: 10) {
    _id
    type
    owner {
      _id
      name
    }
  }
}
```

...it will give me the following error:

![image](https://cloud.githubusercontent.com/assets/7143583/17286384/34a637e4-577e-11e6-861e-976a9238644c.png)

However, if it isn't batched with other queries, it will work fine. It will also work fine if I don't rename the query:

```
query getActivity {
  lookActivities(skip: 0, limit: 10) {
    _id
    type
    owner {
      _id
      name
    }
  }
}
```



Hints:

Patch:
diff --git a/src/batching/queryMerging.ts b/src/batching/queryMerging.ts
--- 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,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);


Test patch:
diff --git a/test/queryMerging.ts b/test/queryMerging.ts
--- 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 });
+  });
 });