Typed Errors and Graphene
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
調査の方向性
リポジトリのファイル、テスト、エントリポイントは指定されていません。まず、issue で提案されている ErrorInterface、error union、mutation shape を確認し、その後、これをドキュメント化するのか実装するのか、また完了の基準となる受け入れ基準は何かを maintainer と確認してください。
索引モデルが issue の本文から書いたものです。
説明
Hi folks,
I'm raising this issue to gauge ideas from the Python community on error handling best practices.
The usual way to handle errors in GraphQL is by inspecting the top-level errors key:
{
"errors": {
# ... your error details ...
},
"data": null
}
However, this is usually problematic for API clients as they don't know what to expect from this errors key.
This means that error discoverability is impacted.
Another downside, is that the data key must be null when these high-level errors are raised.
In many situations API clients are interested in calling a mutation and, even if it fails, they'd like to receive data back. This data can be anything, but it's usually the object being mutated itself.
Another approach is extending upon this idea of typed errors that is strongly supported by Lee Byron as you can see here.
This seems to solve both problems above (along with many others). Here's my take on what this would look like in Graphene:
class ErrorInterface(graphene.Interface):
message = graphene.NonNull(graphene.String)
class ThingAErrorType(graphene.ObjectType):
class Meta:
interfaces = [ErrorInterface]
class ThingBErrorType(graphene.ObjectType):
class Meta:
interfaces = [ErrorInterface]
class MySweetMutationErrorUnion(graphene.Union):
class Meta:
types = [ThingAErrorType, ThingBErrorType]
class MySweetMutation():
error = graphene.Field(MySweetMutationErrorUnion)
output = graphene.Field(MySweetOutputType)
def mutate(self, info, input):
try:
thing_a = do_thing_a(input)
except ThingAException:
return MySweetMutation(error=ThingAErrorType())
try:
thing_b = do_thing_b(input)
except ThingBException:
return MySweetMutation(error=ThingBErrorType())
# happy path!
output = MySweetOutputType(thing_a=thing_a, thing_b=thing_b)
return MySweetMutation(output=output)
If we have a look at what the schema looks like, we have this:


And finally, API clients can query this mutation like this:
mutation mySweetMutation($input: MySweetMutationInput!) {
mySweetMutation(input: $input) {
output {
# ....
}
error {
... on ThingAError {
__typename
message
}
... on ThingBError {
__typename
message
}
# We have an interface here so that we
# can extend the union with more errors without breaking
# backwards compatibility!!
__typename
message
}
}
}
I'm interested in your thoughts in this approach.
Thanks!
- 主要言語
- Python
- スター
- 8.2k
- フォーク
- 818
- PR マージ指標
- 30日以内にマージされた PR はありません
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
graphql-python/graphene のほかの issue
-
Tutorial mistakes オープン🐛 bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
graphql-python/graphene#1389 · コメント 5 件 · リアクション 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 68/100
graphql-python/graphene#1606 ·
-
Python 3.14 support オープン✨ 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
-
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
-
hcocena オープンpolicies-accepted pre-review precheck-passed
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
Bioconductor/BiocContributions#214 · コメント 5 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
TencentCloud/Octop#1169 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
521xueweihan/HelloGitHub#3778 ·
-
The version checker's trailing attribute region has no control for a less-than inside a quoted value オープンarea: dashboard area: tests bug perceived difficulty: 2 python
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
Nitjsefnie-Harness-Commons/daedalus#1105 · コメント 1 件 ·