无法从 ‘strawberry.fastapi’ 导入 ‘GraphQL’。

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月18日 09:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定