From 53d8f17344944182456a7c863ae186a7e37fb131 Mon Sep 17 00:00:00 2001 From: theobat Date: Wed, 22 Feb 2017 01:54:40 +0100 Subject: [PATCH] Fix Cannot read property 'length' of undefined (#162) * fix(denormalizeStore): refresh empty list data properly * test(denormalizeStore) --- .../__tests__/denormalizeStore-tests.js | 23 +++++++++++++++++++ src/normalize/denormalizeStore.js | 8 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/normalize/__tests__/denormalizeStore-tests.js b/src/normalize/__tests__/denormalizeStore-tests.js index a386a39..772c67f 100644 --- a/src/normalize/__tests__/denormalizeStore-tests.js +++ b/src/normalize/__tests__/denormalizeStore-tests.js @@ -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) +}); diff --git a/src/normalize/denormalizeStore.js b/src/normalize/denormalizeStore.js index cc85cc3..07c63a6 100644 --- a/src/normalize/denormalizeStore.js +++ b/src/normalize/denormalizeStore.js @@ -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); }