Skip to content

Commit

Permalink
Include any available fields in response if throwOnMissingField is …
Browse files Browse the repository at this point in the history
…false.
  • Loading branch information
dahjelle committed Jul 6, 2016
1 parent 2366ba6 commit 5ae2fd1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/data/diffAgainstStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
64 changes: 63 additions & 1 deletion test/diffAgainstStore.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
});
});
});
});

0 comments on commit 5ae2fd1

Please sign in to comment.