From 5ae2fd147b1a0310efd10dbcd14f6357e9681a15 Mon Sep 17 00:00:00 2001 From: David Alan Hjelle Date: Wed, 6 Jul 2016 15:23:55 -0500 Subject: [PATCH 1/4] Include any available fields in response if `throwOnMissingField` is false. --- CHANGELOG.md | 2 ++ src/data/diffAgainstStore.ts | 6 ++-- test/diffAgainstStore.ts | 64 +++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98bf937464a..e4d0de1f7ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT +- Made sure `diffSelectionSetAgainstStore` will return any available data from the local cache if `throwOnMissingField` is `false`, even if some fields in the query are missing. This also means that the `returnPartialData` option of `watchQuery` will return partial data if some fields are missing in the cache, rather than an empty object. [Issue #359](https://github.com/apollostack/apollo-client/issues/359) and [PR #360](https://github.com/apollostack/apollo-client/pull/360). + ### v0.3.27 - Removed dependency on `graphql` npm package, which was causing compilation errors in the React Native bundler. Issues [#261](https://github.com/apollostack/apollo-client/issues/261) [#163](https://github.com/apollostack/apollo-client/issues/163), [PR #357](https://github.com/apollostack/apollo-client/pull/357) diff --git a/src/data/diffAgainstStore.ts b/src/data/diffAgainstStore.ts index 673821d4396..3b1799ebb47 100644 --- a/src/data/diffAgainstStore.ts +++ b/src/data/diffAgainstStore.ts @@ -147,14 +147,14 @@ export function diffSelectionSetAgainstStore({ included: includeField, }); + const resultFieldKey = resultKeyNameFromField(selection); if (fieldIsMissing) { // even if the field is not included, we want to keep it in the // query that is sent to the server. So, we push it into the set of // fields that is missing. pushMissingField(selection); - } else if (includeField) { - const resultFieldKey = resultKeyNameFromField(selection); - + } + if (includeField && fieldResult !== undefined) { result[resultFieldKey] = fieldResult; } } else if (isInlineFragment(selection)) { diff --git a/test/diffAgainstStore.ts b/test/diffAgainstStore.ts index 23ba5866492..c3c4bb0753d 100644 --- a/test/diffAgainstStore.ts +++ b/test/diffAgainstStore.ts @@ -1,8 +1,13 @@ import { assert } from 'chai'; -import { diffQueryAgainstStore } from '../src/data/diffAgainstStore'; +import { + diffQueryAgainstStore, + diffSelectionSetAgainstStore, +} from '../src/data/diffAgainstStore'; import { writeQueryToStore } from '../src/data/writeToStore'; import { printQueryForMissingData } from '../src/queryPrinting'; +import { getQueryDefinition } from '../src/queries/getFromAST'; + import { getIdField, @@ -279,4 +284,61 @@ describe('diffing queries against the store', () => { `); assert.deepEqual(store['1'], result.people_one); }); + + it('returns available fields if throwOnMissingField is false', () => { + const firstQuery = gql` + { + people_one(id: "1") { + __typename + id + name + } + } + `; + + const firstResult = { + people_one: { + __typename: 'Person', + id: 'lukeId', + name: 'Luke Skywalker', + }, + }; + + const store = writeQueryToStore({ + result: firstResult, + query: firstQuery, + }); + + const queryWithMissingField = gql` + { + people_one(id: "1") { + name + age + } + } + `; + + const { result } = diffSelectionSetAgainstStore({ + store, + rootId: 'ROOT_QUERY', + selectionSet: getQueryDefinition(queryWithMissingField).selectionSet, + variables: null, + throwOnMissingField: false, + }); + + assert.deepEqual(result, { + people_one: { + name: 'Luke Skywalker', + }, + }); + assert.throws(function() { + diffSelectionSetAgainstStore({ + store, + rootId: 'ROOT_QUERY', + selectionSet: getQueryDefinition(queryWithMissingField).selectionSet, + variables: null, + throwOnMissingField: true, + }); + }); + }); }); From f88069c1cf552dd74b8c37b54d1fa8b89539bb73 Mon Sep 17 00:00:00 2001 From: hammadj Date: Wed, 6 Jul 2016 20:57:06 -0600 Subject: [PATCH 2/4] export addQueryMerging (#363) --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index d495f12c5ef..1c6840a9e0a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { NetworkInterface, createNetworkInterface, + addQueryMerging, } from './networkInterface'; import { @@ -65,6 +66,7 @@ import assign = require('lodash.assign'); // custom network interfaces can turn query ASTs into query strings as needed. export { createNetworkInterface, + addQueryMerging, createApolloStore, createApolloReducer, readQueryFromStore, From 4edc88f4e7b076d208a385f41465ba3e6f5d3803 Mon Sep 17 00:00:00 2001 From: hammadj Date: Wed, 6 Jul 2016 21:06:28 -0600 Subject: [PATCH 3/4] add to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d0de1f7ca..784b4f82fda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT +- Added missing export for the `addQueryMerging` method defined in the docs [here](http://docs.apollostack.com/apollo-client/network.html#addQueryMerging). [PR #364](https://github.com/apollostack/apollo-client/pull/364) and [Issue #363](https://github.com/apollostack/apollo-client/issues/363). - Made sure `diffSelectionSetAgainstStore` will return any available data from the local cache if `throwOnMissingField` is `false`, even if some fields in the query are missing. This also means that the `returnPartialData` option of `watchQuery` will return partial data if some fields are missing in the cache, rather than an empty object. [Issue #359](https://github.com/apollostack/apollo-client/issues/359) and [PR #360](https://github.com/apollostack/apollo-client/pull/360). ### v0.3.27 From f023b759d9b701c78cc7322184538f593f965c9f Mon Sep 17 00:00:00 2001 From: Sashko Stubailo Date: Wed, 6 Jul 2016 20:30:33 -0700 Subject: [PATCH 4/4] Bump version to 0.3.28 --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 784b4f82fda..d43455be9b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT +### v0.3.28 + - Added missing export for the `addQueryMerging` method defined in the docs [here](http://docs.apollostack.com/apollo-client/network.html#addQueryMerging). [PR #364](https://github.com/apollostack/apollo-client/pull/364) and [Issue #363](https://github.com/apollostack/apollo-client/issues/363). - Made sure `diffSelectionSetAgainstStore` will return any available data from the local cache if `throwOnMissingField` is `false`, even if some fields in the query are missing. This also means that the `returnPartialData` option of `watchQuery` will return partial data if some fields are missing in the cache, rather than an empty object. [Issue #359](https://github.com/apollostack/apollo-client/issues/359) and [PR #360](https://github.com/apollostack/apollo-client/pull/360). diff --git a/package.json b/package.json index dc08ac51c59..9b82b998470 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-client", - "version": "0.3.27", + "version": "0.3.28", "description": "A simple yet functional GraphQL client.", "main": "./lib/src/index.js", "typings": "./lib/src/index.d.ts",