|
| 1 | +======= |
| 2 | +Filters |
| 3 | +======= |
| 4 | + |
| 5 | +Starting in graphene-sqlalchemy version 3, the SQLAlchemyConnectionField class implements filtering by default. The query utilizes a ``filter`` keyword to specify a filter class that inherits from ``graphene.InputObjectType``. |
| 6 | + |
| 7 | +Migrating from graphene-sqlalchemy-filter |
| 8 | +--------------------------------------------- |
| 9 | + |
| 10 | +If like many of us, you have been using |graphene-sqlalchemy-filter|_ to implement filters and would like to use the in-built mechanism here, there are a couple key differences to note. Mainly, in an effort to simplify the generated schema, filter keywords are nested under their respective fields instead of concatenated. For example, the filter partial ``{usernameIn: ["moderator", "cool guy"]}`` would be represented as ``{username: {in: ["moderator", "cool guy"]}}``. |
| 11 | + |
| 12 | +.. |graphene-sqlalchemy-filter| replace:: ``graphene-sqlalchemy-filter`` |
| 13 | +.. _graphene-sqlalchemy-filter: https://github.com/art1415926535/graphene-sqlalchemy-filter |
| 14 | + |
| 15 | +Further, some of the constructs found in libraries like `DGraph's DQL <https://dgraph.io/docs/query-language/>`_ have been implemented, so if you have created custom implementations for these features, you may want to take a look at the examples below. |
| 16 | + |
| 17 | + |
| 18 | +Example model |
| 19 | +------------- |
| 20 | + |
| 21 | +Take as example a Pet model similar to that in the sorting example. We will use variations on this arrangement for the following examples. |
| 22 | + |
| 23 | +.. code:: |
| 24 | +
|
| 25 | + class Pet(Base): |
| 26 | + __tablename__ = 'pets' |
| 27 | + id = Column(Integer(), primary_key=True) |
| 28 | + name = Column(String(30)) |
| 29 | + age = Column(Integer()) |
| 30 | +
|
| 31 | +
|
| 32 | + class PetNode(SQLAlchemyObjectType): |
| 33 | + class Meta: |
| 34 | + model = Pet |
| 35 | +
|
| 36 | +
|
| 37 | + class Query(graphene.ObjectType): |
| 38 | + allPets = SQLAlchemyConnectionField(PetNode.connection) |
| 39 | +
|
| 40 | +
|
| 41 | +Simple filter example |
| 42 | +--------------------- |
| 43 | + |
| 44 | +Filters are defined at the object level through the ``BaseTypeFilter`` class. The ``BaseType`` encompasses both Graphene ``ObjectType``\ s and ``Interface``\ s. Each ``BaseTypeFilter`` instance may define fields via ``FieldFilter`` and relationships via ``RelationshipFilter``. Here's a basic example querying a single field on the Pet model: |
| 45 | + |
| 46 | +.. code:: |
| 47 | +
|
| 48 | + allPets(filter: {name: {eq: "Fido"}}){ |
| 49 | + edges { |
| 50 | + node { |
| 51 | + name |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | +This will return all pets with the name "Fido". |
| 57 | + |
| 58 | + |
| 59 | +Custom filter types |
| 60 | +------------------- |
| 61 | + |
| 62 | +If you'd like to implement custom behavior for filtering a field, you can do so by extending one of the base filter classes in ``graphene_sqlalchemy.filters``. For example, if you'd like to add a ``divisible_by`` keyword to filter the age attribute on the ``Pet`` model, you can do so as follows: |
| 63 | + |
| 64 | +.. code:: python |
| 65 | +
|
| 66 | + class MathFilter(FloatFilter): |
| 67 | + class Meta: |
| 68 | + graphene_type = graphene.Float |
| 69 | +
|
| 70 | + @classmethod |
| 71 | + def divisible_by_filter(cls, query, field, val: int) -> bool: |
| 72 | + return is_(field % val, 0) |
| 73 | +
|
| 74 | + class PetType(SQLAlchemyObjectType): |
| 75 | + ... |
| 76 | +
|
| 77 | + age = ORMField(filter_type=MathFilter) |
| 78 | +
|
| 79 | + class Query(graphene.ObjectType): |
| 80 | + pets = SQLAlchemyConnectionField(PetType.connection) |
| 81 | +
|
| 82 | +
|
| 83 | +Filtering over relationships with RelationshipFilter |
| 84 | +---------------------------------------------------- |
| 85 | + |
| 86 | +When a filter class field refers to another object in a relationship, you may nest filters on relationship object attributes. This happens directly for 1:1 and m:1 relationships and through the ``contains`` and ``containsExactly`` keywords for 1:n and m:n relationships. |
| 87 | + |
| 88 | + |
| 89 | +:1 relationships |
| 90 | +^^^^^^^^^^^^^^^^ |
| 91 | + |
| 92 | +When an object or interface defines a singular relationship, relationship object attributes may be filtered directly like so: |
| 93 | + |
| 94 | +Take the following SQLAlchemy model definition as an example: |
| 95 | + |
| 96 | +.. code:: python |
| 97 | +
|
| 98 | + class Pet |
| 99 | + ... |
| 100 | + person_id = Column(Integer(), ForeignKey("people.id")) |
| 101 | +
|
| 102 | + class Person |
| 103 | + ... |
| 104 | + pets = relationship("Pet", backref="person") |
| 105 | +
|
| 106 | +
|
| 107 | +Then, this query will return all pets whose person is named "Ada": |
| 108 | + |
| 109 | +.. code:: |
| 110 | +
|
| 111 | + allPets(filter: { |
| 112 | + person: {name: {eq: "Ada"}} |
| 113 | + }) { |
| 114 | + ... |
| 115 | + } |
| 116 | +
|
| 117 | +
|
| 118 | +:n relationships |
| 119 | +^^^^^^^^^^^^^^^^ |
| 120 | + |
| 121 | +However, for plural relationships, relationship object attributes must be filtered through either ``contains`` or ``containsExactly``: |
| 122 | + |
| 123 | +Now, using a many-to-many model definition: |
| 124 | + |
| 125 | +.. code:: python |
| 126 | +
|
| 127 | + people_pets_table = sqlalchemy.Table( |
| 128 | + "people_pets", |
| 129 | + Base.metadata, |
| 130 | + Column("person_id", ForeignKey("people.id")), |
| 131 | + Column("pet_id", ForeignKey("pets.id")), |
| 132 | + ) |
| 133 | +
|
| 134 | + class Pet |
| 135 | + ... |
| 136 | +
|
| 137 | + class Person |
| 138 | + ... |
| 139 | + pets = relationship("Pet", backref="people") |
| 140 | +
|
| 141 | +
|
| 142 | +this query will return all pets which have a person named "Ben" in their ``people`` list. |
| 143 | + |
| 144 | +.. code:: |
| 145 | +
|
| 146 | + allPets(filter: { |
| 147 | + people: { |
| 148 | + contains: [{name: {eq: "Ben"}}], |
| 149 | + } |
| 150 | + }) { |
| 151 | + ... |
| 152 | + } |
| 153 | +
|
| 154 | +
|
| 155 | +and this one will return all pets which hvae a person list that contains exactly the people "Ada" and "Ben" and no fewer or people with other names. |
| 156 | + |
| 157 | +.. code:: |
| 158 | +
|
| 159 | + allPets(filter: { |
| 160 | + articles: { |
| 161 | + containsExactly: [ |
| 162 | + {name: {eq: "Ada"}}, |
| 163 | + {name: {eq: "Ben"}}, |
| 164 | + ], |
| 165 | + } |
| 166 | + }) { |
| 167 | + ... |
| 168 | + } |
| 169 | +
|
| 170 | +And/Or Logic |
| 171 | +------------ |
| 172 | + |
| 173 | +Filters can also be chained together logically using `and` and `or` keywords nested under `filter`. Clauses are passed directly to `sqlalchemy.and_` and `slqlalchemy.or_`, respectively. To return all pets named "Fido" or "Spot", use: |
| 174 | + |
| 175 | + |
| 176 | +.. code:: |
| 177 | +
|
| 178 | + allPets(filter: { |
| 179 | + or: [ |
| 180 | + {name: {eq: "Fido"}}, |
| 181 | + {name: {eq: "Spot"}}, |
| 182 | + ] |
| 183 | + }) { |
| 184 | + ... |
| 185 | + } |
| 186 | +
|
| 187 | +And to return all pets that are named "Fido" or are 5 years old and named "Spot", use: |
| 188 | + |
| 189 | +.. code:: |
| 190 | +
|
| 191 | + allPets(filter: { |
| 192 | + or: [ |
| 193 | + {name: {eq: "Fido"}}, |
| 194 | + { and: [ |
| 195 | + {name: {eq: "Spot"}}, |
| 196 | + {age: {eq: 5}} |
| 197 | + } |
| 198 | + ] |
| 199 | + }) { |
| 200 | + ... |
| 201 | + } |
| 202 | +
|
| 203 | +
|
| 204 | +Hybrid Property support |
| 205 | +----------------------- |
| 206 | + |
| 207 | +Filtering over SQLAlchemy `hybrid properties <https://docs.sqlalchemy.org/en/20/orm/extensions/hybrid.html>`_ is fully supported. |
| 208 | + |
| 209 | + |
| 210 | +Reporting feedback and bugs |
| 211 | +--------------------------- |
| 212 | + |
| 213 | +Filtering is a new feature to graphene-sqlalchemy, so please `post an issue on Github <https://github.com/graphql-python/graphene-sqlalchemy/issues>`_ if you run into any problems or have ideas on how to improve the implementation. |
0 commit comments