- Newest
- Most votes
- Most comments
To use ClientExtensions accessors and methods in an Amplify Gen2 app with separate backend and frontend, and to supply an accurate Schema type parameter when calling generateClient, you can follow these steps:
-
Copy the schema definition from your backend's
amplify/data/resource.tsfile to a new file in your frontend project, such asschema.ts. -
Add a path alias in your frontend's
tsconfig.jsonthat points to this new schema file:{ "compilerOptions": { "paths": { "@/data-schema": ["./path/to/your/schema.ts"] } } } -
In your frontend code, import the schema and use it as the type parameter when calling
generateClient:import { generateClient } from 'aws-amplify/api'; import type { Schema } from '@/data-schema'; const client = generateClient<Schema>();
This approach allows you to use ClientExtensions methods with proper typing:
const items = await client.models.SomeModel.list({ selectionSet: ['id', 'name', 'description'] });
To keep your frontend schema in sync with your backend, consider setting up a script that copies the schema file from your backend to your frontend during deployment. Remember to update your frontend's schema file whenever you make changes to your backend schema to ensure type safety and consistency between your frontend and backend.
Sources
Separate frontend and backend teams - AWS Amplify Gen 2 Documentation
Fullstack TypeScript: Reintroducing AWS Amplify | Front-End Web & Mobile
answered 2 years ago
This is a good question, and the AI answer is helpful to understand how to solve the problem. It would be great if the AWS Amplify team could document the preferred way to connect to the schema when implementing multi-repo projects in Amplify--over here in the Amplify Gen-2 Documentation Full Stack Workflows >> Separate Frontend and Backend Teams
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 5 years ago
- AWS OFFICIALUpdated 5 years ago
