Skip to content

Conversation

alexkirsz
Copy link
Contributor

refine_queryset lets DjangoObjectType define exactly how the queryset should be refined before being returned to the user. For instance, some objects could be filtered out according to a predicate, or some fields could be prefetched depending on what the initial query requested.

get_connection_parameters lets DjangoObjectType define the name and value of parameters that can be passed to any DjangoConnectionField that uses them.

Both these additions come as building blocks to allow custom refinements and filters without having to go through django-filter. Moreover, such filters can also be further optimized than previously allowed, as the GraphQL ResolveInfo instance is available in refine_queryset.

Example usage:

class RecipeOrderBy(graphene.Enum):
    name__ASC = 'name'
    name__DESC = '-name'

class RecipeNode(DjangoObjectType):
    class Meta:
        model = Recipe
        interfaces = (graphene.relay.Node,)

    @classmethod
    def get_connection_parameters(cls):
        return {
            'order_by': RecipeOrderBy()
        }

    @classmethod
    def refine_queryset(cls, qs, info, **kwargs):
        if 'order_by' in kwargs:
            qs = qs.order_by(RecipeOrderBy.get(kwargs['order_by']).value)
        # Since we have access to the GraphQL query's ResolveInfo instance through the info
        # argument, you can enable some prefetch optimizations here as well.
        return qs

@alexkirsz alexkirsz changed the title Introduce refine_queryset and get_connection_parameters Make it easier to define custom filters without django-filter Feb 4, 2019
refine_queryset lets DjangoObjectType define exactly how the queryset
should be refined before being returned to the user. For instance, some
objects could be filtered out according to a predicate, or some fields
could be prefetched depending on what the initial query requested.

get_connection_parameters lets DjangoObjectType define the name and
value of parameters that can be passed to any DjangoConnectionField that
uses them.

Both these additions come as building blocks to allow custom refinements
and filters without having to go through django-filter. Moreover, such
filters can also be further optimized than previously allowed, as the
GraphQL info object is available in refine_queryset.
@coveralls
Copy link

coveralls commented Feb 5, 2019

Coverage Status

Coverage increased (+0.07%) to 94.59% when pulling 6fda2f6 on acu:custom-filter into f76f38e on graphql-python:master.

@stale
Copy link

stale bot commented Jun 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 11, 2019
@stale stale bot closed this Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants