hybrid_property annotations with built-in types defaulting to `ForwardRef` and failing schema compilation
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 35/100
Piste de recherche
Examinez graphene_sqlalchemy/converter.py, en particulier convert_hybrid_property_return_type et convert_sqlalchemy_hybrid_property_bare_str, en utilisant comme reproduction les exemples de hybrid_property de MyModel signalés. Vérifiez comment les types de retour annotés str et bool sont convertis lors de la construction du schéma. Le travail est terminé lorsque les annotations intégrées ne provoquent plus l’échec de la compilation du schéma en tant que valeurs ForwardRef, ou lorsque l’erreur résultante explique clairement le comportement pris en charge.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
dependencies:
python 3.10.7
sqlalchemy==1.4.48
graphene==3.2.2
graphene-sqlalchemy==v3.0.0b4
graphql-server[flask]==v3.0.0b6
I am upgrading from graphene==2.1.9 and graphene-sqlalchemy==2.3.0 where these hybrid properties worked.
simplified example:
# myproject/routes/graphql_view.py
from myproject.schema import schema
GraphQLView.as_view('graphql', schema=schema, graphiql=False) # simplified, used in flask create_app in __init__.py
# myproject/models/core.py
class MyModel(db.Model, DictMixin, Timestamp):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
...
@hybrid_property
def prop_string -> str:
# logic based on model's other fields
result: str = ...
return result
@hybrid_property
def prop_boolean -> bool:
# logic based on model's other fields
result: bool = ...
return result
# myproject/schema.py
from myproject.models.core import MyModel
class MyModelQuery(SQLAlchemyObjectType):
class Meta:
model = MyModel
interfaces = (relay.Node,)
class Query(graphene.ObjectType):
node = relay.Node.Field()
... # other query fields
model = graphene.Field(MyModelQuery, model_id=graphene.Int())
def resolve_model(self, info, model_id):
query = MyModelQuery.get_query(info)
return query.get(model_id)
schema = graphene.Schema(query=Query, mutation=Mutation)
It seems like support was added for these annotations in https://github.com/graphql-python/graphene-sqlalchemy/pull/340 in v3.0.0b2, but then is no longer working as expected since several refactors.
I get this error for the string return type hybrid
../../../myproject/schema.py:581: in <module>
schema = graphene.Schema(query=Query, mutation=Mutation)
../../../venv/lib/python3.10/site-packages/graphene/types/schema.py:440: in __init__
self.graphql_schema = GraphQLSchema(
../../../venv/lib/python3.10/site-packages/graphql/type/schema.py:224: in __init__
collect_referenced_types(query)
../../../venv/lib/python3.10/site-packages/graphql/type/schema.py:433: in collect_referenced_types
collect_referenced_types(field.type)
../../../venv/lib/python3.10/site-packages/graphql/type/schema.py:433: in collect_referenced_types
collect_referenced_types(field.type)
../../../venv/lib/python3.10/site-packages/graphql/type/schema.py:433: in collect_referenced_types
collect_referenced_types(field.type)
../../../venv/lib/python3.10/site-packages/graphql/type/schema.py:432: in collect_referenced_types
for field in named_type.fields.values():
/[...]/.pyenv/versions/3.10.7/lib/python3.10/functools.py:981: in __get__
val = self.func(instance)
../../../venv/lib/python3.10/site-packages/graphql/type/definition.py:811: in fields
raise cls(f"{self.name} fields cannot be resolved. {error}") from error
E TypeError: MyModelQuery fields cannot be resolved. No model found in Registry for forward reference for type ForwardRef('str'). Only forward references to other SQLAlchemy Models mapped to SQLAlchemyObjectTypes are allowed.
and this for the Boolean.
TypeError: MyModelQuery fields cannot be resolved. No model found in Registry for forward reference for type ForwardRef('bool'). Only forward references to other SQLAlchemy Models mapped to SQLAlchemyObjectTypes are allowed.
MyModel in reality has many many fields so it was also hard to figure out it was the hybrid_property fields that were causing problems, vs. the actual relationship fields etc
Which seems to be because the function convert_hybrid_property_return_type
https://github.com/graphql-python/graphene-sqlalchemy/blob/v3.0.0b4/graphene_sqlalchemy/converter.py#L640
returns a string version of the type, and string defaults to being processed by convert_sqlalchemy_hybrid_property_bare_str even though those strings are valid types, not ForwardRefs.
https://github.com/graphql-python/graphene-sqlalchemy/blob/v3.0.0b4/graphene_sqlalchemy/converter.py#L632
I am getting around this by explicitly declaring the graphene types but my understanding was that graphene-sqlalchemy should be able to pick them up automatically due to the recent support that was integrated, as referenced above.
workaround:
class MyModelQuery(SQLAlchemyObjectType):
class Meta:
model = MyModel
interfaces = (relay.Node,)
prop_string = graphene.String()
prop_boolean = graphene.Boolean()
If this is intended behaviour, could the error be more helpful? Spent several hours digging as I was not attempting anything with a ForwardRef type.
- Langage dominant
- Python
- Étoiles
- 984
- Forks
- 224
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de graphql-python/graphene-sqlalchemy
-
Difficulté 4/5 3-5 jours Accessibilité débutants 38/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 52/100
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
graphql-python/graphene-sqlalchemy#422 · 1 commentaire ·
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
graphql-python/graphene-sqlalchemy#419 · 3 commentaires ·
Toutes les issues de graphql-python/graphene-sqlalchemy
Issues similaires
-
area: desktop area: website priority: P2 type: feature
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
appandflow/stim#3411 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 Moins d'une heure Accessibilité débutants 88/100
baptistehamon/lsapy#185 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
PolicyEngine/policyengine-us#10073 ·
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
syedhamidali/radarx#277 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Arekkazu/sgpmp-backend#549 ·
Les mainteneurs répondent en général sous 1 jour