- Newest
- Most votes
- Most comments
When organizing a large schema file in Amplify Gen 2, there are several approaches you can take to make it more maintainable.
For handling multiple authorization rules across different services and models, you can:
-
Split your schema into multiple files: Create separate files for different logical groups of models and import them into your main schema file. This helps with organization and readability.
-
Create reusable authorization functions: Define common authorization patterns as variables or functions that you can reuse across your schema:
// Define reusable auth patterns const publicAndAuthenticatedAccess = (allow) => [ allow.publicApiKey(), allow.authenticated() ]; const ownerOnlyAccess = (allow) => [ allow.owner() ]; // Then use them in your schema const schema = a.schema({ schema_1: a .model({ // fields... }) .authorization(publicAndAuthenticatedAccess), schema_2: a .model({ // fields... }) .authorization(ownerOnlyAccess), someservice: a .mutation() .arguments({ // fields }) .returns(a.ref("Group")) .handler(a.handler.function(someservice)) .authorization(publicAndAuthenticatedAccess) });
- Use the combine method: Amplify provides an
a.combine()
method that allows you to merge multiple schemas. This lets you define schemas in separate files and combine them:
// In schema1.ts export const schema1 = a.schema({ model1: a.model({/*...*/}).authorization((allow) => [allow.publicApiKey()]) }); // In schema2.ts export const schema2 = a.schema({ model2: a.model({/*...*/}).authorization((allow) => [allow.authenticated()]) }); // In your main schema file import { schema1 } from './schema1'; import { schema2 } from './schema2'; const combinedSchema = a.combine([schema1, schema2]); export const data = defineData({ schema: combinedSchema });
Remember that Amplify will use the most specific authorization rule available, and if multiple rules are present, they will be logically OR'ed. This means you can define global authorization rules and then override them with more specific rules at the model or field level as needed.
Sources
Building RAG-based applications with AWS Amplify AI Kit and Neon Postgres | Front-End Web & Mobile
Customize your auth rules - AWS Amplify Gen 2 Documentation
Relevant content
- asked 8 months ago
- asked a year ago
- asked 6 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 3 years ago