Description
Is your feature request related to a problem? Please describe.
There are lots of cases where a model is self-referential and the entities have a bi-directional relationship. Eg. posts with comments where each comment has a reference back to the post. When queries and mutations are generated for these models, the return objects can be unnecessarily nested.
Describe the solution you'd like
Be able to define optional codegen depth on a per-operation basis that will override the global default. The following is an example @model SDL where this behavior would be specified
directive @model(
queries: ModelQueryMap,
mutations: ModelMutationMap,
subscriptions: ModelSubscriptionMap
) on OBJECT
input ModelMutationMap { create: String, update: String, delete: String, createDepth: Int, updateDepth: Int, deleteDepth: Int }
input ModelQueryMap { get: String, list: String, getDepth: Int, listDepth: Int }
input ModelSubscriptionMap {
onCreate: [String]
onUpdate: [String]
onDelete: [String]
level: ModelSubscriptionLevel
}
enum ModelSubscriptionLevel { off public on }
And then it could be used in the following way:
type Comment @model(queries: {getDepth: 4, listDepth: 5}) {
id: ID
}
to generate get queries with a depth of 4 and list queries with a depth of 5.
Describe alternatives you've considered
Specify a reasonable codegen depth for the current project and create custom queries / mutations for any operations where this default doesn't work. This creates unnecessary work making sure these custom queries stay in sync with the rest of the project.
Another option that seems like it may solve most use cases is to ignore the "items" field in the depth count when generating queries for one-to-many relationships. Based on discussion in aws-amplify/amplify-cli#1013 this would generate code that is closer to developer expectations. However I would not say this is the same feature because there still may be cases where you have some models that are deeply nested and others that aren't.
Additional context
There have been some other related requests made aws-amplify/amplify-cli#1013, but they all seem to target trying to come up with more sensible defaults whereas this gives the developer total control to adjust depth for each operation. Another related issue is aws-amplify/amplify-cli#2977.