-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
rawRequest() return type wrong: can never include errors #174
Comments
I don't agree with your solution because many libs are not working in this way such as apollo. The common way which is most libs do is:
So in ...
if (response.ok && result.data) {
const { headers, status } = response
return { ...result, headers, status }
}
... Additionally, This bug is also happens in ...
if (response.ok && result.data) {
return result
}
... |
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. new GraphQLClient({ throwOnClientError: true })
// or
new GraphQLClient({ clientErrorsAsData: true }) Then use conditional types to maintain an accurately typed API. |
We are currently trying to debug some issues we have interacting with a 3rd party GraphQL API. I was hoping to use |
I also vote for errors as data as implemented in #174 If it helps, I can try to do the config option mentioned earlier and submit another pull request. |
That would be most welcome |
I think the most important here is to fix the return type signature, as it is misleading. You can decide on how to handle errors later, but I would prefer there's no breaking changes, so error handling method should either be configurable or left as is now (i.e. throwing an exception). |
@AlexIII PR welcome |
@jasonkuhrt ok, here it is. No breaking changes. Let me know if something's wrong. |
Same issue here. I presumed |
Good news! We've got the return type fixed. To those who want to see errors in the return type - please reconsider. The language already has a built-in method for handling errors, namely, exceptions. There's simply no good reason not to use them, as they lead to much cleaner code (common error interface, error propagation without boilerplate, handling errors in one place, etc.). |
I just ran into this problem as well because it looks like graphql-codegen is still generating code that includes the
I think it would be worth reconsidering try/catch versus explicit return types. A lot of GraphQL APIs end up using the I really like the simplicity of the library and so I'm providing this example as consideration for building on that simplicity. |
The reteurn types of |
Quick comment - the reason why it's here is because it's pretty common in larger (federated) schemas to have partial results - this code block here
Really should be returning the result no matter what if it's truly supposed to be the |
Hey there! Is this issue resolved by this PR? #352 |
Yes, thank you for your PR!. There is still the issue that the errors property should be |
I will work on better TS types soon. |
Actually I'm going to overhaul the error system: #509. |
The return type signature of rawRequest() includes
errors?: GraphqlError[]
https://github.com/prisma-labs/graphql-request/blob/4b02516fc3707f00bd27ddd724a190bd78a32beb/src/index.ts#L16-L25
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
:https://github.com/prisma-labs/graphql-request/blob/4b02516fc3707f00bd27ddd724a190bd78a32beb/src/index.ts#L71-L76
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.
The text was updated successfully, but these errors were encountered: