Error messaging best practice #579
Replies: 2 comments
-
Example of how we do this on the django side: mirumee/ariadne-django#37 |
Beta Was this translation helpful? Give feedback.
-
For mutations making errors part of your schema is best approach. Here's Ariadne doc with examples. For
Speaking from experience, combination of errors in schema, nulls and using custom error formatter for things like missing auth/expired auth token most powerful:
I've found that if you follow "errors in schema" approach for mutations, those mutations start resembling Django's form views:
Here's example from my OS project where this pattern is clearly visible: @post_reply_mutation.field("postReply")
@convert_kwargs_to_snake_case
@error_handler
async def resolve_post_reply(
_, info: GraphQLResolveInfo, *, input: dict # pylint: disable=redefined-builtin
):
# Create pydantic model to use for validation and validate input against it
input_model = create_input_model(info.context)
cleaned_data, errors = validate_model(input_model, input)
if cleaned_data.get("thread"):
thread = await load_thread(info.context, cleaned_data["thread"])
else:
thread = None
# If data types validation passed, validate data against database and business logic
if cleaned_data:
validators: Dict[str, List[Validator]] = {
"thread": [
ThreadExistsValidator(info.context),
ThreadCategoryValidator(
info.context, CategoryIsOpenValidator(info.context)
),
ThreadIsOpenValidator(info.context),
],
ErrorsList.ROOT_LOCATION: [
UserIsAuthorizedRootValidator(info.context),
],
}
cleaned_data, errors = await validate_input_data(info.context, validators, cleaned_data, errors)
# Return errors list and unmodified Thread object to the client
if errors:
return {"errors": errors, "thread": thread}
# Update database to post new reply to thread, then return updated thread and new post to client
thread, post = post_reply(info.context, cleaned_data)
return {"thread": thread, "post": post} |
Beta Was this translation helpful? Give feedback.
-
Hi all,
Does anyone found a best way to make consistent error messaging (like permission or validation errors) raised by resolvers? (usage of ariadne with flask)
Beta Was this translation helpful? Give feedback.
All reactions