1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Testing AWS AppSync GraphQL APIs with AWS Amplify - Authorization and Custom Queries

    nader dabitnader dabit
    awsAWS
    graphqlGraphQL

    In this video I show you how to test three types of GraphQL APIs: Basic, querying on custom index, and group-based authorization.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Nader Dabit: We'll start by creating a new Amplify project by running amplify init. Next, we'll add authentication by running amplify add auth. Here, we'll choose the default configuration allowing users to sign in with their username. We're adding authentication, so we can test authorization rules against our GraphQL API. We'll create the GraphQL API by running amplify add API. When prompted for a guided schema creation, we'll choose yes, and then single object with fields as the starter project. This will start us off with a basic to-do application with an ID, name and description field within the to- do type. To test the API, we can run amplify mock API. The CLI will run GraphQL code generation, so you could also test the API out from your client application. Once the endpoint is up-and-running, we can now test it out. We'll first create a couple of to-dos by running the create to-do mutation. Next, we'll fetch a list of to-dos by running the list to-dos query. We can also get a single to-do by ID, by running get to-do. For our next API, I'll open the GraphQL recipes blog post and look for the events app. We'll save the schema, and then run amplify mock API. To test this out, we'll create a couple of events by running the createEvent mutation. To query for the event, we'll run the listEvents query. Here, we'll notice that the events come back, but they're not in any particular order. To get the events by date, we can run the eventsByDate query. The next schema that we'll test out is for a conference app. We'll save the schema, and then run amplify mock API. Next, we'll test this out by trying to create a new talk. We'll notice that we get an unauthorized exception because only an admin can create a talk. To simulate an admin, we'll open the auth options, and add us to the admin group. Now, the mutation is successful. To query for all of the talks, we can run the listTalks query. This query returns a list of talks as expected.