GraphQL Transform `keyField` in `@connection` directive is broken.
Describe the bug
When using the keyField in @connection, the relationship does not work and the query returns an error.
I have tested this locally and in the service..
Steps to reproduce the behavior:
- Create an AppSync API from the Amplify CLI
- Add the following shema:
type Talk @model {
id: ID!
name: String!
description: String!
comments: [Comment] @connection(name: "TalkComments")
}
type Comment @model {
id: ID!
message: String
createdAt: String
talk: Talk @connection(sortField: "createdAt", name: "TalkComments", keyField: "talkId")
}
Run the following operations in this order:
mutation createTalk {
createTalk(input: {
name: "Talk 1"
description: "Cool talk"
}) {
id name description
}
}
mutation createComment {
createComment(input: {
talkId: "<TALK_ID>
message: "Great talk"
}) {
id message
}
}
query listTalks {
listTalks {
items {
id
name
description
comments {
items {
message
}
}
}
}
}
Expected behavior The comments array is populated with the comment.
Screenshots

Desktop (please complete the following information):
- OS: MacOS
Additional context CLI Version: 1.12.0 & the latest from Yathi's release
You are using a Many to One connection, and the keyField on the Many side directive is used to find the hash key on the One side. In this case your many side @connection does not include keyField, so the default ID is used, commentTalkId, not talkId. However, when the cli sets up the One side, it uses the specified keyField to set the GSI hash key. Result is wrong key is used in lookup. Short term fix is to supply the keyField on the Many side as well, matching the One side keyField.
At a code level, graphql-connection-transformer/src/ModelConnectionTransformer.ts 'field' method part 2 is the problem. Specifically, validateKeyField does not throw an error when the keyField passed in is undefined. If passing in undefined threw an error, this issue would not be deployed and a user could correct the schema.
From a code architecture standpoint, tsconfig.json does not have strict null checks enabled. The validateKeyField definition is not correct if you turn on strict null checks, and I can't see a good reason why this is not enabled.
Documentation on this is not clear, and DX does not seem good. The need to match keyFields on both connections could be eliminated by replicating the use of 'associatedSortFieldName' for the keyField name as well.
@dabit3 The Talk.comments field doesn't look at the keyField argument inside the @connection on the Comment.talk field. You can fix the problem by changing your schema to:
type Talk @model {
id: ID!
name: String!
description: String!
comments: [Comment] @connection(sortField: "createdAt", name: "TalkComments", keyField: "talkId")
}
type Comment @model {
id: ID!
message: String
createdAt: String
talk: Talk @connection(sortField: "createdAt", name: "TalkComments", keyField: "talkId")
}
We will add some smarts that will infer these when possible so you don't need to duplicate the arguments. We also have some new enhancements to the @connection directive in PR here if you want to offer some feedback: https://github.com/aws-amplify/amplify-cli/pull/2118
@mikeparisstuff currently having trouble adding the sortField to my schema. It's quite similar to the fix you posted but I get this error.
Attempting to edit the global secondary index gsi-UserOrder on the OrderTable table in the Order stack. An error occured during the push operation: Attempting to edit the global secondary index gsi-UserOrder on the OrderTable table in the Order stack.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.