将 Flask 与石墨烯和 py2neo 结合使用:不能在查询类/解析器中使用变量

问题描述 投票:0回答:1

对于一个项目,我正在尝试通过 flask 实现 graphql 端点,以便使用 py2neo 和 graphene 查询 neo4j-DB。

在 docker 容器中,我使用的是 python 3.9 并安装了以下软件包:

  • 烧瓶==2.2.3
  • 石墨烯>=3.0
  • py2neo==2021.2.3
  • graphql-server[烧瓶]
  • graphql-core==3.1.0

我的 py2neo.ogm Graph-Object 看起来像这样:

class Movie(BaseModel):
    __primarykey__='title'

    id = graphene.Int()
    title = graphene.String()
    year = graphene.Int()
    persons = RelatedFrom("Person", "ACTED_IN")

    def as_dict(self):
        return {
            'id': self.id,
            'title': self.title,
            'year': self.year,
        }
    
    def fetch(self):
        return self.match(graph, self.title).first()

我的石墨烯模式和查询类是这样定义的

class MovieSchema(graphene.ObjectType):
    id = graphene.String()
    title = graphene.String()
    year = graphene.Int()

class Query(graphene.ObjectType):
    # Hello is only used for test purposes
    hello = graphene.String(description='A typical hello world')
    movie = graphene.Field(lambda: MovieSchema, title=graphene.String())

    def resolve_hello(self, info):
        return 'World'
    
    def resolve_movie(self, info, title):
        return Movie.fetch(title=title)

schema = graphene.Schema(query=Query)

使用 docker-compose 构建服务后,我在启动时收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 735, in fields
    fields = resolve_thunk(self._fields) 
  File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 293, in resolve_thunk
    return thunk() if callable(thunk) else thunk 
  File "/usr/local/lib/python3.9/site-packages/graphene/types/schema.py", line 326, in create_fields_for_type
    args[processed_arg_name] = GraphQLArgument( TypeError: __init__() got an unexpected keyword argument 'deprecation_reason'| During handling of the above exception, another exception occurred: 
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import 
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load 
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked 
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked 
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module 
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed 
  File "/var/www/server.py", line 11, in <module>
    from src.graphql.schemas import schema 
  File "/var/www/src/graphql/schemas.py", line 47, in <module>
    schema = graphene.Schema(query=Query) 
  File "/usr/local/lib/python3.9/site-packages/graphene/types/schema.py", line 440, in __init__
    self.graphql_schema = GraphQLSchema( 
  File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 205, in __init__
     collect_referenced_types(query) 
  File "/usr/local/lib/python3.9/site-packages/graphql/type/schema.py", line 382, in collect_referenced_types
    for field in named_type.fields.values(): 
  File "/usr/local/lib/python3.9/functools.py", line 993, in __get__
    val = self.func(instance) 
  File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 737, in fields
    raise TypeError(f"{self.name} fields cannot be resolved. {error}") TypeError: Query fields cannot be resolved. __init__() got an unexpected keyword argument 'deprecation_reason'

问题肯定是由这一行的标题参数引起的:

movie = graphene.Field(lambda: MovieSchema, title=graphene.String())

每当我尝试在查询类中运行没有 title 参数的容器时,容器就会按预期启动,我可以通过 graphql 接口看到正确的模式。

非常感谢您的帮助!

neo4j py2neo graphene-python flask-graphql
1个回答
0
投票

问题通过更改包来解决:

  • 烧瓶==2.2.3
  • 石墨烯>=3.0
  • py2neo==2021.2.3
  • graphql-server[烧瓶]
  • graphql-core==3.1.0

至:

  • 烧瓶==2.2.3
  • 石墨烯>=3.1
  • py2neo==2021.2.3
  • graphql_server
© www.soinside.com 2019 - 2024. All rights reserved.