Skip to content

Commit

Permalink
Fix Cannot read property 'length' of undefined (#162)
Browse files Browse the repository at this point in the history
* fix(denormalizeStore): refresh empty list data properly

* test(denormalizeStore)
  • Loading branch information
theobat authored and mattkrick committed Feb 22, 2017
1 parent 58c48e3 commit 53d8f17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/normalize/__tests__/denormalizeStore-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,26 @@ test('flag sendToServer = true for array of objects', t => {
// getPostById -> keywordsMentioned -> word
t.true(context.operation.selectionSet.selections[0].selectionSet.selections[1].selectionSet.selections[0].sendToServer);
});

test('Same query with another argument', t => {
const rawQuery = `
query {
getCommentsByPostId {
_id
postId
content
}
}`;
const queryAST = parseAndInitializeQuery(rawQuery, clientSchema, idFieldName);
const context = buildExecutionContext(queryAST, {
variables: {"postId": "p126"},
coerceTypes,
getState: () => ({entities:{}, result: {getCommentsByPostId: {'"postId": "p123"':[]}}}),
idFieldName,
paginationWords,
schema: clientSchema
});
const actual = denormalizeStore(context);
const expected = {getCommentsByPostId:[{_id:null,postId:null,content:null}]}
t.deepEqual(actual, expected)
});
8 changes: 6 additions & 2 deletions src/normalize/denormalizeStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ const visitObject = (subState = {}, reqAST, parentTypeSchema, context, reduction
if (isPrimitive(nnFieldType.kind) || subState[fieldName] === null) {
reduction[aliasOrFieldName] = visitScalar(fieldState, context.coerceTypes[typeSchema.name])
} else {
if (nnFieldType.kind === LIST) {
reduction[aliasOrFieldName] = visitIterable(fieldState, field, typeSchema, context, aliasOrFieldName);
if ((nnFieldType.kind === LIST)) {
if (fieldState) {
reduction[aliasOrFieldName] = visitIterable(fieldState, field, typeSchema, context, aliasOrFieldName);
} else {
reduction[aliasOrFieldName] = [visitObject(fieldState, field, typeSchema, context)];
}
} else {
reduction[aliasOrFieldName] = visitObject(fieldState, field, typeSchema, context);
}
Expand Down

0 comments on commit 53d8f17

Please sign in to comment.