Replies: 2 comments 18 replies
-
Are you talking about the unions+interfaces approach we discussed few days back? I agree it is a lot of boilerplates, that's true, but I still feel like the added benefit of Type-safety and customizable error behavior are a considerable pro when considering that. Not only that but paired up with graphql-codegen the approach becomes extremely worthy of consideration. https://the-guild.dev/blog/graphql-error-handling-with-fp#typesafe-error-handling-on-the-client-side . (The entire blog is pretty informative) I would say not to dismiss that approach only based on the boilerplate code thingy. If it is developed in a more modular approach yes, even though the API code will increase complexity a little, the good thing will be our API will be safer and the error handling will be a lot more consistent in the future anytime. The client-side boilerplate will be minimal. |
Beta Was this translation helpful? Give feedback.
-
We need a definitive document on this that we can add to Talawa-Docs. It makes no sense making a GitHub discussion the source of truth. Create a Google Doc and when done, let's get the content added to Talawa-Docs |
Beta Was this translation helpful? Give feedback.
-
Right now I'm outlining the structure for graphql error object that will be returned for client's input.
For the following graphql schema:-
The errors can be mapped like this:-
This is only a trivial example, the process is same for queries, mutations and nested resolvers, no matter how many levels of nested arguments you have, or what their type is.
You see that
path
list on the root object? That tells us the resolver/field in which the error happened. If it was a nested resolver/field thepath
list would look like this["dummyMutation", "nestedResolverName"]
.Most of the time you'd not need a nested resolver for a scalar fields, but you can even implement that if it's preferred. A use case would be to protect some data, and throw error if the user who's accessing that field isn't authorized to do so. For example the email field of a user, shouldn't be accessible to anyone but the user who it belongs to and higher ups like admins.
This design would help us return all errors that are present in user's input in one go. But implementing a validator/transformer function for this structure would be equally as hard.
Another way to return errors could be one by one for each argument. Though it would be less flexible and would need many cycles to convey all errors that client's input has.
I had given thought to defining errors as a part of schema itself, and though it looks like an attractive approach, there's way too much boilerplate required for it. And since it's not a part of graphql specification, no tool in graphql ecosystem supports it in any way, so you'd need a boilerplaty approach on both client and server side to handle these errors.
Beta Was this translation helpful? Give feedback.
All reactions