Skip to content

feat: add instructions for sharing schema type between apps #7742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,39 @@ frontend:
paths:
- node_modules/**/*
```

<InlineFilter filters={['angular','javascript','nextjs','react','react-native','vue']}>
## Sharing schema type definitions

If you're using Amplify Data, we recommend adding a `paths` entry in the `tsconfig.json` of your frontend app that points to the `amplify/data/resource.ts` file in your backend app to easily access your schema type definitions from your frontend apps.

First, cone your backend repo into the same parent directory as your frontend app, then add the following entry:

```json title="tsconfig.json" showLineNumbers={false}
{
"compilerOptions": {
"paths": {
"@/data-schema": ["../backend-app/amplify/data/resource"]
}
}
}
```

You can then import the `Schema` type from this path in your frontend code to get code completion and strong typing for your API calls:

```ts title="apps/admin-dashboard/page.tsx"
import { generateClient } from "aws-amplify/data";
import type { Schema } from "@/data-schema";

const client = generateClient<Schema>();

const createTodo = async () => {
await client.models.Todo.create({
content: window.prompt("Todo content?"),
isDone: false,
});
}

```

</InlineFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,46 @@ Once the sandbox environment is running, you would also generate the configurati
</InlineFilter>

- To locate the `App ID` for your backend application, navigate to the Amplify console and select your **backend-app**. On the Overview page, the `App ID` is displayed under the project name.

<InlineFilter filters={['angular','javascript','nextjs','react','react-native','vue']}>

```bash title="Terminal" showLineNumbers={false}
npx ampx generate outputs --branch main --app-id BACKEND-APP-ID
```
</InlineFilter>


<InlineFilter filters={['angular','javascript','nextjs','react','react-native','vue']}>

## Sharing schema type definitions

If you're using Amplify Data, we recommend adding a `paths` entry in your `tsconfig.json` file that points to the `amplify/data/resource.ts` file to easily access your schema type definitions from your frontend apps.

```json title="tsconfig.json" showLineNumbers={false}
{
"compilerOptions": {
"paths": {
"@/data-schema": ["./packages/my-shared-backend/amplify/data/resource"]
}
}
}
```

You can then import the `Schema` type from this path in your frontend code to get code completion and strong typing for your API calls:

```ts title="apps/admin-dashboard/page.tsx"
import { generateClient } from "aws-amplify/data";
import type { Schema } from "@/data-schema";

const client = generateClient<Schema>();

const createTodo = async () => {
await client.models.Todo.create({
content: window.prompt("Todo content?"),
isDone: false,
});
}

```

</InlineFilter>