Define relationship resource type in serialization options
See original GitHub issueFor example I have the following blog post with an author, and I want it serialized as a resource of type posts
, with an included resource of type users
:
{
id: '12'
text: 'Check this out!',
author: {
id: '42',
fullname: 'Foo Bar'
}
}
The only options I’ve found so far is to add a customType: 'users'
attribute to my author data and to use typeForAttribute: (attribute, data) => data.customType || attribute
option.
But when I already know that all my authors are users
, is there a solution to statically tell that to the serializer?
Otherwise would it be conceivable to define the relationship type with something like this:
var UserSerializer = new JSONAPISerializer('posts', {
attributes: ['author', 'text'],
author: {
ref: 'id',
type: 'users' // <-- new option
}
});
What do you think ?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:6
Top Results From Across the Web
Resources - JSONAPI
A resource defines which attributes are exposed, as well as relationships to ... the serialized relationship will include the type and id of...
Read more >Serializer relations - Django REST framework
Relational fields are used to represent model relationships. They can be applied to ForeignKey , ManyToManyField and OneToOneField relationships, ...
Read more >DS.RESTSerializer - 1.13 - Ember API Documentation
Defined in: packages/ember-data/lib/serializers/rest-serializer.js:23 ... Returns the resource's relationships formatted as a JSON-API "relationships ...
Read more >JaSerializer.Serializer - HexDocs
When you define a relationship, a function is defined of the same name in the serializer module. This function is overrideable but by...
Read more >Everything You Need to know about Serialization in Rails
This article focuses on the various Serializers that prepare and construct API transferable data in Ruby on Rails.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dhautotlf nothing really proper, I used
typeForAttribute
option to override the type if the relation - found by its name…which won’t work when relations of different types have the same name - had a specific type.ex (completing my previous example):
Thanks It’s very helpful