amplify-swift icon indicating copy to clipboard operation
amplify-swift copied to clipboard

codegen of @hasOne doesn't hide query fields

Open kallyaleksiev opened this issue 3 years ago • 1 comments

Describe the bug

Codegen of @hasOne for swift with specified fields does not hide those fields.

I have a slight issue with codegen for swift with a schema like for example the following:

type Project @model {
  id: ID!
  
  teamID: ID! @index(name: "byTeamID", queryField: "projectByTeamID")
  team: Team! @hasOne(fields: ["teamID"])
}

type Team @model {
  id: ID!
}

With the old Transformer, the code generation would hide the teamID field, so that I would get something like this

public struct Project: Model {
  public let id: String
  public var team: Team

...

model.fields(
      .id(),
      .belongsTo(project.team, is: .required, ofType: Team.self, targetName: "teamID"))

However, after we migrated to Transformer v2, the codegen no longer hides the teamID so I get something like this

public struct Project: Model {
  public let id: String
  public var teamID: String
  public var team: Team
...
model.fields(
      .id(),
      .field(project.teamID, is: .required, ofType: .string),
      .hasOne(project.team, is: .required, ofType: Team.self, associatedWith: Team.keys.id, targetName: "teamID"),
  • The behaviour of codegen is the same even if I don't have an index on teamID - the only way to hide it is to not specify fields but the issue is we need that secondaryIndex on teamID

  • Why does it not hide it? Given that the team is non-optional and teamID is associated with Team.keys.id, I don't see a reason to keep teamID

  • It was hiding it before the migration and

  • If I manually edit the generated swift model files to remove teamID and to replace the .hasOne association with .belongsTo everything works perfectly

I tried various ways to generate the schema and all that but they all end up the same way.

So is there another "better" way to generate that relationship along with the index so that teamID gets hidden?

Steps To Reproduce

1. Create a new project with api

2. Create a schema like this 

type Project @model {
  id: ID!
  
  teamID: ID! @index(name: "byTeamID", queryField: "projectByTeamID")
  team: Team! @hasOne(fields: ["teamID"])
}

type Team @model {
  id: ID!
}
  1. run amplify codegen models

  2. See generated models

Expected behavior

teamID field of Project is hidden

Amplify Framework Version

1.14.0

Amplify Categories

API

Dependency manager

Cocoapods

Swift version

5.5.2

CLI version

7.6.15

Xcode version

13.2.1

Relevant log output

No response

Is this a regression?

Yes

Regression additional context

No response

Device

iPhone

iOS Version

≥ iOS 13

Specific to simulators

No response

Additional context

No response

kallyaleksiev avatar Feb 07 '22 23:02 kallyaleksiev

was wondering if there are any updates or plans on this? I can see the draft pr, but apart from that is it known whether the issue will be addressed anytime soon? as you probably already know, the issue is still there for cli version 7.6.25

kallyaleksiev avatar Mar 21 '22 17:03 kallyaleksiev

I believe with the latest CLI 12+ and the lazy loading capabilities enabled, described in https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/ we have fixed this, ie. the teamId in your example should be hidden and you can lazily load the team from the project. Lazy loading was released sometime in February 2023. Please let us know if you are still blocked on your development and we can further help

lawmicha avatar Aug 10 '23 16:08 lawmicha