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: allow required uni-directional hasOne with @connection when datastore is disabled #888

Merged
merged 5 commits into from
Oct 3, 2024

Conversation

dpilch
Copy link
Member

@dpilch dpilch commented Oct 2, 2024

Description of changes

CLI v11.0.0 introduced creating the model introspection schema at the end of every amplify push. Some customers using the v1 transformer with @connection with DataStore disabled were unable to upgrade to later versions of the CLI and successfully push. The model generation at the end of the push failed with DataStore does not support 1 to 1 connection with both sides of connection as optional field. This error is not applicable to customers not using DataStore. This error would also print the case the connection was required on both sides.

This change removes this restriction from customers not using DataStore.

Currently working (user is not required).

type User @model {
  id: ID!
}
type Session @model {
  id: ID!
  sessionUserId: ID!
  # works when user is not required
  user: User @connection(fields: ["sessionUserId"])
}

Results in following connection info.

{
  kind: 'HAS_ONE',
  associatedWith: {
    type: 'ID',
    isNullable: false,
    isList: false,
    name: 'id',
    directives: []
  },
  associatedWithFields: [],
  connectedModel: { name: 'User', type: 'model', directives: [], fields: [ [Object] ] },
  isConnectingFieldAutoCreated: false,
  targetName: 'sessionUserId',
  targetNames: []
}

Removing the restriction allows to set user as a required field.

type User @model {
  id: ID!
}
type Session @model {
  id: ID!
  sessionUserId: ID!
  # user is required
  user: User! @connection(fields: ["sessionUserId"])
}

Results in the following connection info.

{
  kind: 'HAS_ONE',
  associatedWith: {
    type: 'ID',
    isNullable: false,
    isList: false,
    name: 'id',
    directives: []
  },
  associatedWithFields: [],
  connectedModel: { name: 'User', type: 'model', directives: [], fields: [ [Object] ] },
  isConnectingFieldAutoCreated: false,
  targetName: 'sessionUserId',
  targetNames: []
}

Codegen Paramaters Changed or Added

N/A

Issue #, if available

aws-amplify/amplify-category-api#1419

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • E2E test run linked
  • Tests are changed or added
  • 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.

@dpilch dpilch changed the title fix: allow required uni-directional hasOne with fields fix: allow required uni-directional hasOne with @connection with fields when datastore is disabled Oct 3, 2024
@dpilch dpilch changed the title fix: allow required uni-directional hasOne with @connection with fields when datastore is disabled fix: allow required uni-directional hasOne with @connection when datastore is disabled Oct 3, 2024
@dpilch dpilch marked this pull request as ready for review October 3, 2024 15:48
@dpilch dpilch requested a review from a team as a code owner October 3, 2024 15:48
it('uni-directional One:One connection with optional field and datastore is enabled', () => {
const field = { ...modelMap.Session.fields[2] };
field.isNullable = true;
const connectionInfo = (processConnections(field, modelMap.User, modelMap, false) as any) as CodeGenFieldConnectionBelongsTo;
Copy link
Member

Choose a reason for hiding this comment

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

Should dataStoreEnabled be true here?

Suggested change
const connectionInfo = (processConnections(field, modelMap.User, modelMap, false) as any) as CodeGenFieldConnectionBelongsTo;
const connectionInfo = (processConnections(field, modelMap.User, modelMap, true) as any) as CodeGenFieldConnectionBelongsTo;

@dpilch dpilch enabled auto-merge (squash) October 3, 2024 17:23
@dpilch dpilch merged commit 648f2ed into main Oct 3, 2024
4 checks passed
@palpatim palpatim deleted the v1-transform-bug branch October 3, 2024 17:58
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