-
Notifications
You must be signed in to change notification settings - Fork 70
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
feat: relationships #44
Conversation
Whenever you see this, there is another thing I've been working on in a different branch. That I did mostly because I didn't know whether this was the ideal way to do things but I still needed it for my own application. Thing is I needed to be able to include related resources and I didn't see any way to do that other than using the |
👍 Plus one on this |
Just to confirm - without this PR, it is not possible to modify relationship fields on resouces? Is that right? It seems like it ought to be quite useful for this behaviour to be supported... |
Just so everyone's aware, the branch I used for this PR is outdated, if some maintainer decided to review this I'd be happy to merge the latest changes I did in my development branch which fixed some issues I had while using this (all of them related to relationships, I didn't have any issues with the library itself). |
Done, PR is updated and conflicts are solved. @euoia if you have the time to try this out it would be great! |
I tested and I can see that the relationshipMap is adding All the tests pass. With these changes, how do I use
But is this still correct? Wouldn't it make more sense to be like If I leave I wonder how you are using this in your application. One small thing, I was unable to run I wonder whether One last thing, unrelated to this PR, when my server returns an error I get a blank screen and a console error https://marmelab.com/react-admin/DataProviderWriting.html The errors that I am getting back from https://laraveljsonapi.io/ look like this:
|
My idea when I coded this was not to change anything as to the way the library works so if the docs say I don't think it would be a good idea to change what we get in one of these props that are already defined by the framework, especially given the abstraction layer we're working on right here. I did some reading and here's what I found about what's going on under the hood: If you look at the code for Internally that calls the It's possible I never actually tried to use As for generating that edition payload, I see where the issue is: I used the name of the relationship in the map, not its id to check whether the key is a relationship. This makes the error occur. I'll work on adding a test for this particular scenario and making it work. Also, since I no longer have access to the project I did using this I might make a new proof of concept. As for error handling maybe creating a separate issue for it would be the best course of action, that way we can focus on relationship support here. |
So I went back to work and remembered an issue I'd had. The way Explanation: The library generates a filter with a shape similar to In my opinion, for this PR to reach a "mergeable" state two things have to happen first. Firstly we should abstract query parsing so users can configure it. Secondly, and optionally, add typescript typings (no need to migrate the code, just add type declarations) to improve developer experience and remove warnings when using the lib from a ts project. |
Hello world!
I extended this library a bit to support relationships both in serialization and deserialization and wanted to share that implementation with y'all.
Deserialization
Map
that looks like{ [resourceType-resourceId]: resourceAttributes }
.relationships
object I add their attributes, which I get from the aforementionedMap
, and their id to an object with the same name as therelationship
and add it to the resulting value.This applies for arrays as well.
Serialization
relationshipsMap
which was added to the settings. For instance, were they to work withprojects
which define anowner
relationship with theusers
resource type, then they should declare the relationship as follows:attributes
of the resource.relationships
of the resource. To do that, the serializer maps them to objects that look like{ type, id }
and gets the type from therelationshipsMap
. How so? Just gettingrelationshipsMap[resourceType][relationshipName]
you're getting the correct resource type for the related object.I did add tests for getting resources with included relationships (projects with included owner and collaborators which I took from an API I'm developing at the moment) and did the same for creating resources with relationships. I did not add tests for updates and deletions because, at least my API (which uses
JsonApiDotNetCore
) returns a204 NO CONTENT
when I send aPATCH
or aDELETE
, then again, I guess that is only because of the way I configured the framework/the way that particular framework works.A use case I thought of but did not implement at first (since I personally do not need it ATM) is recursive relationships or just relationships within relationships. First case scenario could be Folders which contain folders which contain folders... Second case scenario could be just
projects
which have anowner
which is part of anorganization
.I really don't want to do anything I don't need without first discussing with you whether this approach could be improved or anything so, please, let me know what you think! I'd be happy to work on this a bit more to make it nice and complete so anyone could use it!
BTW, took the liberty of adding myself to the list of contributors at the end of the
README
😄 I also added the@babel/eslint-parser
so I could use optional chaining and the coalescing operator without adding linting errors heheLooking forward to your feedback!
Closes #10