-
Notifications
You must be signed in to change notification settings - Fork 58
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
Apply query_params to plural field. Fixes #219 #225
base: main
Are you sure you want to change the base?
Conversation
I just noticed that there's already a singular helper, and that I hadn't made the same change to the paginated helper. Please don't merge yet, as I'll make this missing change and add a 'plural only' helper too. |
OK, I've reverted There's quite a lot of duplicated code in these helpers, which isn't necessarily bad, but they do have some subtle differences in behaviour that I noticed:
Hopefully this hasn't changed the Python API too drastically, but it's made Grapple a lot more flexible for my use-case. |
Hey @flyte, thank you for this. |
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.
Hey @flyte, thank you for this. Lots of good changes 🚀
While I appreciate them, I think the direction needs to change a bit as per my other comment. tl;dr make register_query_field
and register_paginated_query_field
work, rather than an all new decorator
@@ -162,6 +163,11 @@ class BlogPageRelatedLink(Orderable): | |||
|
|||
|
|||
@register_snippet | |||
@register_singular_query_field("person", {"name": graphene.String()}) | |||
@register_plural_query_field("people", {"job": graphene.String()}) | |||
@register_plural_query_field( |
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.
My main gripe with this approach is that it introduces yet another decorator, when it should not need to.
@register_query_field
supports plurality via the plural_field_name
kwarg, so we should make that work.
register_singular_query_field
is a convenience decorator for very narrow uses cases, such as Wagtail Page models with max_count = 1
Finally, #219 (comment) is an important use case, which this particular example covers, but as mentioned above it would be better to handle within the existing decorators, so register_paginated_query_field
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.
@zerolab on the other hand it may be nice to separate @register_singular_query_field and @register_plural_query_field to make it simpler to document. Decorators in my experience tend to do discrete things. Having one decorator that sets up two different fields means we need to explain how both behave under one heading when each is going to have distinct arguments and return types.
Where as if we had both individual decorators, then the aggregate decorator can be explained by linking to both... just a thought... Maybe not something to include in this fix, but something to think about long term.
an example of our existing docs
class grapple.helpers.register_query_field(field_name, plural_field_name=None, query_params=None, required=False, plural_required=False, plural_item_required=False, middleware=None)
all of those plural_* fields are only valuable if plural_field_name is set and have nothing to do with a singular fields
I almost feel like we should have @register_model_query_singular
, @register_model_query_plural
, @register_model_query_paginated
. So users can choose to some something like...
advert_query_params = {
'blog__id': graphene.Int()
}
@register_model_query_singular("advert", required=True, query_params=advert_query_params)
@register_model_query_plural("allAdverts", required=True, query_params=advert_query_params)
@register_model_query_paginated("adverts", required=False, query_params=advert_query_params, item_required=True, )
class Advert:
# ...
pass
@flyte do you still have any inclination to work on this issue? |
@dopry hi, I'm afraid I don't now. The project I was working on is complete and I'm no longer using this. |
@flyte that is cool. Life happens. Is it okay if another contributor takes over this work? |
Yes, of course! 😊 |
Applies the query_params to the plural field as well.
A more ideal solution might be to separate out the singular and plural query helpers, as some query params might not be appropriate for both queries.
The main 'bug fix' for this is to use the kwargs in
resolve_queryset()
to enable custom filtering. In order to make this work, I needed to removein_site
from kwargs by setting them as a keyword argument onresolve_pages()
, where that flag is actually used. Even if you don't want to alter the API behaviour ofregister_query_field()
, this functionality makes it possible to create the separate helpers, as I mentioned above.