-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Feature: Ability to set single object lookup key #159
Feature: Ability to set single object lookup key #159
Conversation
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 pretty solid to me! I've added a couple of minor comments 😊
lookup_key=UNSET, | ||
lookup_key_django_name=UNSET, | ||
lookup_key_value=UNSET, | ||
): |
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.
I think we can use None
here as default :)
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.
can we add type hints for the new params?
if lookup_key_django_name is UNSET: | ||
# Default to using the same field name as externally, but allow | ||
# the internal name to be overridden. | ||
lookup_key_django_name = lookup_key |
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.
Is this to allow using id
as the GraphQL argument, but map it to pk
in the Django filters?
queryset, | ||
lookup_key=UNSET, | ||
lookup_key_django_name=UNSET, | ||
lookup_key_value=UNSET, |
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.
Should we change this to be value
?
@@ -66,13 +66,27 @@ class Fruit: | |||
name: auto | |||
|
|||
|
|||
# For demonstrating options set on the type. | |||
@strawberry_django.type(models.Fruit, lookup_key="name", lookup_key_type=str) |
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.
do you think we could infer lookup_key_type
from the django model?
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.
In some cases it's certainly possible, but I found that if you're using a custom field type, there's not currently a means to add it to the mapping. Perhaps for the time being, the lookup_key_type
could be optional.
Would be interested in this - is there something one can do to get this merged? |
We are basically waiting for some updates from @benhowes that @patrick91 made. If for some reason @benhowes is not going to work on this anymore, you can probably take on this changes yourself (if you want to do that, of course 😊) |
I no longer work on the codebase which used this change, and have not touched it for a long time. I aspire to get the code review changes implemented, but realistically I doubt I will any time soon. |
Thanks for updating us on that :) @capital-G would you like to work on this to get it finished? |
@bellini666 yes :) Sounds like a good first thing to dive deeper into strawberry :) |
Personally I would suggest creating a new PR starting from the commits on this issue, just to avoid losing the changes done by @benhowes and removing its credits for it. Then feel free to adapt it in any way you want. When the new PR gets opened I'll close this one as superseded |
@bellini666 I started looking into this but I think I would prefer to do this from a new branch as there are merge conflicts when rebasing this to |
You can merge main instead of rebasing, so it is easier to resolve conflicts and still keep the original credits. But it is up mostly to @benhowes to say what he prefers |
Anything is fine with me, no attribution required! |
This has not have any activity for a while now so I'm closing this to clean up the backlog a little. In case op or anyone wants to get back to this, feel free to reopen, or ask me to reopen in case you don't have permission to :) |
Description
This PR adds the ability to alter the behaviour of a single object lookup. Currently the behaviour is that a single object fetch can only operate on the
pk
of that object (withpk
as the variable name), but in many cases developers may want to use other names/types.For maximum flexibility, this PR adds 3 options to the
strawberry.django.field
definition which can be used in many combinations, such as:or
or
It defaults to the existing behaviour of the lookup field being
pk
and the type beingstrawberry.ID
.The only "breaking" change is that the lookup key is now a required field, so this will slightly alter the generated schema. This can be considered more of a bugfix since the lookups would not work without an argument, other than in the special case where there was only a single record of that type (in which case
.get()
on the queryset would work). I would hope that's not behaviour that anyone is relying on though...Types of Changes
Issues Fixed or Closed by This PR
Checklist