Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to consume relationships on the fly ? #215

Open
alberto-f opened this issue Jun 22, 2020 · 2 comments
Open

Way to consume relationships on the fly ? #215

alberto-f opened this issue Jun 22, 2020 · 2 comments

Comments

@alberto-f
Copy link

Hello, thanks for the project. It is really nice structure and the middlewares are a really nice feature.

I was wondering if there is any way to continue consuming the responses

Example:

jsonApi.define('post', {
  title: '',
  content: '',
  comments: {
    jsonApi: 'hasMany',
    type: 'comments'
  }
})

const post = await jsonApi.find('post', '1'); // trigger get request
const comments = await post.comments.findAll();  // trigger get request
@franzliedke
Copy link

Check out the README section on the URL Builder.

This may work:

const comments = await post.all('comment').get();

@tijn
Copy link
Collaborator

tijn commented Nov 17, 2022

I believe the deserialization step in Devour throws away the information that's needed for what you like to do @alberto-f . I could be mistaken though..

I would like to have this functionality too; and I already thought about it. There's one problem that's keeping me from implementing it though: (using the code from your example) post.comments.findAll() will reserve the word findAll for that function. That means this name cannot be used anymore as an attribute or a relationship (collectively called fields).

I see two solutions (with several downsides):

  1. use an underscore prefix for functions. e.g.: await post.comments._findAll()
  2. Change the deserialization so that we keep attributes and relationships separate; but add convenient delegates to for non-clashing names; like such:
const post = await jsonApi.find('post', '1');

post.attributes.title // => the attribute "title"
post.title // a delegate to the title attribute

post.relationships.comments // => a relationship-object with methods like get and delete
post.relationships.comments.included[0] // => a resource-object; an [included related resource](https://jsonapi.org/format/#fetching-includes)

post.comments // a delegate to an included resource (only available if it's included)

I doubt that my second solution will be popular with the users of this library though since it seems like this was a deliberate design decision that was made long ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants