Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve partial data to work with fragments #614

Merged
merged 2 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Expect active development and potentially significant breaking changes in the `0

### vNEXT

- ...
- Make `returnPartialData` work better with fragments. [PR #580](https://github.com/apollostack/apollo-client/pull/580)

### v0.4.14

Expand Down
9 changes: 6 additions & 3 deletions src/data/diffAgainstStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isArray = require('lodash.isarray');
import isNull = require('lodash.isnull');
import isObject = require('lodash.isobject');
import has = require('lodash.has');
import assign = require('lodash.assign');

Expand Down Expand Up @@ -217,10 +218,11 @@ export function diffSelectionSetAgainstStore({

if (fieldIsMissing) {
pushMissingField(selection);
} else {
assign(result, fieldResult);
}

if (isObject(fieldResult)) {
assign(result, fieldResult);
}
if (!fragmentErrors[typename]) {
fragmentErrors[typename] = null;
}
Expand Down Expand Up @@ -256,7 +258,8 @@ export function diffSelectionSetAgainstStore({

if (fieldIsMissing) {
pushMissingField(selection);
} else {
}
if (isObject(fieldResult)) {
assign(result, fieldResult);
}

Expand Down
64 changes: 59 additions & 5 deletions test/diffAgainstStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ describe('diffing queries against the store', () => {
query: firstQuery,
});

const queryWithMissingField = gql`
// Variants on a simple query with a missing field.

const simpleQuery = gql`
{
people_one(id: "1") {
name
Expand All @@ -526,24 +528,76 @@ describe('diffing queries against the store', () => {
}
`;

const { result } = diffSelectionSetAgainstStore({
const inlineFragmentQuery = gql`
{
people_one(id: "1") {
... on Person {
name
age
}
}
}
`;

const namedFragmentQuery = gql`
query {
people_one(id: "1") {
...personInfo
}
}
fragment personInfo on Person {
name
age
}`;

const simpleDiff = diffSelectionSetAgainstStore({
store,
rootId: 'ROOT_QUERY',
selectionSet: getQueryDefinition(queryWithMissingField).selectionSet,
selectionSet: getQueryDefinition(simpleQuery).selectionSet,
variables: null,
throwOnMissingField: false,
});

assert.deepEqual(result, {
assert.deepEqual(simpleDiff.result, {
people_one: {
name: 'Luke Skywalker',
},
});

const inlineDiff = diffSelectionSetAgainstStore({
store,
rootId: 'ROOT_QUERY',
selectionSet: getQueryDefinition(inlineFragmentQuery).selectionSet,
variables: null,
throwOnMissingField: false,
});

assert.deepEqual(inlineDiff.result, {
people_one: {
name: 'Luke Skywalker',
},
});

const namedDiff = diffSelectionSetAgainstStore({
store,
rootId: 'ROOT_QUERY',
selectionSet: getQueryDefinition(namedFragmentQuery).selectionSet,
variables: null,
throwOnMissingField: false,
fragmentMap: createFragmentMap(getFragmentDefinitions(namedFragmentQuery)),
});

assert.deepEqual(namedDiff.result, {
people_one: {
name: 'Luke Skywalker',
},
});

assert.throws(function() {
diffSelectionSetAgainstStore({
store,
rootId: 'ROOT_QUERY',
selectionSet: getQueryDefinition(queryWithMissingField).selectionSet,
selectionSet: getQueryDefinition(simpleQuery).selectionSet,
variables: null,
throwOnMissingField: true,
});
Expand Down