Skip to content

Commit d3e8494

Browse files
iartemievkatieklein
authored andcommitted
feat(api): add code samples 1/2 (#4881)
* feat(api): add code samples 1/2 * commit to re-trigger checks --------- Co-authored-by: Katie Goines <[email protected]>
1 parent c6ef8ac commit d3e8494

File tree

2 files changed

+250
-1
lines changed

2 files changed

+250
-1
lines changed

src/pages/cli/graphql/authorization-rules.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,23 @@ type Post @model @auth(rules: [
288288
content: String
289289
}
290290
```
291+
```js
292+
import { createPost } from './graphql/mutations';
293+
import { listPosts } from './graphql/queries';
294+
295+
// Creating a post is restricted to Cognito User Pools
296+
const newPostResult = await API.graphql({
297+
query: queries.createPost,
298+
variables: { input: { title: 'Hello World' } },
299+
authMode: 'AMAZON_COGNITO_USER_POOLS',
300+
});
301+
302+
// Listing posts is available to all users (verified by IAM)
303+
const listPostsResult = await API.graphql({
304+
query: queries.listPosts,
305+
authMode: 'AWS_IAM',
306+
});
307+
```
291308

292309
In the example above:
293310
- any user (signed in or not, verified by IAM) is allowed to read all posts

src/pages/cli/graphql/data-modeling.mdx

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,25 @@ query QueryAllTodos {
3636
}
3737
}
3838
```
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+
```
3954

4055
### Configure a primary key
4156

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.
4358

4459
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.
4560
```graphql
@@ -73,6 +88,17 @@ query QueryInventoryByProductAndWarehouse($productID: ID!, $warehouseID: ID!) {
7388
}
7489
}
7590
```
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+
```
76102

77103
### Configure a secondary index
78104

@@ -106,6 +132,18 @@ query QueryCustomersForAccountRepresentative($accountRepresentativeID: ID!) {
106132
}
107133
}
108134
```
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+
```
109147

110148
You can also overwrite the `queryField` or `name` to customize the GraphQL query name, or secondary index name respectively:
111149

@@ -131,6 +169,18 @@ query QueryCustomersForAccountRepresentative($representativeId: ID!) {
131169
}
132170
}
133171
```
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+
```
134184

135185
To optionally configure sort keys, provide the additional fields in the `sortKeyFields` parameter:
136186

@@ -155,6 +205,19 @@ query MyQuery {
155205
}
156206
}
157207
```
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+
```
158221

159222
## Setup relationships between models
160223

@@ -203,6 +266,16 @@ mutation CreateProject {
203266
}
204267
}
205268
```
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+
```
206279

207280
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:
208281

@@ -234,6 +307,16 @@ mutation CreateProject {
234307
}
235308
}
236309
```
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+
```
237320

238321
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).
239322

@@ -272,6 +355,17 @@ mutation CreatePost {
272355
}
273356
}
274357
```
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+
```
275369

276370
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.
277371

@@ -313,6 +407,30 @@ mutation CreatePost {
313407
}
314408
}
315409
```
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+
```
316434

317435
### Belongs To relationship
318436

@@ -354,6 +472,41 @@ mutation CreateProject {
354472
}
355473
}
356474
```
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+
357510
</Block>
358511

359512
<Block name='Bi-directional "has many" relationship'>
@@ -392,6 +545,32 @@ mutation CreatePost {
392545
}
393546
}
394547
```
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+
```
395574
</Block>
396575

397576
</BlockSwitcher>
@@ -463,6 +642,44 @@ mutation CreatePost {
463642
}
464643
}
465644
```
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+
```
466683

467684
## Assign default values for fields
468685

@@ -509,6 +726,21 @@ subscription OnCreateTask {
509726
}
510727
}
511728
```
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+
```
512744

513745
If you want to get all subscription events, don’t pass any `filter` parameters.
514746

0 commit comments

Comments
 (0)