Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

hybrid_property annotations with built-in types defaulting to `ForwardRef` and failing schema compilation

未关闭
#396 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python, sqlalchemy
领域
api, database

调研方向

检查 graphene_sqlalchemy/converter.py,尤其是 convert_hybrid_property_return_type 和 convert_sqlalchemy_hybrid_property_bare_str,并使用报告中的 MyModel hybrid_property 示例进行复现。验证在构建 schema 时,带有注解的返回类型 str 和 bool 是如何转换的。完成的标准是:内置注解在 schema 编译期间不再作为 ForwardRef 值导致失败,或者产生的错误能够清楚地说明受支持的行为。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
985
派生
224
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphql-python/graphene-sqlalchemy 的其他 Issue

查看 graphql-python/graphene-sqlalchemy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。