-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor errorFromResponse #246
Refactor errorFromResponse #246
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
I think this is a good change, especially since the concrete method here doesn't make use of those params. Thinking out loud because at first glance, this seems like a breaking change. Explaining why it isn't. The signature of the method does not impose any requirements on the signature in concrete class definitions that extend Additionally, users currently calling this method are unaffected - they won't see any change for already including parameters which become optional (though the other direction optional -> required would be problematic). Related thought worth capturing that doesn't impact this PR: class MyDataSource extends RESTDataSource {
override async errorFromResponse({
response,
parsedBody,
}: {
response: FetcherResponse;
parsedBody: string;
extra: string; // not a TS error, but this won't exist if I don't override `throwIfResponseIsError`
}): Promise<GraphQLError> {
return new GraphQLError('...');
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a patch
changeset describing your changes. You can run the changeset tool via npx changeset
and follow the prompts.
const codeByStatus = new Map<number, string>([ | ||
[401, 'UNAUTHENTICATED'], | ||
[403, 'FORBIDDEN'], | ||
]); | ||
const code = codeByStatus.get(response.status); | ||
|
||
return new GraphQLError(`${response.status}: ${response.statusText}`, { | ||
extensions: { | ||
...(response.status === 401 | ||
? { code: 'UNAUTHENTICATED' } | ||
: response.status === 403 | ||
? { code: 'FORBIDDEN' } | ||
: {}), | ||
...(code && { code }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks correct to me but without a test validating your change I'd rather just leave it as is. I don't think the ternaries here are so complex that it necessitates cleanup.
My preference is to revert this, but if you insist then I'd require a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @trevor-scheer the codes are already tested in those two cases:
- throws a FORBIDDEN error when the response status is 403
- throws an UNAUTHENTICATED error when the response status is 401
I am just adding an extra assertion in this third test case (throws an ApolloError when the response status is 500), just to make sure the property is not being added:
await expect(result).rejects.not.toHaveProperty('extensions.code'); |
Description
url
andrequest
into optional parameterscode
in theextensions
objectFixes #247