Typed Errors and Graphene
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
No se nombran archivos del repositorio, pruebas ni puntos de entrada. Empieza revisando el ErrorInterface, el error union y la mutation shape propuestos en el issue; después, aclara con los maintainers si esto debe documentarse o implementarse y qué criterios de aceptación definirían la finalización.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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!
- Lenguaje dominante
- Python
- Estrellas
- 8.2k
- Forks
- 822
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de graphql-python/graphene
-
Tutorial mistakesAbierto🐛 bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
graphql-python/graphene#1389 · 5 comentarios · 2 reacciones ·
-
Support OneOf input object typesAbierto
Dificultad 4/5 3-5 días Aptitud para principiantes 68/100
graphql-python/graphene#1606 ·
-
Python 3.14 supportAbierto✨ enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 38/100
graphql-python/graphene#1601 · 2 comentarios ·
-
✨ enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 42/100
graphql-python/graphene#1600 ·
-
Inaccurate Float-to-Decimal Conversion in `parse_value` of `Decimal` `Scalar`Posiblemente ocupada @mak626 la tomó hace 606 días. Abierto🐛 bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 55/100
graphql-python/graphene#1593 ·
Todos los issues de graphql-python/graphene
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
EvaluationSuite.run fails with default args_for_task and mutates supplied kwargsPosiblemente ocupada @ktz03 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
huggingface/evaluate#825 ·
Los mantenedores suelen responder en 1 día
-
Add `django-upgrade` to the CIAbiertodependencies feature github_actions good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
wemake-services/wemake-django-template#3149 ·
Los mantenedores suelen responder en 1 día
-
[request] vsg/1.1.16Abiertoupstream update
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
conan-io/conan-center-index#31142 ·
Los mantenedores suelen responder en 1 día
-
area:core bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día