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: add translation behavior to disable gen 1 patterns #2670

Merged
merged 18 commits into from
Jul 9, 2024
Merged

Conversation

dpilch
Copy link
Member

@dpilch dpilch commented Jun 28, 2024

Description of changes

Add translation behavior to disable gen 1 patterns:

  • Use of @manyToMany
  • Use of @searchable
  • Use of @predictions
  • Use of fields argument on @hasOne, @hasMany, and @belongsTo.
  • Use of @hasOne, @hasMany, and @belongsTo on required fields.
CDK / CloudFormation Parameters Changed
  • Add allowGen1Patterns which defaults to true.

Issue #, if available

N/A

Description of how you validated changes

  • Unit testing

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Any CDK or CloudFormation parameter changes are called out explicitly
  • Relevant documentation is changed or added (and PR referenced) Add Gen 2 SDL docs docs#7793

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

@dpilch dpilch marked this pull request as ready for review July 1, 2024 14:03
@dpilch dpilch requested review from a team as code owners July 1, 2024 14:03
@dpilch dpilch force-pushed the allow-gen1 branch 2 times, most recently from 9d01ea3 to dee28d6 Compare July 2, 2024 16:52
@@ -158,6 +159,14 @@ export class BelongsToTransformer extends TransformerPluginBase {

const validate = (config: BelongsToDirectiveConfiguration, ctx: TransformerContextProvider): void => {
const { field, object } = config;
if (!ctx.transformParameters.allowGen1Patterns) {
if (field.type.kind === Kind.NON_NULL_TYPE) {
throw new InvalidDirectiveError(`@${BelongsToDirective.name} cannot be used on required fields.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Pointing customer to the faulty field by including its name along with parent type name can help them debug faster.

throw new InvalidDirectiveError(`@${BelongsToDirective.name} cannot be used on required fields.`);
}
if (config.fields) {
throw new InvalidDirectiveError(`fields argument on @${BelongsToDirective.name} is deprecated. Use references instead.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: same suggestion as above about more informative error message. Please apply the same for other Errors added in this PR.

).not.toThrow();
});

test('does not allow @manyToMany', () => {
Copy link
Member

Choose a reason for hiding this comment

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

nit here & throughout: test titles should reflect the conditions better, as in

Suggested change
test('does not allow @manyToMany', () => {
test('does not allow @manyToMany when allowGen1Patterns is false', () => {

You could accomplish the same thing by organizing nested describe blocks:

describe('allowGen1Patterns', () => {
  test('defaults to allow', () => {...});
  describe('flag is true', () => {/* all the cases */});
  describe('flag is false', () => {/* all the cases */});
});

throw new InvalidDirectiveError(`@${HasManyDirective.name} cannot be used on required fields.`);
}
if (config.fields) {
throw new InvalidDirectiveError(`fields argument on @${HasManyDirective.name} is deprecated. Use references instead.`);
Copy link
Member

Choose a reason for hiding this comment

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

Here and throughout all error messages: It's not deprecated, it's outright forbidden in Gen2 projects.

@@ -25,7 +25,7 @@ import { UserDefinedSlot } from '@aws-amplify/graphql-transformer-core';
export const constructTransform: (config: TransformConfig) => GraphQLTransform;

// @public (undocumented)
export const constructTransformerChain: (options?: TransformerFactoryArgs) => TransformerPluginProvider[];
export const constructTransformerChain: (options?: TransformerFactoryArgs, allowGen1Patterns?: boolean) => TransformerPluginProvider[];
Copy link
Member

Choose a reason for hiding this comment

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

Can we put allowGen1Patterns into TransformerFactoryArgs rather than tacking on a dangling argument?

@@ -314,6 +314,8 @@ export interface OptimisticConflictResolutionStrategy extends ConflictResolution
// @public
export interface PartialTranslationBehavior {
readonly allowDestructiveGraphqlSchemaUpdates?: boolean;
// @internal
readonly _allowGen1Patterns?: boolean;
Copy link
Member Author

Choose a reason for hiding this comment

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

JSII requires params marked with @internal to be prefixed with an underscore.

@dpilch dpilch enabled auto-merge (squash) July 5, 2024 16:55
@dpilch dpilch merged commit 38d1a71 into main Jul 9, 2024
6 checks passed
@dpilch dpilch deleted the allow-gen1 branch July 9, 2024 22:46
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.

3 participants