我目前正在使用fastapi构建一个微服务。
我想通过graphql在另一个路径上公开我的底层数据。来自starlette的直接集成已经被弃用,所以我尝试使用推荐的包strawberry。目前,它似乎不可能与grapqhl结合使用。
示例
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))但是路由没有加载。
发布于 2021-07-15 17:33:08
我很快就会看到这个文档:https://github.com/strawberry-graphql/strawberry/pull/1043
要使用草莓和FastAPI,您可以执行以下操作:
from fastapi import FastAPI
from strawberry.asgi import GraphQL
from api.schema import Schema
graphql_app = GraphQL(schema)
app = FastAPI()
app.add_route("/graphql", graphql_app)https://stackoverflow.com/questions/68381893
复制相似问题