当我运行我的 main.py 文件时,我得到了
ImportError: cannot import name 'ResolveInfo' from 'graphql' (/Users/apple/.local/share/virtualenvs/pythonProject-CGqZmG8Q/lib/python3.9/site-packages/graphql/__init__.py)
- 但是,我正在尝试安装以下软件包,但
ariadne
smeas 与graphen-core
or不兼容graphen
。
uvicorn = "*"
fastapi = "*"
graphene = "*"
starlette = "*"
ariadne = "*"
alembic = "*"
aniso8601 = "*"
appdirs = "*"
asgiref = "*"
black = "*"
click = "*"
colorama = "*"
graphene-sqlalchemy = "*"
graphql-relay = "*"
greenlet = "*"
h11 = "*"
mypy-extensions = "*"
pathspec = "*"
promise = "*"
psycopg2 = "*"
pydantic = "*"
python-dateutil = "*"
python-dotenv = "*"
python-editor = "*"
regex = "*"
singledispatch = "*"
six = "*"
toml = "*"
typing-extensions = "*"
dill = "==0.2.4"
Mako = "*"
MarkupSafe = "*"
Rx = "*"
SQLAlchemy = "*"
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: Could not find a version that matches black (from -r /var/folders/v3/4llt971130g6vvbq53m6k84w0000gn/T/pipenvo1i35v2wrequirements/pipenv-py46372c-constraints.txt (line 24))
Skipped pre-versions: 18.3a0, 18.3a0, 18.3a1, 18.3a1, 18.3a2, 18.3a2, 18.3a3, 18.3a3, 18.3a4, 18.3a4, 18.4a0, 18.4a0, 18.4a1, 18.4a1, 18.4a2, 18.4a2, 18.4a3, 18.4a3, 18.4a4, 18.4a4, 18.5b0, 18.5b0, 18.5b1, 18.5b1, 18.6b0, 18.6b0, 18.6b1, 18.6b1, 18.6b2, 18.6b2, 18.6b3, 18.6b3, 18.6b4, 18.6b4, 18.9b0, 18.9b0, 19.3b0, 19.3b0, 19.10b0, 19.10b0, 20.8b0, 20.8b1, 21.4b0, 21.4b0, 21.4b1, 21.4b1, 21.4b2, 21.4b2, 21.5b0, 21.5b0, 21.5b1, 21.5b1, 21.5b2, 21.5b2, 21.6b0, 21.6b0, 21.7b0, 21.7b0, 21.8b0, 21.8b0
我试图通过从pipenv
文件中删除版本来解决这个问题,我也尝试使用旧版本。
我陷入了一个闭环,所以当我安装旧版本时,
graphql-core
它与 ariadne 配合得很好,但graphene
需要更新版本。目标
# main.py
import graphene
from fastapi import FastAPI
from starlette.graphql import GraphQLApp
import models
from db_conf import db_session
from schemas import PostModel, PostSchema
import asyncio
import uvicorn
from ariadne import make_executable_schema, SubscriptionType, QueryType
from ariadne.asgi import GraphQL
db = db_session.session_factory()
app = FastAPI()
class Query(graphene.ObjectType):
all_posts = graphene.List(PostModel)
post_by_id = graphene.Field(PostModel, post_id=graphene.Int(required=True))
def resolve_all_posts(self, info):
query = PostModel.get_query(info)
return query.all()
def resolve_post_by_id(self, info, post_id):
return db.query(models.Post).filter(models.Post.id == post_id).first()
class CreateNewPost(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
content = graphene.String(required=True)
ok = graphene.Boolean()
@staticmethod
def mutate(root, info, title, content):
post = PostSchema(title=title, content=content)
db_post = models.Post(title=post.title, content=post.content)
db.add(db_post)
db.commit()
db.refresh(db_post)
ok = True
return CreateNewPost(ok=ok)
class PostMutations(graphene.ObjectType):
create_new_post = CreateNewPost.Field()
# ariadne copied from https://ariadnegraphql.org/docs/subscriptions
type_def = """
type Query {
_unused: Boolean
}
type Subscription {
counter: Int!
}
"""
subscription = SubscriptionType()
@subscription.source("counter")
async def counter_generator(obj, info):
for i in range(5):
await asyncio.sleep(1)
yield i
@subscription.field("counter")
def counter_resolver(count, info):
return count + 1
schema = make_executable_schema(type_def, subscription)
query = QueryType()
app.add_route("/", GraphQLApp(schema=graphene.Schema(query=Query, mutation=PostMutations)))
app.mount("/ws", GraphQL(schema, debug=True))
# if __name__ == "__main__":
# uvicorn.run(app, host="0.0.0.0", port=8000)