I'm getting the following error when I attempt to compile my schema:
InvalidDirectiveError: Connection is to a single object but the keyName <key-name> was provided which does not reference the default table.
Is it possible to define a one-to-one relationship with a keyName argument?
Which Category is your question related to?
GraphQL compile
Amplify CLI Version
4.17.2
Can you please provide your schema?
A 1:1 relationship would be covered by "Has One" in the following @connection documentation: https://aws-amplify.github.io/docs/cli-toolchain/graphql#connection
@undefobj I'm confused about how to set a one to one using @key...
For this use case, a customer can only be referred by one other customer, but can refer many customers.
Here's the stripped down schema with relative parts:
type Customer
@model
@key(fields: ["customerId"])
{
customerId: ID!
firstName: String!
lastName: String!
referrals: [Referral]
@connection(keyName: "referralsByCustomer", fields: ["customerId"])
referredBy: [Referral]
@connection(keyName: "referralsByReferralCustomer", fields: ["customerId"])
owner: String
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
type Referral
@model
@key(fields: ["referralId"])
@key(
name: "referralsByCustomer"
fields: ["customerId"]
queryField: "referralsByCustomer"
)
@key(
name: "referralsByReferralCustomer"
fields: ["referralCustomerId"]
queryField: "referralsByReferralCustomer"
) {
referralId: ID!
firstName: String!
lastName: String!
email: String!
customerId: ID!
referralCustomerId: ID
owner: String
createdAt: AWSDateTime
}
if i change:
referredBy: [Referral]
@connection(keyName: "referralsByReferralCustomer", fields: ["customerId"])
to:
referredBy: Referral
@connection(keyName: "referralsByReferralCustomer", fields: ["customerId"])
then gql-compile reports error above.
It just occurred to me that i probably need to store the referral id in the customer record as well and then lookup on referralId. Is that the correct approach?
When I added the id to the customer type with a connection this issue came up: https://github.com/aws-amplify/amplify-cli/issues/3275
Hi @kldeb yes you are correct, you'll need to store the referralId with the customer. Unfortunately there's no great workaround to #3275 at this time. You might be able to get the behavior you want by putting a placeholder value for referralId in the customer record if there is no referralId for the customer:
type Customer @model {
id: ID!
referredBy: Referral @connection(fields: ["referralId"])
referralId: ID!
name: String!
}
type Referral @model {
id: ID!
name: String!
}
When creating a customer without a referral, you can put a placeholder value in the referralId which means that the referredBy field is going to come back null in queries.
I'm going to close this issue and you can track the progress of optional connections in #3275
Most helpful comment
Hi @kldeb yes you are correct, you'll need to store the referralId with the customer. Unfortunately there's no great workaround to #3275 at this time. You might be able to get the behavior you want by putting a placeholder value for referralId in the customer record if there is no referralId for the customer:
When creating a customer without a referral, you can put a placeholder value in the referralId which means that the referredBy field is going to come back null in queries.
I'm going to close this issue and you can track the progress of optional connections in #3275