generated from prisma-korea/prisma-fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (37 loc) · 1.25 KB
/
main.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
import os
from src.prisma import prisma
from src.apis import apis
from src.resolvers import schema
from src.utils.auth import decodeJWT
from strawberry.asgi import GraphQL as BaseGraphQL
from fastapi.middleware.gzip import GZipMiddleware
from fastapi import FastAPI
from dotenv import load_dotenv
class GraphQL(BaseGraphQL):
async def get_context(self, request, response):
headers = request.headers
tokenIsPresent = headers.get('Authorization', False)
if tokenIsPresent:
decoded = decodeJWT(tokenIsPresent)
if 'userId' in decoded:
return {
"userId": decoded['userId'],
"request": request,
}
load_dotenv()
isProduction = os.getenv("IS_PRODUCTION", False)
graphql_app = GraphQL(schema, graphiql=False if isProduction else True)
app = FastAPI()
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.include_router(apis, prefix='/apis')
app.add_route("/graphql", graphql_app)
app.add_websocket_route("/graphql", graphql_app)
@app.on_event("startup")
async def startup():
await prisma.connect()
@app.on_event("shutdown")
async def shutdown():
await prisma.disconnect()
@app.get("/")
async def read_root():
return {"version": "1.0.0"}