Skip to content

Commit

Permalink
Merge pull request #170 from lennyburdette/watchQuery-resultKey
Browse files Browse the repository at this point in the history
fix: gracefully handle null/undefined with watchQuery(..., resultKey)
  • Loading branch information
bgentry committed Sep 19, 2018
2 parents a721d67 + c72cbde commit 3d5bc8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addon/services/apollo.js
Expand Up @@ -32,8 +32,8 @@ function newDataFunc(observable, resultKey, resolve, mergedProps = {}) {
// See https://github.com/bgentry/ember-apollo-client/issues/45
return;
}
let keyedData = isNone(resultKey) ? data : get(data, resultKey);
let dataToSend = copyWithExtras(keyedData, [], []);
let keyedData = isNone(resultKey) ? data : data && get(data, resultKey);
let dataToSend = copyWithExtras(keyedData || {}, [], []);
if (isNone(obj)) {
if (isArray(dataToSend)) {
obj = A(dataToSend);
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/services/apollo-test.js
Expand Up @@ -77,4 +77,40 @@ module('Unit | Service | apollo', function(hooks) {
assert.equal(result.__typename, 'person');
done();
});

test('.watchQuery with key', async function(assert) {
let service = this.owner.lookup('service:apollo');

service.set('client', {
watchQuery() {
assert.ok(true, 'Called watchQuery function on apollo client');

return {
subscribe({ next }) {
next({ data: { human: { name: 'Link' }, __typename: 'person' } });
},
};
},
});

const result = await service.watchQuery({ query: testQuery }, 'human');
assert.equal(result.get('name'), 'Link');
});

test('.watchQuery with key gracefully handles null', async function(assert) {
let service = this.owner.lookup('service:apollo');

service.set('client', {
watchQuery() {
return {
subscribe({ next }) {
next({ data: null });
},
};
},
});

const result = await service.watchQuery({ query: testQuery }, 'human');
assert.equal(result.get('name'), undefined);
});
});

0 comments on commit 3d5bc8c

Please sign in to comment.