-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
54 lines (39 loc) · 1.12 KB
/
schema.py
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
import typing
from fastapi import FastAPI, Response
import strawberry
from strawberry.fastapi import GraphQLRouter
from autometrics import autometrics
from prometheus_client import generate_latest
@strawberry.type
class Book:
title: str
author: str
@strawberry.type
class Query:
books: typing.List[Book]
def get_books():
return [
Book(
title="The Great Gatsby",
author="F. Scott Fitzgerald",
),
]
@strawberry.type
class Query:
books: typing.List[Book] = strawberry.field(resolver=get_books)
# NOTE - `@autometrics` broke unless it was the first decorator on top of the resolver
#
@strawberry.field
@autometrics
def last_book(self) -> Book:
return get_books()[-1]
schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter(schema)
app = FastAPI()
# Set up the graphql server
app.include_router(graphql_app, prefix="/graphql")
# Set up a metrics endpoint for Prometheus to scrape
# `generate_latest` returns metrics data in the Prometheus text format
@app.get("/metrics")
def metrics():
return Response(generate_latest())