graphql-request: rawRequest() return type wrong: can never include errors
The return type signature of rawRequest() includes errors?: GraphqlError[]
However, when processing the result of the operation the function explicitly will not return if the result contains any errors. Instead it throws a new ClientError:
So it seems to me that the return type is incorrect, and this has led to confusion in my team about how to correctly handle errors. The correct way is with try/catch, not by checking whether there is an errors property in the return value.
I think the solution is to just remove errors from the return type.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 17 (9 by maintainers)
Commits related to this issue
- fix: return request (#174) — committed to IttisutBm/graphql-request by deleted user 4 years ago
- fix: remove errors from rawRequest return type (#174) (#249) Co-authored-by: Alexander Chernoskutov <alecherov@gmail.com> — committed to jasonkuhrt/graphql-request by AlexIII 3 years ago
I would like to get more feedback about if people prefer thrown error or error as data.
I am in favour of error as data.
Maybe there should be a config option which adjusts the API behaviour.
Then use conditional types to maintain an accurately typed API.
I just ran into this problem as well because it looks like graphql-codegen is still generating code that includes the
GraphQLErrortype. It’s great that this library has removed the ambiguity though, so at least now I can see pretty readily that we need to do this:I think it would be worth reconsidering try/catch versus explicit return types. A lot of GraphQL APIs end up using the
extensionsobject to surface errors, including those that are domain-specific (for instance a constraint violation when a user tries to create a record with a duplicate name). This means if I want to handle the error in my domain code I have to wrap the invocation as per the above before I can extract the relevant extension information from the errors object. My feeling is that this results in (a) annoying boilerplate, (b) less type safety (thrown errors have no type information) and © occasionally puzzling control flow issues (where did that error come from? why didn’t this code run?).I really like the simplicity of the library and so I’m providing this example as consideration for building on that simplicity.