英文:
Cannot import name 'GraphQL' from 'strawberry.fastapi'
问题
I am trying to run a strawberry example from this tutorial but it does not work.
I have the code:
from fastapi import FastAPI
import strawberry
from strawberry.fastapi import GraphQL
@strawberry.type
class Book:
title: str
author: str
price: int
@strawberry.type
class Query:
@strawberry.field
def book(self) -> Book:
return Book(title="Computer Fundamentals", author="Sinha", price=300)
schema = strawberry.Schema(query=Query)
graphql_app = GraphQL(schema)
app = FastAPI()
app.add_route("/book", graphql_app)
app.add_websocket_route("/book", graphql_app)
but when I try to run it with uvicorn Mystrawberry:app --reload
it says
ImportError: cannot import name 'GraphQL' from 'strawberry.fastapi'
Now, in the Strawberry documentation examples, they use GraphQLRouter rather than GraphQL, and I don't know if that is an old version or a typo.
英文:
I am trying to run an strawberry example from this tutorial but it does not work.
I have the code:
from fastapi import FastAPI
import strawberry
from strawberry.fastapi import GraphQL
@strawberry.type
class Book:
title: str
author: str
price: int
@strawberry.type
class Query:
@strawberry.field
def book(self) -> Book:
return Book(title="Computer Fundamentals", author="Sinha", price=300)
schema = strawberry.Schema(query=Query)
graphql_app = GraphQL(schema)
app = FastAPI()
app.add_route("/book", graphql_app)
app.add_websocket_route("/book", graphql_app)
but when I try to run it with uvicorn Mystrawberry:app --reload
it says
ImportError: cannot import name 'GraphQL' from 'strawberry.fastapi'
Now, in the Strawberry documentation examples they use GraphQLRouter rather than GraphQL, and I don't know if that is an old version or a typo.
答案1
得分: 1
似乎您的教程不正确或已过时。请查看官方文档,看起来正确的做法是使用from strawberry.fastapi import GraphQLRouter
,然后将该路由器包含在您的FastAPI应用程序中。
英文:
It would seem your tutorial is incorrect or out of date. Take a look at the official documentation, it seems like the way to do it is to do from strawberry.fastapi import GraphQLRouter
, and include that router in your FastAPI application.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论