본문 바로가기

개발하자

Fastapi + 딸기 그래프QL

반응형

Fastapi + 딸기 그래프QL

저는 현재 와 마이크로 서비스를 구축하고 있습니다.

추가 경로를 통해 기본 데이터를 노출하고 싶습니다. starlette의 직접 통합은 더 이상 사용되지 않기 때문에 추천 패키지 중 하나를 사용하려고 했습니다. 현재 graphqhl과 함께 사용하는 것은 불가능해 보인다.

my_grapqhql.py

from typing import List
import strawberry

@strawberry.type
class Book:
    title: str
    author: str

@strawberry.type
class Query:
    books: List[Book]

schema = strawberry.Schema(query=Query)

내가 시도한 것

fastapi 설명서에서 asgi 구성 요소는 다음과 같이 추가됩니다:

main.py

from fastapi import FastAPI
from strawberry.asgi import GraphQL
from .my_graphql.py import schema

app = FastAPI()
app.add_middleware(GraphQL, schema=schema)

안타깝게도 이것은 작동하지 않습니다:

TypeError: __init__() got an unexpected keyword argument 'app'

모듈을 장착하기 위한 마지막 라인을 전환하면 적어도 시작됩니다:

app.mount("/graphql", GraphQL(schema))

경로가 로드되지 않습니다.




이 내용은 여기에 문서화되어 있습니다:

문서에서

import strawberry

from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter


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


schema = strawberry.Schema(Query)


graphql_app = GraphQLRouter(schema)


app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")



문서에 있는 FastApi로 딸기를 추가하는 새로운 방법도 있습니다

app = FastAPI()
schema = strawberry.Schema(query=Query,mutation=Mutation,config=StrawberryConfig(auto_camel_case=True))
graphql_app = GraphQLRouter(schema)
app.include_router(graphql_app, prefix="/graphql")

반응형