In this video I show you how to test three types of GraphQL APIs: Basic, querying on custom index, and group-based authorization.
Nader Dabit: [00:00] We'll start by creating a new [00:00] Amplify project by running [00:02] amplify init. Next, we'll add [00:27] authentication by running [00:29] amplify add auth. Here, we'll [00:31] choose the default configuration [00:32] allowing users to sign in with [00:33] their username. We're adding [00:35] authentication, so we can test [00:36] authorization rules against our [00:39] GraphQL API. We'll create the [00:41] GraphQL API by running amplify [00:43] add API. [00:49] When prompted for a [00:50] guided schema creation, we'll [00:52] choose yes, and then single [00:53] object with fields as the [00:55] starter project. This will start [00:59] us off with a basic to-do [01:01] application with an ID, name and [01:03] description field within the to- [01:05] do type. [01:09] To test the API, we [01:11] can run amplify mock API. The [01:13] CLI will run GraphQL code [01:15] generation, so you could also [01:16] test the API out from your [01:18] client application. Once the [01:20] endpoint is up-and-running, we [01:21] can now test it out. [01:25] We'll [01:26] first create a couple of to-dos [01:27] by running the create to-do [01:28] mutation. Next, we'll fetch a [01:46] list of to-dos by running the [01:48] list to-dos query. We can also [01:58] get a single to-do by ID, by [02:00] running get to-do. For our next [02:13] API, I'll open the GraphQL [02:14] recipes blog post and look for [02:16] the events app. [02:35] We'll save the [02:36] schema, and then run amplify [02:38] mock API. To test this out, [02:43] we'll create a couple of events [02:44] by running the createEvent [02:46] mutation. To query for the event, [03:15] we'll run the listEvents query. [03:23] Here, we'll notice that the [03:24] events come back, but they're [03:25] not in any particular order. To [03:31] get the events by date, we can [03:32] run the eventsByDate query. [03:49] The [03:49] next schema that we'll test out [03:51] is for a conference app. We'll [04:20] save the schema, and then run [04:22] amplify mock API. Next, we'll [04:28] test this out by trying to [04:30] create a new talk. We'll notice [04:46] that we get an unauthorized [04:48] exception because only an admin [04:50] can create a talk. To simulate [04:54] an admin, we'll open the auth [04:55] options, and add us to the admin [04:57] group. [05:03] Now, the mutation is [05:04] successful. To query for all of [05:12] the talks, we can run the [05:14] listTalks query. This query [05:23] returns a list of talks as [05:25] expected.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!