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

Support for including the data attribute in relationships #8

Closed
mrcasals opened this issue Jun 10, 2015 · 7 comments
Closed

Support for including the data attribute in relationships #8

mrcasals opened this issue Jun 10, 2015 · 7 comments

Comments

@mrcasals
Copy link
Contributor

Hi!

I opened this issue in the official repo: json-api/json-api#758, open it so that you get the context of this issue. One of the suggested solutions is using the meta keyword on a relationship to flag whether it's a to_one or a to_many realtionship, but it's weird to me. The other suggestion is to include the data keyword for all relationships in the included objects.

Is there any way to specify that I want to add the data attribute on those relationships?

Thanks!

@fotinakis
Copy link
Owner

I just quickly skimmed that, so tell me if I've got this wrong, but I'm pretty sure you're just talking about what the spec calls "full linkage"—ie. that included objects can also include data and relationships that link to each other for both to-one and to-many, so that if you "include:" things that are related backwards and forwards, you get all the relationship information. This is something that was pretty complex to support, but I definitely got that working. :)

Check out https://github.com/fotinakis/jsonapi-serializers#compound-documents-and-includes, see the included section relationships and data, and some notes below that.

Let me know if that doesn't cover what you're talking about.

@mrcasals
Copy link
Contributor Author

Not exactly. Let's take the example JSON form the official website: https://gist.github.com/mrcasals/7353c916f256279ea391 (saved here just in case it changes).

If I analyze the post form data, I see it has one author and many comments. If I go to the author object (inside the included section), I don't know whether it has a relationship on posts, and how is this relationship built (one-to-one, or one-to-many?). There are no examples in the official website with elements in the included section with relationships to other objects.

The issue I linked to asks whether an included object can include relationships or not, and what to do with those objects (do I also have to include them in the included section?). The answer is that yes, an included object can have relationships, and no, I don't need to include them. The only limitation is that all objects in the included section must be referenced before, but not everything I renference has to be included.

Taking that, I can have an included object with a relationship and a data field for it, so that I know how is the relationship built, but I don't have to include the related objects. This is not quite the full-linkage system, as full linkage would add those objects and I don't want that.

This is what I want to achieve:

  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    },
    "relationships":{  
       "posts":{  
          "data":[{  
             "type":"posts",
             "id":"1"
          }, {
             "type":"posts",
             "id":"2"
          }]
       }
    }
  }]

This could be done, for example, including always the data field for all relationships, but I don't know if you'd like this option.

@iancanderson
Copy link

I agree with @mrcasals here. In the app I'm working on, I want to serialize a has_one relationship (including the IDs in the relationship data) without necessarily having to sideload the actual record, since that parent record (that the has_one points to) is already loaded in my Ember app, so I only need the IDs to wire up the relationships.

Like @mrcasals said, it would be nice if the data field could be included for all relationships, or maybe there could be an option like has_one :post, relationship_data: true to conditionally include the IDs

@fotinakis
Copy link
Owner

It's been a while, but I now understand what you're asking for here. :) Also discussing in #23, where we might be able to do this automatically if you provide a hint of how to lookup the ID.

@fotinakis
Copy link
Owner

Note to self: actually, this is similar but unrelated to the other issues, this specifically is about having deeper data for included objects' relationships.

@bonjourmauko
Copy link

I did a quick hack to have linkage-level meta working: https://github.com/maukoquiroga/jsonapi-serializers/commit/ec197cc4d7ab841aa70727774b420220a0f52a06. I leave it here if it helps others to better grasp one possible scenario.

@fotinakis
Copy link
Owner

fotinakis commented Apr 27, 2016

This is finally complete via #70 and launched in v0.11.0 of the gem. See the docs: https://github.com/fotinakis/jsonapi-serializers#control-links-and-data-in-relationships

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

4 participants