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

Discussion: Allow passing in a schema configuration for string based schemas

Open kekami opened this issue 7 months ago • 1 comments

I would like to discuss how we could enable users to pass in a SQL based SDL into defineData and what the API surface should look like. See github issue for further details.

Problem

aws-amplify/amplify-category-api#2685

Changes

Added a SchemaConfiguration property to DataProps to override the default DDB strategy used for string based schemas.

import { defineData, secret } from "@aws-amplify/backend";
import { fileURLToPath } from "url";
import path from "path";
import { AmplifyDataDefinition } from "@aws-amplify/data-construct";
import { localizedTitleHandler } from "./functions/resource";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const gqlSchema = AmplifyDataDefinition.fromFiles(
  path.join(__dirname, "schema.sql.graphql")
).schema;

const schema = /* GraphQL */ `
  ${gqlSchema}
`;

export const data = defineData({
  schema,
  schemaConfiguration: {
    database: {
      engine: "postgresql",
      connectionUri: secret("SQL_CONNECTION_STRING"),
    },
  },
  authorizationModes: {
    defaultAuthorizationMode: "iam",
  },
  functions: {
    localizedTitleHandler,
  },
});

Example Schema:

type Post
  @model(queries: { get: "getPost" }, mutations: null, subscriptions: null)
  @refersTo(name: "posts")
  @auth(rules: [{ allow: owner }]) {
  id: String! @primaryKey
  title: String!
  content: String!
  tags: [Tag] @hasMany(references: ["postId"])
  publishDate: AWSDate
    @refersTo(name: "published_date")
    @auth(rules: [{ allow: owner }])
  localizedTitle(locale: String): String
    @function(name: "localizedTitleHandler")
}

type Tag
  @model(queries: { get: "getTag" }, mutations: null, subscriptions: null)
  @refersTo(name: "tags")
  @auth(rules: [{ allow: owner }]) {
  id: String! @primaryKey
  name: String!
  postId: String @refersTo(name: "post_id")
  post: Post @belongsTo(references: ["postId"])
}

Validation

N/A

Checklist

  • [ ] If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • [ ] If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • [ ] If this PR requires a docs update, I have linked to that docs PR above.
  • [ ] If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

kekami avatar Jul 02 '24 13:07 kekami