graphql-core coerce_string used instead of graphene.String
还没有人认领这个 Issue。
评估
调研方向
首先跟踪 graphene/types/schema.py 中对标量的处理,并将 graphene/types/scalars.py 与 graphql/type/scalars.py 进行比较。使用 graphene.test.Client.execute 重现 HttpUrl 情况,然后确定预期的修复是保留 graphene.String 的强制转换,还是使其与 graphql-core 保持一致。当行为和预期的强制转换都由回归测试覆盖时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.
- What is the current behavior?
Not 100% sure this is a bug, but it seems not quite right.
graphene.String has the following coerce_string method:
https://github.com/graphql-python/graphene/blob/master/graphene/types/scalars.py#L144
This should mean that any value that is resolved via a graphene.String field gets cast to str
In contrast, graphql-core has this coerce_string method:
https://github.com/graphql-python/graphql-core/blob/a2d52df815f81a62649ee637999fc241af1e67a3/src/graphql/type/scalars.py#L176
...note that in graphql-core any value which passes an isinstance(val, str) check will not get cast to str.
I noticed this because I am using pydantic HttpUrl type in my parent object, the value is an HttpUrl instance (which passes an isinstance(val, str) check).
I noticed in my test cases that the HttpUrl value passes untouched through the graphene.test.Client.execute flow.
Looking at the graphene src this was puzzling because it seemed like it should get cast down to a plain string.
Stepping through with ipdb, I could see in complete_value method:
ipdb> pp result
HttpUrl('https://example.com/test-url, scheme='https', host='example.com', tld='com', host_type='domain', path='test-url')
ipdb> return_type
<graphql.type.definition.GraphQLScalarType object at 0x1059876a0>
ipdb> import inspect
ipdb> inspect.getsource(return_type.serialize)
'def coerce_string(value):\n # type: (Any) -> str\n if isinstance(value, string_types):\n return value\n\n if isinstance(value, bool):\n return u"true" if value else u"false"\n\n return text_type(value)\n'
ipdb> inspect.getsourcefile(return_type.serialize)
'/lib/python3.8/site-packages/graphql/type/scalars.py'
...so we have a graphql-core field rather than a graphene one, which explains why the cast to str didn't happen.
I am guessing it may be due to this code:
https://github.com/graphql-python/graphene/blob/f039af2810806ab42521426777b3a0d061b02802/graphene/types/schema.py#L148
Which raises the question... are the methods on graphene.String and friends just dead code?
Or if they're still used in some other circumstance, should they maybe delegate to corresponding graphql-core methods for sake of consistency, rather than having their own subtly different implementation?
If I change my type def to:
import graphene
class HttpUrl(graphene.String):
pass
class MyParent(graphene.ObjectType):
some_url = HttpUrl(required=True)
...then I get the graphene casting behaviour, again I think because we fall thru this check https://github.com/graphql-python/graphene/blob/f039af2810806ab42521426777b3a0d061b02802/graphene/types/schema.py#L148 and don't substitute a graphql-core type.
In the end this behaviour didn't break anything for me, but it's probably possible to imagine a scenario where it would.
- 主要语言
- Python
- 星标
- 8.2k
- 派生
- 818
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/graphene 的其他 Issue
-
🐛 bug
难度 2/5 1-3 小时 新手友好度 72/100
graphql-python/graphene#1389 · 5 条评论 · 2 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 68/100
graphql-python/graphene#1606 ·
-
✨ enhancement
难度 2/5 1-3 小时 新手友好度 38/100
graphql-python/graphene#1601 · 2 条评论 ·
-
✨ enhancement
难度 4/5 3-5 天 新手友好度 42/100
graphql-python/graphene#1600 ·
-
🐛 bug
难度 2/5 1-3 小时 新手友好度 55/100
graphql-python/graphene#1593 ·
查看 graphql-python/graphene 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 86/100
browser-use/browser-use#5905 ·
-
type: enhancement
难度 2/5 1-3 小时 新手友好度 68/100
ynput/ayon-python-api#363 ·
-
bug needs triage
难度 2/5 1-3 小时 新手友好度 88/100
modelscope/FunASR#3728 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
open-compass/opencompass#2655 ·