question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define relationship resource type in serialization options

See original GitHub issue

For 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:open
  • Created 6 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

1reaction
adrien-kcommented, Jun 7, 2017

@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):

// Somehow construct this from your relation structure:
const typesForRelations = {author: 'users'};

// Then, in your serializer options:
typeForAttribute: (attribute, resource) => typesForRelations[attribute] || attribute
0reactions
yogaraja2commented, Sep 30, 2021

@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):

// Somehow construct this from your relation structure:
const typesForRelations = {author: 'users'};

// Then, in your serializer options:
typeForAttribute: (attribute, resource) => typesForRelations[attribute] || attribute

Thanks It’s very helpful

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found