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

Add a method to get identifiers used in a LazyReference

Open gbitaudeau opened this issue 2 years ago • 2 comments
trafficstars

Is your feature request related to a problem? Please describe.

When using LazyReference (using generatemodelsforlazyloadandcustomselectionset option for codegen), sometimes you need to manipulate a model object and discover his relations without lazy loading it.

Describe the solution you'd like

With this schema:

enum PostStatus {
  ACTIVE
  INACTIVE
}

type Post @model @auth(rules: [{allow: public}]) {
  id: ID!
  title: String!
  rating: Int!
  status: PostStatus!
  # new field with @hasMany
  comments: [Comment] @hasMany
}

# new model
type Comment @model {
  id: ID!
  content: String
  post: Post @belongsTo
}

I would like to have something like comment.postId because comment.post is async and I don't need to full load a post.

Describe alternatives you've considered

Custom extension:

extension LazyReference {
    var identifier: String? {
        switch self._state {
        case .notLoaded(let identifiers):
            return identifiers?.first?.value
        case .loaded(let model):
            return model?.identifier
        }
    }
}

public extension Comment {
    var postId: String? {
        return _post.identifier
    }
}

But this uses an internal property LazyReference._state and it might not be available in future release.

Is the feature request related to any of the existing Amplify categories?

DataStore

Additional context

No response

gbitaudeau avatar Sep 29 '23 14:09 gbitaudeau

This has been identified as a feature request. If this feature is important to you, we strongly encourage you to give a 👍 reaction on the request. This helps us prioritize new features most important to you. Thank you!

github-actions[bot] avatar Sep 29 '23 15:09 github-actions[bot]

Thanks for opening the feature request. Our team will try to prioritize this in the backlog and provide an update.

harsh62 avatar Sep 29 '23 15:09 harsh62