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 am upgrading from graphene==2.1.9 and graphene-sqlalchemy==2.3.0 where these hybrid properties worked.
simplified example:
# myproject/routes/graphql_view.pyfrommyproject.schemaimportschemaGraphQLView.as_view('graphql', schema=schema, graphiql=False) # simplified, used in flask create_app in __init__.py# myproject/models/core.pyclassMyModel(db.Model, DictMixin, Timestamp):
id=db.Column(db.Integer, primary_key=True, autoincrement=True)
...
@hybrid_propertydefprop_string->str:
# logic based on model's other fieldsresult: str= ...
returnresult@hybrid_propertydefprop_boolean->bool:
# logic based on model's other fieldsresult: bool= ...
returnresult# myproject/schema.pyfrommyproject.models.coreimportMyModelclassMyModelQuery(SQLAlchemyObjectType):
classMeta:
model=MyModelinterfaces= (relay.Node,)
classQuery(graphene.ObjectType):
node=relay.Node.Field()
... # other query fieldsmodel=graphene.Field(MyModelQuery, model_id=graphene.Int())
defresolve_model(self, info, model_id):
query=MyModelQuery.get_query(info)
returnquery.get(model_id)
schema=graphene.Schema(query=Query, mutation=Mutation)
It seems like support was added for these annotations in #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
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.
You're spot on with your understanding of the problem. Our approach to handling forward references was indeed shaped during a time of less clarity in Python's typing landscape. The shift towards from __future__ import annotations does change the dynamics significantly.
Your workaround of explicitly excluding affected fields or setting types for specific fields is a solid interim solution. Our recent update (#371 was intended to improve handling by inferring types from annotations instead of converting hybrids to strings. However, with from __future__ import annotations turning all hints into ForwardRefs, this does create a hurdle in scenarios like yours.
Manually setting the type for hybrids or use manual ForwardRefs (with type hints in "") while avoiding from __future__ import annotations are good ways to navigate this for now.
I agree that this is not the ideal state, and we're aiming to address this post the 3.0 release. Thanks for pushing this by sharing your experience.
dependencies:
I am upgrading from
graphene==2.1.9
andgraphene-sqlalchemy==2.3.0
where these hybrid properties worked.simplified example:
It seems like support was added for these annotations in #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
and this for the Boolean.
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 etcWhich 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, notForwardRef
s.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:
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.
The text was updated successfully, but these errors were encountered: