Repository: apollographql/apollo-client

Problem statement:
Partial data return prematurely fails (watchQuery)
Hey folks,

Getting following error, when using `returnPartialData` with `watchQuery` (looks like it's trying to get the data from Store, before it's available)

```
store.js:11Caught an exception! Error: Can't find field posts on object [object Object].(…)
```



Hints:
This is a pretty bad bug! Thanks for the report, we should get it fixed ASAP.

Oops, looks like we have exactly 0 unit tests for `returnPartialData`. That explains why that broke...


Patch:
diff --git a/src/QueryManager.ts b/src/QueryManager.ts
--- a/src/QueryManager.ts
+++ b/src/QueryManager.ts
@@ -195,6 +195,7 @@ export class QueryManager {
               rootId: queryStoreValue.query.id,
               selectionSet: queryStoreValue.query.selectionSet,
               variables: queryStoreValue.variables,
+              returnPartialData: options.returnPartialData,
             });
 
             if (observer.next) {
diff --git a/src/data/readFromStore.ts b/src/data/readFromStore.ts
--- a/src/data/readFromStore.ts
+++ b/src/data/readFromStore.ts
@@ -65,11 +65,13 @@ export function readSelectionSetFromStore({
   rootId,
   selectionSet,
   variables,
+  returnPartialData = false,
 }: {
   store: NormalizedCache,
   rootId: string,
   selectionSet: SelectionSet,
   variables: Object,
+  returnPartialData?: boolean,
 }): Object {
   const {
     result,
@@ -77,7 +79,7 @@ export function readSelectionSetFromStore({
     selectionSet,
     rootId,
     store,
-    throwOnMissingField: true,
+    throwOnMissingField: !returnPartialData,
     variables,
   });
 


Test patch:
diff --git a/test/QueryManager.ts b/test/QueryManager.ts
--- a/test/QueryManager.ts
+++ b/test/QueryManager.ts
@@ -689,6 +689,80 @@ describe('QueryManager', () => {
     });
   });
 
+  it('supports returnPartialData #193', () => {
+    const primeQuery = gql`
+      query primeQuery {
+        people_one(id: 1) {
+          name
+        }
+      }
+    `;
+
+    const complexQuery = gql`
+      query complexQuery {
+        luke: people_one(id: 1) {
+          name
+        }
+        vader: people_one(id: 4) {
+          name
+        }
+      }
+    `;
+
+    const diffedQuery = gql`
+      query complexQuery {
+        vader: people_one(id: 4) {
+          name
+        }
+      }
+    `;
+
+    const data1 = {
+      people_one: {
+        name: 'Luke Skywalker',
+      },
+    };
+
+    const data2 = {
+      vader: {
+        name: 'Darth Vader',
+      },
+    };
+
+    const networkInterface = mockNetworkInterface(
+      {
+        request: { query: primeQuery },
+        result: { data: data1 },
+      },
+      {
+        request: { query: diffedQuery },
+        result: { data: data2 },
+        delay: 5,
+      }
+    );
+
+    const queryManager = new QueryManager({
+      networkInterface,
+      store: createApolloStore(),
+      reduxRootKey: 'apollo',
+    });
+
+    // First, prime the store so that query diffing removes the query
+    queryManager.query({
+      query: primeQuery,
+    }).then(() => {
+      const handle = queryManager.watchQuery({
+        query: complexQuery,
+        returnPartialData: true,
+      });
+
+      return handle.result().then((result) => {
+        assert.equal(result.data['luke'].name, 'Luke Skywalker');
+        assert.notProperty(result.data, 'vader');
+      });
+    });
+  });
+
   it('runs a mutation', () => {
     const mutation = gql`
       mutation makeListPrivate {