FastAPI + Strawberry GraphQL Filter Conditions

3k views Asked by At

I am currently build microservice using FastAPI + Strawberry GraphQL. I want to expose filters for the models with and/or condition. For example,

{
   Student(where:{and[{AgeGt: 15},{PercentageLt: 75}]}) {
     edges {
       node {
          Name
          Age
          Percentage
     }
   }
}

Is this possible? Any reference or example would greatly help.

1

There are 1 answers

1
patrick On BEST ANSWER

In Strawberry you can use input types to define arguments for your queries

Here's an example what should help you with definining filters using strawberry:

from typing import Optional, List, TypeVar, Generic
from datetime import date

import strawberry

T = TypeVar("T")

@strawberry.input
class AbelFilter(Generic[T]):
    eq: Optional[T] = None
    gt: Optional[T] = None
    lt: Optional[T] = None



@strawberry.input
class WhereFilter:
    foo: Optional[AbelFilter[str]] = None
    bar: Optional[AbelFilter[int]] = None
    baz: Optional[AbelFilter[str]] = None


@strawberry.type
class Query:
    @strawberry.field
    def student(self, where: WhereFilter) -> str:
        return str(where)

schema = strawberry.Schema(query=Query)

See this on the Strawberry Playground