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

deep assign if nested fragment #629

Closed
Closed
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ delianides <drew@delianides.com>
greenkeeperio-bot <support@greenkeeper.io>
hammadj <jutt@ualberta.ca>
matt debergalis <matt@meteor.com>
Edvin Eriksson <edvinerikson@gmail.com>
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Expect active development and potentially significant breaking changes in the `0

- Options set in middleware can override the fetch query in the network layer. [Issue #627](https://github.com/apollostack/apollo-client/issues/627) and [PR #628](https://github.com/apollostack/apollo-client/pull/628).
- Make `returnPartialData` work better with fragments. [PR #580](https://github.com/apollostack/apollo-client/pull/580)
- Fixed issue with nested fragments overriding each other. [PR #629](https://github.com/apollostack/apollo-client/pull/629)

### v0.4.14

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"author": "Sashko Stubailo <sashko@stubailo.com>",
"license": "MIT",
"dependencies": {
"deep-assign": "^2.0.0",
"es6-promise": "^3.1.2",
"graphql-tag": "^0.1.13",
"lodash.assign": "^4.0.8",
Expand Down
6 changes: 3 additions & 3 deletions src/data/diffAgainstStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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');
const deepAssign = require('deep-assign');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import here.


import {
storeKeyNameFromField,
Expand Down Expand Up @@ -221,7 +221,7 @@ export function diffSelectionSetAgainstStore({
}

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

if (!fragmentErrors[typename]) {
Expand Down
78 changes: 78 additions & 0 deletions test/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,84 @@ describe('reading from the store', () => {
});
});

it.only('runs a nested fragment with multiple fragments', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with it.only you're running just this single test!

const result: any = {
id: 'abcd',
stringField: 'This is a string!',
numberField: 5,
nullField: null,
nestedObj: {
id: 'abcde',
stringField: 'This is a string too!',
numberField: 6,
nullField: null,
} as StoreObject,
deepNestedObj: {
stringField: 'This is a deep string',
numberField: 7,
} as StoreObject,
};

const store = {
abcd: _.assign({}, _.assign({}, _.omit(result, 'nestedObj', 'deepNestedObj')), {
nestedObj: {
type: 'id',
id: 'abcde',
generated: false,
},
}) as StoreObject,
abcde: _.assign({}, result.nestedObj, {
deepNestedObj: {
type: 'id',
id: 'abcdef',
generated: false,
},
}) as StoreObject,
abcdef: result.deepNestedObj as StoreObject,
} as NormalizedCache;

const queryResult = readFragmentFromStore({
store,
fragment: gql`
fragment FragmentName on Item {
stringField,
numberField,
...on Item {
nestedObj {
stringField
deepNestedObj {
stringField
}
}
}
...on Item {
nestedObj {
numberField
deepNestedObj {
numberField
}
}
}
}
`,
rootId: 'abcd',
});

// The result of the query shouldn't contain __data_id fields
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this comment here?

assert.deepEqual(queryResult, {
stringField: 'This is a string!',
numberField: 5,
nestedObj: {
stringField: 'This is a string too!',
numberField: 6,
deepNestedObj: {
stringField: 'This is a deep string',
numberField: 7,
},
},
});
});

it('runs a nested fragment with an array without IDs', () => {
const result: any = {
id: 'abcd',
Expand Down