Swydo / blaze-apollo

Blaze integration for the Apollo Client
MIT License
54 stars 6 forks source link

get() throws when query result is undefined #8

Closed diegonc closed 7 years ago

diegonc commented 7 years ago

I've just tripped into the exception below, triggered when the query result is still undefined.

Exception in template helper: SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at jsonClone (http://localhost:3000/packages/swydo_blaze-apollo.js?hash=c91866076c16a91ad56ef1526f5b5bc5a90bc9cc:78:15)
    at Result.get (http://localhost:3000/packages/swydo_blaze-apollo.js?hash=c91866076c16a91ad56ef1526f5b5bc5a90bc9cc:112:14)

It's happening because I'm doing a this.gplQuery and a get() in the same onCreated callback. Strange though, I tried to overcome the problem using isReady() but that method returned true even when the result was still undefined.

jamiter commented 7 years ago

Hi @diegonc, thanks for reporting this. It would be great if you could write a failing test for it so I can fix it. Let me know if you have time for this or need any help getting the tests up and running. Cheers.

diegonc commented 7 years ago

I'm not sure how to implement the test, as the behaviour depends on the Apollo Client implementation. When apollo dispatches the APOLLO_QUERY_INIT action, the store listeners are triggered and eventually blaze-apollo's next function is called. A portion of the stacktrace is below.

next @ result.js:50
...
QueryManager.broadcastQueries @ modules.js?hash=a07a784…:116521
...
QueryManager.fetchQuery @ modules.js?hash=a07a784…:115998
QueryManager.startQuery @ modules.js?hash=a07a784…:116303
ObservableQuery.setUpQuery @ modules.js?hash=a07a784…:115538
ObservableQuery.onSubscribe @ modules.js?hash=a07a784…:115490
subscriberFunction @ modules.js?hash=a07a784…:115264
Observable.subscribe @ modules.js?hash=a07a784…:115153
subscribe @ result.js:48
Result @ result.js:24
gqlQuery @ setup.js:31
...

next gets called with the following object:

{
  "data": undefined,
  "loading":true,
  "networkStatus":1,
  "stale":true
}

Ok, I think I can fake a call to next in the unit test. But it feels a bit dirty :stuck_out_tongue_closed_eyes:

jamiter commented 7 years ago

What version of the Apollo client is this?

diegonc commented 7 years ago

It's apollo-client version 1.8.0. I just created PR #10, adding the corresponding test.

jamiter commented 7 years ago

Thanks for the help on this one. It should be fixed in 0.5.0.

jamiter commented 7 years ago

Note that it will now return the original immutable data directly from the Apollo Client store. This could impact your code if you were modifying the returned data. Those modifications will either error or simply not show up.