Skip to content

Commit 8122836

Browse files
add angular and vue examples
1 parent e7259e6 commit 8122836

File tree

1 file changed

+104
-1
lines changed
  • src/pages/[platform]/build-a-backend/data/query-data

1 file changed

+104
-1
lines changed

src/pages/[platform]/build-a-backend/data/query-data/index.mdx

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ const {
147147
});
148148
```
149149

150+
<InlineFilter filters={["react", "javascript", "nextjs", "react-native"]}>
151+
150152
If you're building a React application, you can use the `usePagination` hook in Amplify UI to help with managing the pagination user experience.
151153

152154
```js
@@ -187,6 +189,8 @@ export const PaginationHasMorePagesExample = () => {
187189
};
188190
```
189191

192+
</InlineFilter>
193+
190194
<Callout>
191195

192196
**Limitations:**
@@ -214,7 +218,11 @@ const { data: blogWithSubsetOfData, errors } = await client.models.Blog.get(
214218

215219
## TypeScript type helpers for Amplify Data
216220

217-
When using TypeScript, you frequently need to specify data model types for type generics. For instance, with React's `useState`, you provide a type in TypeScript to ensure type-safety in your component code using the state. Use the `Schema["MODEL_NAME"]["type"]` pattern to get TypeScript types for the shapes of data models returned from the backend API. This allows you to get consumable TypeScript types for the shapes of the data model return values coming from the backend API.
221+
When using TypeScript, you frequently need to specify data model types for type generics.
222+
223+
<InlineFilter filters={["react", "javascript", "nextjs", "react-native"]}>
224+
225+
For instance, with React's `useState`, you provide a type in TypeScript to ensure type-safety in your component code using the state. Use the `Schema["MODEL_NAME"]["type"]` pattern to get TypeScript types for the shapes of data models returned from the backend API. This allows you to get consumable TypeScript types for the shapes of the data model return values coming from the backend API.
218226

219227
```ts
220228
import { type Schema } from '@/amplify/data/resource';
@@ -224,8 +232,21 @@ type Post = Schema['Post']['type'];
224232
const [posts, setPosts] = useState<Post[]>([]);
225233
```
226234

235+
</InlineFilter>
236+
237+
<InlineFilter filters={["angular", "vue"]}>
238+
239+
```ts
240+
import { type Schema } from '@/amplify/data/resource';
241+
242+
type Post = Schema['Post']['type'];
243+
```
244+
245+
</InlineFilter>
246+
227247
You can combine the `Schema["MODEL_NAME"]["type"]` type with the `SelectionSet` helper type to describe the return type of API requests using the `selectionSet` parameter:
228248

249+
<InlineFilter filters={["react", "javascript", "nextjs", "react-native"]}>
229250
```ts
230251
import type { SelectionSet } from 'aws-amplify/data';
231252
import type { Schema } from '../amplify/data/resource';
@@ -245,6 +266,88 @@ const fetchPosts = async () => {
245266
}
246267
```
247268

269+
</InlineFilter>
270+
271+
<InlineFilter filters={['vue']}>
272+
```ts
273+
<script setup lang="ts">
274+
import { ref, onMounted } from 'vue';
275+
import { generateClient, type SelectionSet } from 'aws-amplify/data';
276+
import type { Schema } from '../../../amplify/data/resource';
277+
278+
const client = generateClient<Schema>();
279+
280+
const selectionSet = ['content', 'blog.author.*', 'comments.*'] as const;
281+
282+
type PostWithComments = SelectionSet<
283+
Schema['Post']['type'],
284+
typeof selectionSet
285+
>;
286+
287+
const posts = ref<PostWithComments[]>([]);
288+
289+
const fetchPosts = async (): Promise<void> => {
290+
const { data: postsWithComments } = await client.models.Post.list({
291+
selectionSet,
292+
});
293+
posts.value = postsWithComments;
294+
};
295+
296+
onMounted(() => {
297+
fetchPosts();
298+
});
299+
</script>
300+
301+
<template>
302+
<div>
303+
<!-- Add your template here to display posts -->
304+
</div>
305+
</template>
306+
```
307+
</InlineFilter>
308+
309+
<InlineFilter filters={["angular"]}>
310+
```ts
311+
import { Component, OnInit } from '@angular/core';
312+
import { generateClient, type SelectionSet } from 'aws-amplify/data';
313+
import type { Schema } from '../../../amplify/data/resource';
314+
import { CommonModule } from '@angular/common';
315+
316+
const client = generateClient<Schema>();
317+
318+
const selectionSet = ['content', 'blog.author.*', 'comments.*'] as const;
319+
320+
type PostWithComments = SelectionSet<
321+
Schema['Post']['type'],
322+
typeof selectionSet
323+
>;
324+
325+
@Component({
326+
selector: 'app-todos',
327+
standalone: true,
328+
imports: [CommonModule],
329+
templateUrl: './todos.component.html',
330+
styleUrls: ['./todos.component.css'],
331+
})
332+
export class TodosComponent implements OnInit {
333+
posts: PostWithComments[] = [];
334+
335+
constructor() {}
336+
337+
ngOnInit(): void {
338+
this.fetchPosts();
339+
}
340+
341+
async fetchPosts(): Promise<void> {
342+
const { data: postsWithComments } = await client.models.Post.list({
343+
selectionSet,
344+
});
345+
this.posts = postsWithComments;
346+
}
347+
}
348+
```
349+
</InlineFilter>
350+
248351
## Cancel read requests
249352

250353
You can cancel any query API request by calling `.cancel` on the query request promise that's returned by `.list(...)` or `.get(...)`.

0 commit comments

Comments
 (0)