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

Apply query_params to plural field. Fixes #219 #225

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

flyte
Copy link

@flyte flyte commented Apr 9, 2022

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 remove in_site from kwargs by setting them as a keyword argument on resolve_pages(), where that flag is actually used. Even if you don't want to alter the API behaviour of register_query_field(), this functionality makes it possible to create the separate helpers, as I mentioned above.

@flyte flyte changed the title Feature/plural query fields Apply query_params to plural field #219 Apr 9, 2022
@flyte flyte changed the title Apply query_params to plural field #219 Apply query_params to plural field. Fixes #219 Apr 9, 2022
@flyte
Copy link
Author

flyte commented Apr 9, 2022

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.

@flyte
Copy link
Author

flyte commented Apr 9, 2022

OK, I've reverted register_query_field() to its original behaviour and added register_plural_query_field().

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:

  • Default query parameters are different between each helper
  • Differences between returning a result or None on a singular query if multiple results are returned
  • Different handling of urlPath queries
  • No default 'order' on non-Page model based paginated queries on register_paginated_query_field()'s resolve_plural() which raises a warning if using a QuerySet that's not ordered by default, like on snippets: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list

Hopefully this hasn't changed the Python API too drastically, but it's made Grapple a lot more flexible for my use-case.

@zerolab
Copy link
Member

zerolab commented Apr 10, 2022

Hey @flyte, thank you for this.
Will aim to review in the coming week or so

Copy link
Member

@zerolab zerolab left a 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(
Copy link
Member

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

Copy link
Collaborator

@dopry dopry May 12, 2023

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

@zerolab zerolab linked an issue Sep 8, 2022 that may be closed by this pull request
@dopry
Copy link
Collaborator

dopry commented May 12, 2023

@flyte do you still have any inclination to work on this issue?

@flyte
Copy link
Author

flyte commented May 12, 2023

@dopry hi, I'm afraid I don't now. The project I was working on is complete and I'm no longer using this.

@dopry
Copy link
Collaborator

dopry commented May 12, 2023

@flyte that is cool. Life happens. Is it okay if another contributor takes over this work?

@flyte
Copy link
Author

flyte commented May 12, 2023

Yes, of course! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filter pages with page model field
3 participants