question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] - Single filter lookup object argument in queries instead of one for each filter field.

See original GitHub issue

Im still really new to graphql in general so i dont know much about whether this is viable or not, but i know that if you define the filter fields on the node object as such,

filter_fields = {
    'email' : [....],
    'name' : [....]
}

They can be defined as single arguments on the query:


query ($email: String, ....) {
    ...
    users(email: $email, name_Istartswith:....) {
      ...
    }
  
}

However if you need to pass many filter arguments this can make the query string arguments really long. When we build up our graphql query in the front end, It would be convenient to just pass a single “filters” javascript object dynamically, so you dont have to pass each filter as an argument:


query ($filters: SomeNodeFilterType, ....) {
    ...
    users(filters: $filters) {
      ...
    }
  
}

Where filters object looks something like and is passed into the variables:


{
    "filters": {
        "email_Icontains": "someone@example.com"
         "name_Istartswith": "someone",
          //etc
    }
}

I tried to do something along the lines in a custom extended filter connection field class, and basically taking the filtering arguments and creating dynamic class of object type where it’s fields are the filter lookup as the name and the type being their corresponding graphene field type, then merging “filters” into the kwargs that get passed to to_arguments. I did not have any luck with this approach.

class CustomFilterConnectionField(DjangoFilterConnectionField):
   ...

   @property
    def args(self):
        node_name = str(self.node_type)
        class_name = "{node}Filter".format(node=node_name)
        fields = {}

        # extract the filtering args "{lookuptype: graphene field type}"
        for k, v in self.filtering_args.items():
            fields[k] = v.type 

        filter_kwargs = self.filtering_args
        # if we have filtering args then, create a dynamic object type.
        if len(fields):
            
            # fails with "fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping."
            dynamic_class = type(class_name, (ObjectType,), fields)

            # cant set meta directly due to object base type preventing it, so this method fails as well.
            # dynamic_class = type(class_name, (ObjectType,), {"_meta": {"fields": fields}})

            merge_kwargs = {"filters": Argument(dynamic_class)}

            filter_kwargs.update(merge_kwargs)

        return to_arguments(self._base_args or OrderedDict(), filter_kwargs)

    @args.setter
    def args(self, args):
        self._base_args = args

Perhaps im missing something or im taking the wrong approach to accomplish this but im curious as to whether this possible or not or if anyone has had any luck achieving something similar?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
surgiiecommented, Sep 23, 2019

@jkimbo

Yes here’s what i ended up with:


    @property
    def args(self):
        node_name = str(self.node_type)
        class_name = "{node}Filter".format(node=node_name)
        fields = {}

        for k, v in self.filtering_args.items():
            fields[k] = v.type()

        filter_kwargs = self.filtering_args
        # if we have filtering args then, create a dynamic object type.
        if len(fields):
            dynamic_class = type(class_name, (InputObjectType,), fields)

            filter_kwargs.update({"filters": Argument(dynamic_class)})

        return to_arguments(self._base_args or OrderedDict(), filter_kwargs)

    @args.setter
    def args(self, args):
        self._base_args = args

0reactions
jkimbocommented, Sep 22, 2019

Sorry for not getting back to you with this @surgiie . Did you manage to resolve your issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

difference between filter with multiple arguments and chain ...
If you are using the same table or an "OneToOneField" to filter objects, then there will be no difference between using a "Multiple...
Read more >
Search and Filtering - GraphQL - Dgraph
You can filter query results to find objects with one or more specified values using the in keyword. This keyword can find matches...
Read more >
Query and filter context | Elasticsearch Guide [8.5] | Elastic
The filter parameter indicates filter context. Its term and range clauses are used in filter context. They will filter out documents which do...
Read more >
Making queries | Django documentation
A QuerySet represents a collection of objects from your database. It can have zero, one or many filters. Filters narrow down the query...
Read more >
Filter, Search, and LookUp functions in Power Apps
The Filter function finds records in a table that satisfy a formula. Use Filter to find a set of records that match one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found