You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently upgrading a graphene 2 project to graphene 3. The project uses graphene-sqlalchemy and graphql-server[flask]. I'm noticing strange behavior when using batching while flask is in development mode, specifically when auto-reload is enabled. When auto-reload is enabled and batching is enabled, then all requests fail. Instead an asyncio error is thrown, TypeError: UserConvo fields cannot be resolved. There is no current event loop in thread 'Thread-1'. The issue only occurs when batching is enabled and flask auto reload is enabled. Disabling batching or disabling the flask auto-reload resolves the issue. The issue also only occurs with a many to many relationship that include the back_populates param in the relationships.
Below is a [somewhat] minimal example to reproduce the issue. The app.py, schema.py, and requirements.txt file are below.
Let me know if you need more info or clarification. Thanks!
Repro:
0) set up a virtual env and install packages from requirements.txt below. Then copy app.py and schema.py to your working directory.
run: FLASK_ENV=development && flask run
execute query { __schema { types { name } } } and receive error TypeError: UserConvo fields cannot be resolved. There is no current event loop in thread 'Thread-1'.
run `FLASK_ENV=development && flask run --no-reload
execute query { __schema { types { name } } } and receive no error.
Error Trace:
Traceback (most recent call last):
File "/Users/erichemphill/workspace/craft/graphene-test/app.py", line 4, in <module>
from schema import schema
File "/Users/erichemphill/workspace/craft/graphene-test/schema.py", line 74, in <module>
schema = Schema(query=Query)
File "/Users/erichemphill/workspace/craft/graphene-test/env/lib/python3.8/site-packages/graphene/types/schema.py", line 430, in __init__
self.graphql_schema = GraphQLSchema(
File "/Users/erichemphill/workspace/craft/graphene-test/env/lib/python3.8/site-packages/graphql/type/schema.py", line 208, in __init__
collect_referenced_types(query)
File "/Users/erichemphill/workspace/craft/graphene-test/env/lib/python3.8/site-packages/graphql/type/schema.py", line 423, in collect_referenced_types
collect_referenced_types(field.type)
File "/Users/erichemphill/workspace/craft/graphene-test/env/lib/python3.8/site-packages/graphql/type/schema.py", line 422, in collect_referenced_types
for field in named_type.fields.values():
File "/opt/homebrew/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/functools.py", line 967, in __get__
val = self.func(instance)
File "/Users/erichemphill/workspace/craft/graphene-test/env/lib/python3.8/site-packages/graphql/type/definition.py", line 737, in fields
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
TypeError: UserConvo fields cannot be resolved. There is no current event loop in thread 'Thread-1'.
schema.py:
from graphene import Field, ObjectType, Schema
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy import Column, Integer, ForeignKey, BigInteger
Base = declarative_base()
class UserModel(Base): # pylint: disable=too-few-public-methods
"""
Models the User Object
"""
__tablename__ = "user"
user_id = Column(BigInteger, primary_key=True, autoincrement=True)
convos = relationship("UserConvoAssociation", back_populates="user")
class UserConvoAssociation(Base):
"""
Association Object for User and Convo relationships
"""
__tablename__ = "user_convo"
convo_id = Column(ForeignKey("convo.convo_id"), primary_key=True)
user_id = Column(ForeignKey("user.user_id"), primary_key=True)
num_unread_messages = Column(Integer, default=0)
convo = relationship("ConvoModel", back_populates="users")
user = relationship("UserModel", back_populates="convos")
class ConvoModel(Base): # pylint: disable=too-few-public-methods
"""
Models the Convo object
"""
__tablename__ = "convo"
convo_id = Column(BigInteger, primary_key=True, autoincrement=True)
users = relationship("UserConvoAssociation", back_populates="convo")
class UserConvo(SQLAlchemyObjectType):
"""
User x Convo relationship
"""
class Meta:
model = UserConvoAssociation
interfaces = (relay.Node,)
batching = True
class User(SQLAlchemyObjectType):
"""
Defines the schema for a User
"""
class Meta:
"""
Defines the metadata for a User
"""
model = UserModel
interfaces = (relay.Node,)
batching = True
class Query(ObjectType):
convo = Field(UserConvo)
user = Field(User)
schema = Schema(query=Query)
app.py
from flask import Flask
from graphql_server.flask import GraphQLView
from schema import schema
app = Flask(__name__)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
'graphql',
schema=schema.graphql_schema,
graphiql=True,
))
if __name__ == '__main__':
app.run()
requirements.txt (this is not a minimal requirements.txt. it's from the project that i'm working on.)
This looks like an issue with graphql-server[flask] or graphene rather than this library. Maybe the event loop introduced with graphene3 is destroyed during live-reload?
Have you found any fix so far?
Unfortunately I did not find a solution outside of disabling the live-reload. The project I was working on actually pivoted away from Graphene so I'm no longer using this library.
Thank you for the reply! I'll keep this open for further reference and try to find a solution next time I'm working on something flask-related. Thanks for reporting.
Hi,
I'm currently upgrading a graphene 2 project to graphene 3. The project uses
graphene-sqlalchemy
andgraphql-server[flask].
I'm noticing strange behavior when using batching while flask is in development mode, specifically when auto-reload is enabled. When auto-reload is enabled and batching is enabled, then all requests fail. Instead anasyncio
error is thrown,TypeError: UserConvo fields cannot be resolved. There is no current event loop in thread 'Thread-1'.
The issue only occurs when batching is enabled and flask auto reload is enabled. Disabling batching or disabling the flask auto-reload resolves the issue. The issue also only occurs with a many to many relationship that include theback_populates
param in the relationships.Below is a [somewhat] minimal example to reproduce the issue. The app.py, schema.py, and requirements.txt file are below.
Let me know if you need more info or clarification. Thanks!
Repro:
0) set up a virtual env and install packages from
requirements.txt
below. Then copyapp.py
andschema.py
to your working directory.FLASK_ENV=development && flask run
{ __schema { types { name } } }
and receive errorTypeError: UserConvo fields cannot be resolved. There is no current event loop in thread 'Thread-1'.
{ __schema { types { name } } }
and receive no error.Error Trace:
schema.py:
app.py
requirements.txt (this is not a minimal requirements.txt. it's from the project that i'm working on.)
The text was updated successfully, but these errors were encountered: