Skip to content
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

Merged
merged 4 commits into from
Sep 12, 2023

Conversation

lotmek
Copy link
Contributor

@lotmek lotmek commented Aug 26, 2023

Description

  • Turn url and request into optional parameters
  • Refactor the way to add code in the extensions object

Fixes #247

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 26, 2023

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.

@trevor-scheer
Copy link
Member

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 RESTDataSource and override this method. That means an overridden errorFromResponse method can have any signature, meaning users who currently extend errorFromResponse will not be affected by this change. This isn't the first time I've "discovered" this, but usually surprises me.

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:
throwIfResponseIsError and errorFromResponse are "linked" by their types. If you override errorFromResponse and provide a different signature (specifically, an extended one), you're going to be surprised when throwIfResponseIsError calls your method. I'm actually not sure how to fix this.

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('...');
  }
}

Copy link
Member

@trevor-scheer trevor-scheer left a 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.

Comment on lines +364 to +372
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 }),
Copy link
Member

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.

Copy link
Contributor Author

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:

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');

@trevor-scheer trevor-scheer merged commit c6ac292 into apollographql:main Sep 12, 2023
@github-actions github-actions bot mentioned this pull request Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make errorFromResponse parameters optional
2 participants