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.

Including self-referential fields in InputObjectType subclass

See original GitHub issue

I am trying to create an InputObjectType that has dynamic fields that is meant for complex filtering of fields but that also contains a couple of self-referencing fields to allow boolean operations (AND, OR). Here is a concrete example of what I am trying to do, taken from here:

input FriendlyUserFilter {
  sys: SysFilter
  name: String
  name_not: String
  name_exists: Boolean
  name_contains: String
  # ... more name filters
  age: Number
  age_gt: Number
  age_lt: Number
  # ... more age filters
  AND: [FriendlyUserFilter]
  OR: [FriendlyUserFilter]
}

I am using the technique described here to do so. I’m curious how to implement the AND and OR functionality that includes a reference to the class itself.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
art1415926535commented, Sep 3, 2019

Hmm… Maybe like this?

class BaseFilter(graphene.InputObjectType):
    @classmethod
    def __init_subclass_with_meta__(cls, *args, **kwargs):
        super().__init_subclass_with_meta__(*args, **kwargs)
        field = graphene.InputField(graphene.List(graphene.NonNull(cls)))
        cls._meta.fields.update({'or': field, 'and': field})

    class Meta:
        abstract = True


class UserFilter(BaseFilter):
    name = graphene.String()

Same result as https://github.com/graphql-python/graphene/issues/1063#issuecomment-527537653 (+NonNull)

1reaction
art1415926535commented, Sep 3, 2019

Hi, do you need something like that?


class UserFilters(graphene.InputObjectType):
    name = graphene.String()
    or_ = graphene.Field(graphene.List(lambda: UserFilters), name='or')
    and_ = graphene.Field(graphene.List(lambda: UserFilters), name='and')


class Query(ObjectType):
    all_users = ConnectionField(
        UserConnection, filters=UserFilters()
    )

Result (graphiql): image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for graphene.types.inputobjecttype
_meta.fields: setattr(self, key, self.get(key, ... (class): A class reference for a value object that allows for attribute initialization and access.
Read more >
Generate Graphene Mutation Inputs from SQLAlchemy Class ...
I solved creating this class: from graphene.types.utils import yank_fields_from_attrs from graphene.utils.subclass_with_meta import ...
Read more >
Graphene Documentation - Read the Docs
A GraphQL Schema describes each Field in the data model provided by the server using scalar types like String, Int and Enum and...
Read more >
Class: GraphQL::Schema::Directive
Subclasses of this can influence how Execution::Interpreter runs queries. Directive.include?: if it returns false , the field or fragment will be skipped ...
Read more >
Object Type Definition - graphql-php
Conceptually Object Type is a collection of Fields. Each field, in ... (or one of its subclasses) which accepts a configuration array in...
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