Graphene: DjagnoFilterConnectionField filtering functionality without relay overhead

Created on 30 Jun 2016  路  3Comments  路  Source: graphql-python/graphene

Hello,
First of all thanks for a great library, it's been very helpful for out project.

I have a django model, let's call it Transaction, that I want to be able to filter on all it's fields without having to configure the fields in advance.
Currently I use DjangoFilterConnectionField, so in my case the code looks like:

class TransactionNode(DjangoNode):

    class Meta:
        model = models.Transaction

class Query(graphene.ObjectType):
    transactions = DjangoFilterConnectionField(TransactionNode)


But when using DjangoFilterConnectionField, it introduce the relay nested structure:

{
query_name(name:"a") {
edges {
node{
....
}}}

Is there an option to query without this extra nesting? Like this:
But only

{query_name(name:"a"){
...
}

Thanks!

Most helpful comment

Currently I implemented this in the following way:

class TransactionFilter(FilterSet):
    class Meta:
        model = models.Transaction


class TransactionNode(DjangoNode):

    class Meta:
        model = models.Transaction

class Query(graphene.ObjectType):
    transactions = TransactionNode.List(**get_filtering_args_from_filterset(TransactionFilter, "a"))

    def resolve_transactionTest(self, args, info):
        # If you are using graphql-django-view, the request_context is the Django request
        return models.BusinessTransaction.objects.filter(**args)

It works, but it's much more cumbersome than using DjangoFilterConnectionField. Is there an easier way to implement this?

All 3 comments

That is following the Relay spec for connections. In principle you can write a Graph query which is not relay compatible, but you'd need to invent some other way to handle pagination.

Currently I implemented this in the following way:

class TransactionFilter(FilterSet):
    class Meta:
        model = models.Transaction


class TransactionNode(DjangoNode):

    class Meta:
        model = models.Transaction

class Query(graphene.ObjectType):
    transactions = TransactionNode.List(**get_filtering_args_from_filterset(TransactionFilter, "a"))

    def resolve_transactionTest(self, args, info):
        # If you are using graphql-django-view, the request_context is the Django request
        return models.BusinessTransaction.objects.filter(**args)

It works, but it's much more cumbersome than using DjangoFilterConnectionField. Is there an easier way to implement this?

Hi @tom-zeit The Django integration with Django is now in a separate repository (https://github.com/graphql-python/graphene-django). If this issue is still a problem for you could you create an issue on that repository so that it can be addressed?

Was this page helpful?
0 / 5 - 0 ratings