Skip to content

Commit

Permalink
Merge pull request #785 from apollostack/fix-776
Browse files Browse the repository at this point in the history
Test and fix for #776
  • Loading branch information
Sashko Stubailo authored Oct 15, 2016
2 parents 580c855 + 797948a commit 46d4622
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Expect active development and potentially significant breaking changes in the `0
- added "ApolloClient" to the named exports to make it compatible with Angular2 AOT compile [Issue #758](https://github.com/apollostack/apollo-client/issues/758) [PR #778](https://github.com/apollostack/apollo-client/pull/778)
- Fix: moved dev @types to devDependencies otherwise they potentially brake projects that are importing apollo-client [Issue #713](https://github.com/apollostack/apollo-client/issues/713) [PR #778](https://github.com/apollostack/apollo-client/pull/778)
- Fix rejecting promises on `refetch` and similar methods. Also improve error handling and stop using `ApolloError` internally. [Failing test in PR #524](https://github.com/apollostack/apollo-client/pull/524) [PR #781](https://github.com/apollostack/apollo-client/pull/781)
- Fix multidimentional array handling. [Issue #776](https://github.com/apollostack/apollo-client/issues/776) [PR #785](https://github.com/apollostack/apollo-client/pull/785)

### v0.4.21
- Added some temporary functions (`_setVariablesNoResult` and `_setOptionsNoResult`) to work around a `react-apollo` problem fundamentally caused by the issue highlighted in [PR #694](https://github.com/apollostack/apollo-client/pull/694). The code as been refactored on `master`, so we expect it to be fixed in 0.5.x, and is not worth resolving now.
Expand Down
77 changes: 46 additions & 31 deletions src/data/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,38 +259,10 @@ function writeFieldToStore({
json: value,
};
} else if (Array.isArray(value)) {
// this is an array with sub-objects
const thisIdList: Array<string> = [];
const generatedId = `${dataId}.${storeFieldName}`;

value.forEach((item: any, index: any) => {
if (isNull(item)) {
thisIdList.push(null);
} else {
let itemDataId = `${dataId}.${storeFieldName}.${index}`;

if (dataIdFromObject) {
const semanticId = dataIdFromObject(item);

if (semanticId) {
itemDataId = semanticId;
}
}

thisIdList.push(itemDataId);

writeSelectionSetToStore({
dataId: itemDataId,
result: item,
store,
selectionSet: field.selectionSet,
variables,
dataIdFromObject,
fragmentMap,
});
}
});

storeValue = thisIdList;
storeValue = processArrayValue(value, generatedId, dataIdFromObject, store,
field.selectionSet, variables, fragmentMap);
} else {
// It's an object
let valueDataId = `${dataId}.${storeFieldName}`;
Expand Down Expand Up @@ -370,3 +342,46 @@ function writeFieldToStore({
store[dataId] = newStoreObj;
}
}

function processArrayValue(
value: any[],
generatedId: string,
dataIdFromObject: IdGetter,
store: NormalizedCache,
selectionSet: SelectionSet,
variables: any,
fragmentMap: FragmentMap,
): any[] {
return value.map((item: any, index: any) => {
if (isNull(item)) {
return null;
}

let itemDataId = `${generatedId}.${index}`;

if (Array.isArray(item)) {
return processArrayValue(item, itemDataId, dataIdFromObject, store, selectionSet,
variables, fragmentMap);
}

if (dataIdFromObject) {
const semanticId = dataIdFromObject(item);

if (semanticId) {
itemDataId = semanticId;
}
}

writeSelectionSetToStore({
dataId: itemDataId,
result: item,
store,
selectionSet,
variables,
dataIdFromObject,
fragmentMap,
});

return itemDataId;
});
}
21 changes: 21 additions & 0 deletions test/roundtrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ describe('roundtrip', () => {
});
});

it('multidimensional array (#776)', () => {
storeRoundtrip(gql`
{
rows {
value
}
}
`, {
rows: [
[
{ value: 1 },
{ value: 2 },
],
[
{ value: 3 },
{ value: 4 },
],
],
});
});

it('with an alias', () => {
storeRoundtrip(gql`
{
Expand Down

0 comments on commit 46d4622

Please sign in to comment.