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

`ampify codegen model` filters out connection fields

Open TheDutchCoder opened this issue 4 years ago • 4 comments

Describe the bug When you have a connection field in your schema, like this:

type Order
  @model(subscriptions: null)
  @key(name: "ByCustomer", fields: ["customerID"], queryField: "ordersByCustomer")
{
  id: ID!
  businessID: ID!
  customerID: ID!
  customer: Customer @connection(fields: ["customerID"])
}

The generated models do not include the customerID property and instead nest it under customer.business.id because it maps business to be of type Business.

This is technically not "wrong", but the result still very much includes customerID when queries and now the models don't line up anymore. Reading the result as customer.business.id probably works, but it's not the most efficient.

Amplify CLI Version 4.41.1

To Reproduce Create a schema with a connection field. Run codegen for models. See the property is missing.

Expected behavior Property to still be included.

Screenshots Missing property in the models. image

TS error when using the model image

Desktop (please complete the following information):

  • OS: MacOS 10.15.7
  • Node Version. 12.19.0

TheDutchCoder avatar Jan 13 '21 18:01 TheDutchCoder

@TheDutchCoder could you share the complete schema and the use case you try to achieve to be able to help you better? From this partial information it is harder to see what you try to do, beside that modelgen generated code is not "optimal", but I suspect that maybe the model could be change if we know what are the goals.

attilah avatar Jan 14 '21 17:01 attilah

I can't really give the whole schema, it's quite big, but I'll give you all related things for the one model:

Model

type UserInvite
  @model(subscriptions: null)
  @key(name: "invitationsByBusiness", fields: ["businessID","id"], queryField: "invitationsByBusiness")
  @key(name: "invitationsByEmail", fields: ["email","id"], queryField: "invitationsByEmail")
{
  id: ID!
  businessID: ID!
  email: String!
  role: UserRole!
  business: Business @connection(fields: ["businessID"])
  status: UserInviteStatus!
  createdAt: AWSDateTime!
}

Subscriptions

type Subscription {
	onCreateUserInvite(businessID: ID): UserInvite
		@aws_subscribe(mutations: ["createUserInvite"])
	onUpdateUserInvite(businessID: ID): UserInvite
		@aws_subscribe(mutations: ["updateUserInvite"])
	onDeleteUserInvite(businessID: ID): UserInvite
		@aws_subscribe(mutations: ["deleteUserInvite"])

	onCreateUserInviteEmail(email: String): UserInvite
		@aws_subscribe(mutations: ["createUserInvite"])
	onUpdateUserInviteEmail(email: String): UserInvite
		@aws_subscribe(mutations: ["updateUserInvite"])
	onDeleteUserInviteEmail(email: String): UserInvite
		@aws_subscribe(mutations: ["deleteUserInvite"])
}

Enum

enum UserInviteStatus {
	pending
	accepted
	rejected
}

Query

export const getUserInvite = /* GraphQL */ `
  query GetUserInvite($id: ID!) {
    getUserInvite(id: $id) {
      id
      businessID
      email
      role
      business {
        id
        name
        slug
        owner
        users {
          items {
            id
            businessID
            name
            initials
            email
            role
            active
            business {
              id
              name
              slug
              owner
              createdAt
              updatedAt
            }
            createdAt
            updatedAt
          }
          nextToken
        }
        createdAt
        updatedAt
      }
      status
      createdAt
      updatedAt
    }
  }
`;

Goal: to get businessID into the model, instead of it being "filtered" out. As you can see, the query returns businessID, but the model is missing it, which means that TypeScript thinks it doesn't exist on the returned data.

Is that enough to go on?

TheDutchCoder avatar Jan 14 '21 18:01 TheDutchCoder

We should verify if this is an issue in the v2 transformer version as well.

alharris-at avatar Apr 21 '22 18:04 alharris-at

linking the following issues for visibility: https://github.com/aws-amplify/amplify-codegen/issues/349 https://github.com/aws-amplify/amplify-codegen/issues/148

ykethan avatar Jun 22 '22 16:06 ykethan