I got problems, when FastAPI route use call graphQL app, found error as below.
RuntimeError: Unexpected ASGI message 'http.response.start' sent, after response already completed.
I know I can app.mount(graphql_app) or app.add_route, but our company want this style.
@router.post('/')
def graph_ql():
pass
here is my code.
from starlette_graphene3 import GraphQLApp
import graphene
def create_graphql_app(
query: graphene.types.ObjectType = None,
mutation=None,
subscription=None
) -> GraphQLApp:
"""
Args:
query: graph_ql query
mutation: graph_ql update and insert
subscription: graph_ql streaming data
Returns:
"""
schema = graphene.Schema(
query=query,
mutation=mutation,
subscription=subscription
)
graphql_app = GraphQLApp(
schema=schema,
on_get=make_graphiql_handler() # Graphiql IDE
)
return graphql_app
graphql_app = create_graphql_app(query=Query)
router = APIRouter()
@router.post('/')
async def graph_query(request: Request):
return await graphql_app(
scope=request.scope,
receive=request.receive,
send=request.send
)