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-relational-transformer): nullability enforcement for references relational fields #2510

Merged
merged 7 commits into from
Apr 29, 2024

Conversation

atierian
Copy link
Member

@atierian atierian commented Apr 29, 2024

Description of changes

1. Validate relational fields are nullable

Relational fields MUST be nullable. These changes validate this rule.

The following is valid because the fields Primary.relatedOne, Primary.relatedMany, RelatedOne.primary and RelatedMany.primary are all nullable / non-required fields.

type Primary @model { 
  id: ID!
  relatedOne: RelatedOne @hasOne(references: "primaryId")
  relatedMany: [RelatedMany] @hasMany(references: "primaryId")
}

type RelatedOne @model { 
  id: ID!
  primaryId: ID
  primary: Primary @belongsTo(references: "primaryId")
}

type RelatedMany @model { 
  id: ID!
  primaryId: ID
  primary: Primary @belongsTo(references: "primaryId")
}

Any of these changes are invalid:

-  relatedOne: RelatedOne @hasOne(references: "primaryId")
+  relatedOne: RelatedOne! @hasOne(references: "primaryId")
-  relatedMany: [RelatedMany] @hasMany(references: "primaryId")
+  relatedMany: [RelatedMany]! @hasMany(references: "primaryId")
+  relatedMany: [RelatedMany!] @hasMany(references: "primaryId")
+  relatedMany: [RelatedMany!]! @hasMany(references: "primaryId")
-  primary: Primary @belongsTo(references: "primaryId")
+  primary: Primary! @belongsTo(references: "primaryId")

2. Validate consistent nullability of Related model's reference fields

Reference fields MUST have consistent nullability. These changes fix a bug in the existing validation implementation and improve the error message.

CDK / CloudFormation Parameters Changed

N/A

Issue #, if available

N/A

Description of how you validated changes

Unit tests

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
  • Any CDK or CloudFormation parameter changes are called out explicitly

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

@atierian atierian marked this pull request as ready for review April 29, 2024 16:35
@atierian atierian requested a review from a team as a code owner April 29, 2024 16:35
@@ -185,7 +185,7 @@ test('fails if used as a has one relationship', () => {
type Member @model {
id: ID!
teamID: String
team: Team @belongsTo(fields: ["teamID"])
team: Team @belongsTo(references: ["teamID"])
Copy link
Member Author

Choose a reason for hiding this comment

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

unrelated fix in test schema

palpatim
palpatim previously approved these changes Apr 29, 2024
AaronZyLee
AaronZyLee previously approved these changes Apr 29, 2024
palpatim
palpatim previously approved these changes Apr 29, 2024
@palpatim palpatim dismissed stale reviews from AaronZyLee and themself via 6a98790 April 29, 2024 22:47
@palpatim palpatim enabled auto-merge (squash) April 29, 2024 22:48
@palpatim palpatim merged commit d540097 into main Apr 29, 2024
5 of 6 checks passed
@palpatim palpatim deleted the relational-nullability-enforcement branch April 29, 2024 22:53
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.

None yet

3 participants