Skip to content
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

fix(vue): Update data setup guide for Vue, remove tsx #7727

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
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
169 changes: 166 additions & 3 deletions src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ First, install the Amplify client library to your project:
```bash title="Terminal" showLineNumbers={false}
npm add aws-amplify
```
</InlineFilter>

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

In your app's entry point, typically **main.tsx** for React apps created using Vite, make the following edits:

Expand All @@ -139,6 +142,20 @@ Amplify.configure(outputs);

</InlineFilter>

<InlineFilter filters={["vue"]}>

In your app's entry point, typically **main.ts** for Vue apps created using Vite, make the following edits:
ErikCH marked this conversation as resolved.
Show resolved Hide resolved

```tsx title="src/main.ts"
import { Amplify } from 'aws-amplify';
import outputs from '../amplify_outputs.json';

Amplify.configure(outputs);
```

</InlineFilter>


<InlineFilter filters={["android"]}>

Under Gradle Scripts, open build.gradle (Module :app), add the following lines:
Expand Down Expand Up @@ -356,10 +373,15 @@ Future<void> main() async {
</InlineFilter>
## Write data to your backend

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

Let's first add a button to create a new todo item. To make a "create Todo" API request, generate the data client using `generateClient()` in your frontend code, and then call `.create()` operation for the Todo model. The Data client is a fully typed client that gives you in-IDE code completion. To enable this in-IDE code completion capability, pass in the `Schema` type to the `generateClient` function.

</InlineFilter>


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

```tsx title="src/TodoList.tsx"
import type { Schema } from '../amplify/data/resource'
import { generateClient } from 'aws-amplify/data'
Expand All @@ -380,6 +402,40 @@ export default function TodoList() {
}
```

</InlineFilter>


<InlineFilter filters={["vue"]}>

```html title="src/TodoList.vue"

ErikCH marked this conversation as resolved.
Show resolved Hide resolved
<script setup lang="ts">
import type { Schema } from '../../amplify/data/resource'
import { generateClient } from 'aws-amplify/data'

const client = generateClient<Schema>()

async function createTodo() {
await client.models.Todo.create({
content: window.prompt("Todo content?"),
isDone: false
})
}
</script>

<template>
<div>
<button @click="createTodo">Add new todo</button>
</div>
</template>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

```

</InlineFilter>

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


Run the application in local development mode and check your network tab after creating a todo. You should see a successful request to a `/graphql` endpoint.

<Callout>
Expand Down Expand Up @@ -579,7 +635,7 @@ Creating Todo successful.

Next, list all your todos and then refetch the todos after a todo has been added:

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


```tsx title="src/TodoList.tsx"
Expand Down Expand Up @@ -623,6 +679,56 @@ export default function TodoList() {
}
```

</InlineFilter>

<InlineFilter filters={["vue"]}>

```html title="src/TodoList.vue"

ErikCH marked this conversation as resolved.
Show resolved Hide resolved
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import type { Schema } from '../../amplify/data/resource'
import { generateClient } from 'aws-amplify/data'

const client = generateClient<Schema>()

// create a reactive reference to the array of todos
const todos = ref<Array<Schema['Todo']["type"]>>([]);
ErikCH marked this conversation as resolved.
Show resolved Hide resolved

function fetchTodos() {
const { data: items, errors } = await client.models.Todo.list();
todos.value = items;
}

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

onMounted(() => {
fetchTodos();
});

</script>

<template>
<div>
<button @click="createTodo">Add new todo</button>
<ul>
<li
v-for="todo in todos"
:key="todo.id">
{{ todo.content }}
</li>
</ul>
</div>
</template>

ErikCH marked this conversation as resolved.
Show resolved Hide resolved
```

</InlineFilter>
<InlineFilter filters={["android"]}>

Expand Down Expand Up @@ -834,7 +940,7 @@ class _MyHomePageState extends State<MyHomePage> {

## Subscribe to real-time updates

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

You can also use `observeQuery` to subscribe to a live feed of your backend data. Let's refactor the code to use a real-time observeQuery instead.

Expand Down Expand Up @@ -880,6 +986,63 @@ export default function TodoList() {
}
```

</InlineFilter>

<InlineFilter filters={["vue"]}>
```html title="src/TodoList.vue"

ErikCH marked this conversation as resolved.
Show resolved Hide resolved
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import type { Schema } from '../../amplify/data/resource'
import { generateClient } from 'aws-amplify/data'

const client = generateClient<Schema>()

// create a reactive reference to the array of todos
const todos = ref<Array<Schema['Todo']["type"]>>([]);

function fetchTodos() {
client.models.Todo.observeQuery().subscribe({
next: ({ items, isSynced }) => {
todos.value = items
},
});
}

async function createTodo() {
await client.models.Todo.create({
content: window.prompt("Todo content?"),
isDone: false
})
// no more manual refetchTodos required!
// - fetchTodos()
}

onMounted(() => {
fetchTodos();
});

</script>

<template>
<div>
<button @click="createTodo">Add new todo</button>
<ul>
<li
v-for="todo in todos"
:key="todo.id">
{{ todo.content }}
</li>
</ul>
</div>
</template>

ErikCH marked this conversation as resolved.
Show resolved Hide resolved
```

</InlineFilter>

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

Now try to open your app in two browser windows and see how creating a todo in one window automatically adds the todo in the second window as well.

<Callout>
Expand Down