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

would not be nice to have a generic valueForRelationship? #256

Open
ftaiolivista opened this issue Aug 13, 2021 · 1 comment
Open

would not be nice to have a generic valueForRelationship? #256

ftaiolivista opened this issue Aug 13, 2021 · 1 comment

Comments

@ftaiolivista
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Currently I have to declare valueForRelationship for every relationship but many times the function must be applied to all entities so could be nice to have the possibility to declare a general valueForRelationship tha can be replaced per specific relationship.

Here is the diff that solved my problem:

diff --git a/node_modules/jsonapi-serializer/lib/deserializer-utils.js b/node_modules/jsonapi-serializer/lib/deserializer-utils.js
index 1265fd4..b63e3e6 100644
--- a/node_modules/jsonapi-serializer/lib/deserializer-utils.js
+++ b/node_modules/jsonapi-serializer/lib/deserializer-utils.js
@@ -14,9 +14,9 @@ module.exports = function (jsonapi, data, opts) {
   }
 
   function getValueForRelationship(relationshipData, included) {
-    if (opts && relationshipData && opts[relationshipData.type]) {
-      var valueForRelationshipFct = opts[relationshipData.type]
-        .valueForRelationship;
+    if (opts && relationshipData && (opts[relationshipData.type] || opts.valueForRelationship)) {
+      var valueForRelationshipFct =
+        opts[relationshipData.type]?.valueForRelationship || opts.valueForRelationship;
 
       return valueForRelationshipFct(relationshipData, included);
     } else {

This issue body was partially generated by patch-package.

@Gonzalo2683
Copy link

Hello, interesting the patch you have made, could I add an example to understand how to use this change? Thanks

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

2 participants