Using Flask with graphene and py2neo: Cannot use variables in Query Class/resolver

119 Views Asked by At

for a project I am trying to implement a graphql endpoint via flask in order to query a neo4j-DB using py2neo and graphene.

Within a docker container I am using python 3.9 and the follwing packages are installed:

  • Flask==2.2.3
  • graphene>=3.0
  • py2neo==2021.2.3
  • graphql-server[flask]
  • graphql-core==3.1.0

My py2neo.ogm Graph-Object looks like that:

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()

My graphene schema and query class are definied like that

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)

After building the services using docker-compose I am receiving the following error on start-up:

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'

The problem is definitely caused by the title argument in this line: movie = graphene.Field(lambda: MovieSchema, title=graphene.String())

Whenever I try to run the container without the title argument in the query class the container starts as expected and I can see the correct schema via the graphql interface.

Many thanks in advance for your help!

1

There are 1 best solutions below

0
On

Issue is solved by changing the packages from:

  • Flask==2.2.3
  • graphene>=3.0
  • py2neo==2021.2.3
  • graphql-server[flask]
  • graphql-core==3.1.0

to:

  • Flask==2.2.3
  • graphene>=3.1
  • py2neo==2021.2.3
  • graphql_server