Closed kekami closed 1 month ago
Hey👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂
@kekami Can you share the SQL table definitions for Contract and Organisation, as well as the generated source schema.sql.ts? Connection fields (that is, the fields decorated with hasMany
, hasOne
, or belongsTo
, must always be optional, in order to prevent potential schema validation errors on nested queries.
The reference fields (that is, the fields on the related model that comprise the primary key of the primary model), may be either optional or required.
We have tested relationships with optional required fields in SQL data sources (e.g., see https://github.com/aws-amplify/amplify-category-api/blob/main/packages/amplify-graphql-api-construct-tests/src/__tests__/relationships/references/references-sqlprimary-sqlrelated.test.ts) generated from the CDK construct. It's possible we didn't cover your particular configuration, and it's also possible that there's something lost in translation between the schema builder (the Gen2 typescript definition) and the GraphQL schema it generates, but we'll need more info to know for sure.
Hi @palpatim, I've created a smaller schema that this reproduces the problem.
Here is the table definition used:
CREATE TABLE organisations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owners TEXT[],
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE contracts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
organisation_id UUID,
owners TEXT[],
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (organisation_id) REFERENCES organisations(id)
);
And the generated schema.sql.ts
/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from "@aws-amplify/data-schema";
import { configure } from "@aws-amplify/data-schema/internals";
import { secret } from "@aws-amplify/backend";
export const schema = configure({
database: {
identifier: "IDLkPsFEcNeUVt6GqrbYTxYg",
engine: "postgresql",
connectionUri: secret("SQL_CONNECTION_STRING_REPRO")
}
}).schema({
"contracts": a.model({
id: a.id().required(),
organisation_id: a.id(),
owners: a.string().array(),
created_at: a.datetime(),
updated_at: a.datetime()
}).identifier([
"id"
]),
"organisations": a.model({
id: a.id().required(),
owners: a.string().array(),
created_at: a.datetime(),
updated_at: a.datetime()
}).identifier([
"id"
])
});
And resource.ts
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
import { schema as generatedSqlSchema } from "./schema.sql.js";
const sqlSchema = generatedSqlSchema
.renameModels(() => [
["contracts", "Contract"],
["organisations", "Organisation"],
])
.setRelationships((models) => [
models.Contract.relationships({
organisation: a.belongsTo("Organisation", "organisation_id"),
}),
models.Organisation.relationships({
contracts: a.hasMany("Contract", "organisation_id"),
}),
])
.setAuthorization((models) => [
models.Contract.authorization((allow) => [
allow.ownersDefinedIn("owners"),
allow.groups(["admin"]),
]),
models.Organisation.authorization((allow) => [
allow.ownersDefinedIn("owners"),
allow.groups(["admin"]),
]),
]);
export type Schema = ClientSchema<typeof sqlSchema>;
export const data = defineData({
schema: sqlSchema,
authorizationModes: {
defaultAuthorizationMode: "userPool",
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
Hi @kekami, thanks for the repro case. I've confirmed the issue on my side and am looking at the fix for this.
Fixed in https://github.com/aws-amplify/amplify-category-api/pull/2587, pending next release of this package.
This has been released on constructs:
You should be able to do an npm update
in your project to update to the latest dependencies and consume this fix.
This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.
Environment information
Description
Schema:
On running
createContract
mutation, the following error is received.Presumably due to the following line in the
ContractOrganisationDataResolverFn
where it defaults to""
if the createdContract
has noorganisation_id
(optional relationship)Same error is also received when querying
Contract
with noorganisation_id
, e.g.Ping me if you have any further questions.