-
Notifications
You must be signed in to change notification settings - Fork 76
feat: add backwards compatible polymorphic resource support #359
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR. I’m on a trip for the next two weeks so unless someone else jumps in there will be a bit of a delay in looking this over. Just wanted to let you know I see the PR! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a pretty reasonable approach to me. I'll do a bit more thinking on it but in the meantime there are a few things I found to comment on.
lib/jsonapi/view.ex
Outdated
@@ -140,10 +140,12 @@ defmodule JSONAPI.View do | |||
@type options :: keyword() | |||
@type resource_id :: String.t() | |||
@type resource_type :: String.t() | |||
@type polymorphic_resource :: boolean() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't get used best I can tell. I'd also argue that aliasing the boolean type to polymorphic_resource
doesn't add clarity if used in a type spec (the statement "this is a value of type polymorphic_resource
" almost sounds like it is describing a polymorphic resource instead of a boolean indicating whether a resource is polymorphic).
lib/jsonapi/view.ex
Outdated
def fields, do: raise("Need to implement fields/0") | ||
|
||
@impl View | ||
def polymorphic_fields(_data), do: nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of this callback does not include nil
(same for the other new callback).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If in practice this function just gets ignored whenever @polymorphic_resource
is false
, may as well return []
(which fits its type spec) instead of nil
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative design might be explicitly allow nil
and avoid the need for the @polymorphic_type
boolean by instead defining the resource_type/1
function to be polymorphic_type(data)
if it isn't nil
and otherwise type()
. Having the @polymorphic_type
boolean is probably the better option, though.
ee95988
to
fa2d880
Compare
Hey there, |
Hey there :) |
Hey! Sorry, I kept not thinking of this PR when I had time to look at it because it had been removed from my review queue after my previous comments. I've added it back into my queue so I should get this reviewed in the next few (week) days. |
Hey there,
I tried my best to add support for polymorphic resources.
The idea is to allow the user to infer resource specifics from the given data.
Currently this only allows
type
andfields
to be polymorphic. I will probably implement relationships as well.Additionally I only tested it to work as a relation with an api endpoint of its own - quite frankly there are some things that are not possible:
The
QueryParser
fetchesview.fields()
at a moment where no data is present, thus inference is not possible.This is my first time really working with a behaviour, my approach on implementing a backwards compatible solution felt a bit clunky, so any feedback is very welcome. :)