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

fix(graphql): avoid duplicate function when overriding resolvers #9980

Merged
merged 12 commits into from
Mar 28, 2022

Conversation

sundersc
Copy link
Contributor

Description of changes

Avoid creating a duplicate function when overriding a generated resolver.

Issue #, if available

Fixes #9650

Description of how you validated changes

  • E2E test
  • Manual test

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@sundersc sundersc requested a review from a team as a code owner March 14, 2022 23:17
@sundersc sundersc marked this pull request as draft March 15, 2022 22:24
@sundersc sundersc force-pushed the duplicate-resolver-override branch from 1729559 to c5b5b79 Compare March 21, 2022 22:34
@codecov-commenter
Copy link

codecov-commenter commented Mar 21, 2022

Codecov Report

Merging #9980 (bffe39d) into master (2f2229c) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #9980   +/-   ##
=======================================
  Coverage   54.09%   54.09%           
=======================================
  Files         837      837           
  Lines       46374    46374           
  Branches     9890     9890           
=======================================
  Hits        25084    25084           
  Misses      19295    19295           
  Partials     1995     1995           

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@sundersc sundersc marked this pull request as ready for review March 23, 2022 04:26
return false;
}

for (const slotItem of slotEntires) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than needing a mutable slotIndex can we just call slotEntries.forEach((slotEntry, slotIndex) => { /** your logic below **/ };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block requires early termination if match is found. The only way to early exit foreach is using exception, that would look ugly. I have also considered the usage of every() and some(), however for .. of ... seems to be the best fit here. I'm open to suggestions if there is a better way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see. I'm fine w/ this as-is in that case.


for (const slotItem of slotEntires) {
const [slotItemRequestMappingTemplate, slotItemResponseMappingTemplate] = [
(slotItem.requestMappingTemplate as any)?.name ?? '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific type we can use here instead of any?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no. We could alter the class structure we have for storing templates but that would be outside the scope of the change intended here and requires quite a bit of refactoring.

Copy link
Contributor

@lazpavel lazpavel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


// If name matches, then it is an overridden resolver
if (
slotItemRequestMappingTemplate === ((requestMappingTemplate as any)?.name ?? '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you use $TSAny if there isn't a better alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$TSAny is defined on amplify-cli-core. Currently, amplify-graphql-transformer-core doesn't have a dependency on amplify-cli-core. We can consider adding something similar to $TSAny type within data package once we split the repository.


// If name matches, then it is an overridden resolver
if (
slotItemRequestMappingTemplate === ((requestMappingTemplate as any)?.name ?? '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case the user only wants to override the request mapping template but not the response VTL, will this logic work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. For example, if response VTL is not overridden, (responseMappingTemplate as any)?.name would be undefined. In that case, the slot's response VTL is preserved.

slot.responseMappingTemplate = (responseMappingTemplate as any)?.name
        ? responseMappingTemplate
        : slot.responseMappingTemplate;

Copy link
Contributor

@alharris-at alharris-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM - one n.p. on the slotExists method.

Comment on lines 182 to 183
const slot = this.findSlot(slotName, requestMappingTemplate, responseMappingTemplate);
if (slot) {
return true;
}
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can probably simplify this to return this.findSlot() !== undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was me being lazy 🤣 I've updated it now.

@sundersc sundersc force-pushed the duplicate-resolver-override branch from 9051d44 to bffe39d Compare March 25, 2022 22:55
@sundersc sundersc merged commit 94398f8 into aws-amplify:master Mar 28, 2022
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.

overrid an auto generated pipeline resolver create another function
5 participants