File: api.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (90 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from collections.abc import AsyncIterator

import strawberry
from strawberry.directive import DirectiveLocation, DirectiveValue


@strawberry.type
class Item:
    name: str
    index: int


@strawberry.type
class Person:
    name: str
    age: int
    description: str
    address: str

    prop_a: str
    prop_b: str
    prop_c: str
    prop_d: str
    prop_e: str
    prop_f: str
    prop_g: str
    prop_h: str
    prop_i: str
    prop_j: str


def create_people(n: int):
    for i in range(n):
        yield Person(
            name=f"Person {i}",
            age=i,
            description=f"Description {i}",
            address=f"Address {i}",
            prop_a=f"Prop A {i}",
            prop_b=f"Prop B {i}",
            prop_c=f"Prop C {i}",
            prop_d=f"Prop D {i}",
            prop_e=f"Prop E {i}",
            prop_f=f"Prop F {i}",
            prop_g=f"Prop G {i}",
            prop_h=f"Prop H {i}",
            prop_i=f"Prop I {i}",
            prop_j=f"Prop J {i}",
        )


people = list(create_people(n=1_000))


@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello World!"

    @strawberry.field
    def people(self, limit: int = 100) -> list[Person]:
        return people[:limit] if limit else people

    @strawberry.field
    def items(self, count: int) -> list[Item]:
        return [Item(name="Item", index=i) for i in range(count)]


@strawberry.type
class Subscription:
    @strawberry.subscription
    async def something(self) -> AsyncIterator[str]:
        yield "Hello World!"

    @strawberry.subscription
    async def long_running(self, count: int) -> AsyncIterator[int]:
        for i in range(count):
            yield i


@strawberry.directive(locations=[DirectiveLocation.FIELD])
def uppercase(value: DirectiveValue[str]) -> str:
    return value.upper()


schema = strawberry.Schema(query=Query, subscription=Subscription)
schema_with_directives = strawberry.Schema(
    query=Query, directives=[uppercase], subscription=Subscription
)