zooniverse / json-api-client

Apache License 2.0
10 stars 5 forks source link

Fetching with included relationships #42

Closed d-lamers closed 6 years ago

d-lamers commented 6 years ago

It seems it does not allow to fetch included relationships. As described here: http://jsonapi.org/format/#fetching-includes. Would you consider supporting it?

srallen commented 6 years ago

Include requests do work, but there was a design decision early on with caching that resulted in some quirky behavior with the include requests, see zooniverse/json-api-client#34. There's also another related bug about inconsistent type used by the client for include requests: zooniverse/json-api-client#35

You can make an include request by adding it to the options object. If it's a single include request, put it in an array as noted in zooniverse/json-api-client#35: apiClient.type('resource').get('1234', { include: ['otherResource'] })

Or multiple includes: apiClient.type('resource').get('1234', { include: 'otherResource,anotherResource' })

Assuming there aren't any issue with the API you're requesting from, it should return a 200 response in your network tab in dev tools. The client, however, will only return the original requested resource in promise. The includes are put into the cache. You have to form another 'fake' request to get them out of the cache:

apiClient.type('otherResource').get() and this will return in the promise that included resource out of the client cache and won't form another network request.

It's a quirky design decision and not really helpful in real world application, imo. Just so you know, we're not really maintaining this client outside of critical bug fixes at the moment. We're likely moving toward using something like superagent directly rather than an abstraction like this client in the future.

d-lamers commented 6 years ago

Thanks for clearing this up. Makes sense.