@@ -36,10 +36,25 @@ query QueryAllTodos {
36
36
}
37
37
}
38
38
```
39
+ ``` js
40
+ import { Amplify , API , graphqlOperation } from ' aws-amplify' ;
41
+ import awsconfig from ' ./aws-exports' ;
42
+ import { listTodos } from ' ./graphql/queries' ;
43
+
44
+ Amplify .configure (awsconfig);
45
+
46
+ try {
47
+ const result = await API .graphql (graphqlOperation (listTodos));
48
+ const todos = result .data .listTodos ;
49
+ } catch (res) {
50
+ const { errors } = res;
51
+ console .error (errors);
52
+ }
53
+ ```
39
54
40
55
### Configure a primary key
41
56
42
- Every GraphQL type with the ` @model ` directive will automatically have an ` id ` field set as the primary key. You can override this behavior by marking another required field with the ` @primaryKey ` directive.
57
+ Every GraphQL type with the ` @model ` directive will automatically have an ` id ` field set as the primary key. You can override this behavior by marking another required field with the ` @primaryKey ` directive.
43
58
44
59
In the example below, ` todoId ` is the database's primary key and an ` id ` field will no longer be created automatically anymore by the ` @model ` directive.
45
60
``` graphql
@@ -73,6 +88,17 @@ query QueryInventoryByProductAndWarehouse($productID: ID!, $warehouseID: ID!) {
73
88
}
74
89
}
75
90
```
91
+ ``` js
92
+ import { getInventory } from ' ./graphql/queries' ;
93
+
94
+ const params = {
95
+ productID: ' product-id' ,
96
+ warehouseID: ' warehouse-id' ,
97
+ };
98
+
99
+ const result = await API .graphql (graphqlOperation (getInventory, params));
100
+ const inventory = result .data .getInventory ;
101
+ ```
76
102
77
103
### Configure a secondary index
78
104
@@ -106,6 +132,18 @@ query QueryCustomersForAccountRepresentative($accountRepresentativeID: ID!) {
106
132
}
107
133
}
108
134
```
135
+ ``` js
136
+ import { customersByAccountRepresentativeID } from ' ./graphql/queries' ;
137
+
138
+ const params = {
139
+ accountRepresentativeID: ' account-rep-id' ,
140
+ };
141
+
142
+ const result = await API .graphql (
143
+ graphqlOperation (customersByAccountRepresentativeID, params)
144
+ );
145
+ const customers = result .data .customersByAccountRepresentativeID ;
146
+ ```
109
147
110
148
You can also overwrite the ` queryField ` or ` name ` to customize the GraphQL query name, or secondary index name respectively:
111
149
@@ -131,6 +169,18 @@ query QueryCustomersForAccountRepresentative($representativeId: ID!) {
131
169
}
132
170
}
133
171
```
172
+ ``` js
173
+ import { customerByRepresentative } from ' ./graphql/queries' ;
174
+
175
+ const params = {
176
+ accountRepresentativeID: ' account-rep-id' ,
177
+ };
178
+
179
+ const result = await API .graphql (
180
+ graphqlOperation (customerByRepresentative, params)
181
+ );
182
+ const customer = result .data .customerByRepresentative ;
183
+ ```
134
184
135
185
To optionally configure sort keys, provide the additional fields in the ` sortKeyFields ` parameter:
136
186
@@ -155,6 +205,19 @@ query MyQuery {
155
205
}
156
206
}
157
207
```
208
+ ```js
209
+ import { customerByNameAndPhone } from './graphql /queries ';
210
+
211
+ const params = {
212
+ phoneNumber : { beginsWith : '+1' },
213
+ name : 'Rene ',
214
+ };
215
+
216
+ const result = await API .graphql (
217
+ graphqlOperation (customerByNameAndPhone , params)
218
+ );
219
+ const customer = result .data .customerByNameAndPhone ;
220
+ ```
158
221
159
222
## Setup relationships between models
160
223
@@ -203,6 +266,16 @@ mutation CreateProject {
203
266
}
204
267
}
205
268
```
269
+ ``` js
270
+ import { createProject } from ' ./graphql/mutations' ;
271
+
272
+ const params = {
273
+ input: { projectTeamId: ' team-id' , name: ' Some Name' },
274
+ };
275
+
276
+ const result = await API .graphql (graphqlOperation (createProject, params));
277
+ const project = result .data .createProject ;
278
+ ```
206
279
207
280
To customize the field to be used for storing the relationship information, set the ` fields ` array argument and matching it to a field on the type:
208
281
@@ -234,6 +307,16 @@ mutation CreateProject {
234
307
}
235
308
}
236
309
```
310
+ ``` js
311
+ import { createProject } from ' ./graphql/mutations' ;
312
+
313
+ const params = {
314
+ input: { teamID: ' team-id' , name: ' New Project' },
315
+ };
316
+
317
+ const result = await API .graphql (graphqlOperation (createProject, params));
318
+ const project = result .data .createProject ;
319
+ ```
237
320
238
321
A ` @hasOne ` relationship always uses a reference to the primary key of the related model, by default ` id ` unless overridden with the [ ` @primaryKey ` directive] ( #configure-a-primary-key ) .
239
322
@@ -272,6 +355,17 @@ mutation CreatePost {
272
355
}
273
356
}
274
357
```
358
+ ``` js
359
+ import { createPost } from ' ./graphql/mutations' ;
360
+
361
+ const params = {
362
+ input: { title: ' Hello World!!' },
363
+ };
364
+
365
+ const result = await API .graphql (graphqlOperation (createPost, params));
366
+ const post = result .data .createPost ;
367
+ const comments = post .comments .items ;
368
+ ```
275
369
276
370
Under the hood, ` @hasMany ` configures a default secondary index on the related table to enable you to query the related model from the source model.
277
371
@@ -313,6 +407,30 @@ mutation CreatePost {
313
407
}
314
408
}
315
409
```
410
+ ``` js
411
+ import { createPost , createComment } from ' ./graphql/mutations' ;
412
+ import { getPost } from ' ./graphql/mutations' ;
413
+
414
+ // create post
415
+ const postParams = {
416
+ input: { title: ' Hello World!!' },
417
+ };
418
+
419
+ const result = await API .graphql (graphqlOperation (createPost, postParams));
420
+ const post = result .data .createPost ;
421
+
422
+ // create comment
423
+ const commentParams = {
424
+ input: { content: ' Hi!' , postID: post .id },
425
+ };
426
+
427
+ await API .graphql (graphqlOperation (createComment, commentParams));
428
+
429
+ // get post
430
+ const result = await API .graphql (graphqlOperation (getPost, { id: post .id }));
431
+ const postWithComments = result .data .createPost ;
432
+ const postComments = postWithComments .comments .items ; // access comments from post
433
+ ```
316
434
317
435
### Belongs To relationship
318
436
@@ -354,6 +472,41 @@ mutation CreateProject {
354
472
}
355
473
}
356
474
```
475
+ ``` js
476
+ import { createProject , createTeam , updateTeam } from ' ./graphql/mutations' ;
477
+
478
+ // create team
479
+ const teamParams = {
480
+ input: { name: ' New Team' },
481
+ };
482
+
483
+ const result = await API .graphql (graphqlOperation (createTeam, teamParams));
484
+ const team = result .data .createTeam ;
485
+
486
+ // create project
487
+ const projectParams = {
488
+ input: { name: ' New Project' , projectTeamId: team .id },
489
+ };
490
+
491
+ const result = await API .graphql (
492
+ graphqlOperation (createProject, projectParams)
493
+ );
494
+ const project = result .data .createProject ;
495
+ const projectTeam = project .team ; // access team from project
496
+
497
+ // update team
498
+ const updateParams = {
499
+ input: { id: team .id , teamProjectId: project .id },
500
+ };
501
+
502
+ const updateTeamResult = await API .graphql (
503
+ graphqlOperation (updateTeam, updateParams)
504
+ );
505
+
506
+ const updatedTeam = updateTeamResult .data .updateTeam ;
507
+ const teamProject = updatedTeam .project ; // access project from team
508
+ ```
509
+
357
510
</Block >
358
511
359
512
<Block name = ' Bi-directional "has many" relationship' >
@@ -392,6 +545,32 @@ mutation CreatePost {
392
545
}
393
546
}
394
547
```
548
+ ``` js
549
+ import { createPost , createComment } from ' ./graphql/mutations' ;
550
+ import { getPost } from ' ./graphql/mutations' ;
551
+
552
+ // create post
553
+ const postParams = {
554
+ input: { title: ' Hello World!!' },
555
+ };
556
+
557
+ const result = await API .graphql (graphqlOperation (createPost, postParams));
558
+ const post = result .data .createPost ;
559
+
560
+ // create comment
561
+ const commentParams = {
562
+ input: { content: ' Hi!' , postID: post .id },
563
+ };
564
+
565
+ await API .graphql (graphqlOperation (createComment, commentParams));
566
+
567
+ // get post
568
+ const result = await API .graphql (graphqlOperation (getPost, { id: post .id }));
569
+ const postWithComments = result .data .createPost ;
570
+ const postComments = postWithComments .comments .items ; // access comments from post
571
+
572
+ const commentPost = postComments[0 ].post ; // access post from comment;
573
+ ```
395
574
</Block >
396
575
397
576
</BlockSwitcher >
@@ -463,6 +642,44 @@ mutation CreatePost {
463
642
}
464
643
}
465
644
```
645
+ ``` js
646
+ import { createPost , createTag , createPostTags } from ' ./graphql/mutations' ;
647
+ import { listPosts } from ' ./graphql/queries' ;
648
+
649
+ // create post
650
+ const postParams = {
651
+ input: { title: ' Hello World' },
652
+ };
653
+
654
+ const result = await API .graphql (graphqlOperation (createPost, postParams));
655
+ const post = result .data .createPost ;
656
+
657
+ // create tag
658
+ const tagParams = {
659
+ input: {
660
+ label: ' My Tag' ,
661
+ },
662
+ };
663
+
664
+ const tagResult = await API .graphql (graphqlOperation (createTag, tagParams));
665
+ const tag = tagResult .data .createTag ;
666
+
667
+ // connect post and tag
668
+ const postTagParams = {
669
+ input: {
670
+ postId: post .id ,
671
+ tagId: tag .id ,
672
+ },
673
+ };
674
+
675
+ await API .graphql (graphqlOperation (createPostTags, postTagParams));
676
+
677
+ // get posts
678
+ const listPostsResult = await API .graphql (graphqlOperation (listPosts));
679
+ const posts = listPostsResult .data .listPosts ;
680
+
681
+ const postTags = posts[0 ].tags ; // access tags from post
682
+ ```
466
683
467
684
## Assign default values for fields
468
685
@@ -509,6 +726,21 @@ subscription OnCreateTask {
509
726
}
510
727
}
511
728
```
729
+ ``` js
730
+ import { onCreateTask } from ' ./graphql/subscriptions' ;
731
+
732
+ const filter = {
733
+ and: [
734
+ { type: { eq: " Security" } }
735
+ { priority: { gt: 5 } }
736
+ ]
737
+ };
738
+
739
+ const subscription = API .graphql (graphqlOperation (onCreateTask, filter)).subscribe ({
740
+ next : ({ provider, value }) => console .log ({ provider, value }),
741
+ error : (error ) => console .warn (error)
742
+ });
743
+ ```
512
744
513
745
If you want to get all subscription events, don’t pass any ` filter ` parameters.
514
746
0 commit comments