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

generate searchable aggregateItems 2 additional levels #268

Merged
merged 8 commits into from
Oct 27, 2021

Conversation

sundersc
Copy link
Contributor

@sundersc sundersc commented Oct 19, 2021

Description of changes

GraphQL searchable aggregateItems field contains a union type for the result field. It requires two additional level of traversing with the default statement depth (2) to generate the queries as expected.

Description of how you validated changes

  • Manual test
  • yarn test

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • Breaking changes to existing customers are released behind a feature flag or major version update
  • Changes are tested using sample applications for all relevant platforms (iOS/android/flutter/Javascript) that use the feature added/modified

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 October 19, 2021 22:44
@sundersc sundersc requested a review from a team October 19, 2021 22:44
@codecov-commenter
Copy link

codecov-commenter commented Oct 20, 2021

Codecov Report

Merging #268 (475c409) into master (8c78e97) will decrease coverage by 0.21%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #268      +/-   ##
==========================================
- Coverage   85.53%   85.32%   -0.22%     
==========================================
  Files         144      144              
  Lines        6617     6779     +162     
  Branches     1553     1709     +156     
==========================================
+ Hits         5660     5784     +124     
- Misses        883      905      +22     
- Partials       74       90      +16     
Impacted Files Coverage Δ
.../graphql-docs-generator/src/generator/getFields.ts 86.84% <75.00%> (-5.76%) ⬇️
...psync-modelgen-plugin/src/utils/process-has-one.ts 75.00% <0.00%> (-10.72%) ⬇️
packages/graphql-types-generator/src/errors.ts 65.62% <0.00%> (-8.45%) ⬇️
...kages/amplify-codegen/src/walkthrough/configure.js 82.05% <0.00%> (-6.84%) ⬇️
...sync-modelgen-plugin/src/utils/process-has-many.ts 60.00% <0.00%> (-6.67%) ⬇️
packages/amplify-codegen/src/commands/add.js 75.00% <0.00%> (-1.93%) ⬇️
...ackages/amplify-codegen/src/commands/statements.js 88.23% <0.00%> (-1.77%) ⬇️
...tor/src/compiler/visitors/collectAndMergeFields.ts 90.74% <0.00%> (-1.72%) ⬇️
...ages/graphql-types-generator/src/swift/language.ts 65.38% <0.00%> (-1.29%) ⬇️
...ges/graphql-types-generator/src/serializeToJSON.ts 83.63% <0.00%> (-1.27%) ⬇️
... and 43 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c78e97...475c409. Read the comment docs.

Copy link
Contributor

@frimfram frimfram left a comment

Choose a reason for hiding this comment

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

This PR needs more error & edge case checking. Also recommend creating a feature flag to turn on/off this change.


function adjustDepth(fieldName, depth) {
if (fieldName == 'aggregateItems') {
return depth + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

can the depth be increased indefinitely? Would need to check for some upper bound.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a statement to check for the typename. Since 'SearchableAggregateResult' type is generated by CLI V2 transformer, we can be sure this doesn't go through infinite loop.

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 of isGraphQLAggregateField(field) is true and depth >= maxDepth, we may need to either return depth or throw errorr. Currently it'll return depth-1 which may not be what's intended.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above. If it is the aggregate field but the depth reaches maxDepth, we cannot return depth-1 here. I suggest throwing an error.

Copy link
Contributor Author

@sundersc sundersc Oct 25, 2021

Choose a reason for hiding this comment

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

The only way this can reach maxDepth is if someone have a schema like below.

type SearchableAggregateResult {
  aggregateItems: SearchableAggregateResult
}

Modified to throw an error if depth exceeds 100.

@sundersc sundersc requested a review from frimfram October 21, 2021 00:15

function adjustDepth(fieldName, depth) {
if (fieldName == 'aggregateItems') {
return depth + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above. If it is the aggregate field but the depth reaches maxDepth, we cannot return depth-1 here. I suggest throwing an error.

const getPossibleTypeSpy = jest.spyOn(schema, 'getPossibleTypes');
getFields(schema.getQueryType().getFields().aggregateItems, schema, maxDepth, { useExternalFragmentForS3Object: false });
expect(getPossibleTypeSpy).toHaveBeenCalled();
expect(getFragment).toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes happen on getField but is not covered in the 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.

The main reason to traverse two additional levels for 'aggregateItems' field is to make sure that the union type (fragments) are generated with default depth of 2. In this test, we are invoking getFields with default depth of 2 and make sure that the union type is generated fully. This will happen only if the getFields is looking for two additional levels for 'aggregateItems' field.


expect(getFragment.mock.calls[1][0]).toEqual(aggregateBucketResult);
expect(getFragment.mock.calls[1][1]).toEqual(schema);
expect(getFragment.mock.calls[1][2]).toEqual(maxDepth - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

If the getField is tested, the expect value should be depth+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the depth is not traversing two additional levels, then getFragment would never be called for the schema being tested here. getFragment being invoked twice confirms that getFields is looking for two additional levels when field is of type 'SearchableAggregateResult' > 'aggregateItems'.

@sundersc sundersc requested a review from AaronZyLee October 25, 2021 06:13
return depth - 1;
}

function isGraphQLAggregateField(field) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: could be simplified to:

function isGraphQLAggregateField(field) {
  return (
    field &&
    field.name == 'aggregateItems' &&
    field.type?.ofType?.name == 'SearchableAggregateResult'
  ):
}

@dpilch dpilch merged commit a54db9f into aws-amplify:master Oct 27, 2021
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.

5 participants