Become a member
to unlock all features

Level Up!

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


    Autogenerate an ID Field in GraphQL with AppSync Resolvers

    nader dabitnader dabit

    GraphQL resolvers are the glue between your GraphQL schema & your data source. In this lesson we’ll take a look at how resolvers work in AWS AppSync. We’ll also update a resolver to autogenerate an ID field so we no longer have to hard code it ourselves.



    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




    Instructor: In our to-do app, we currently have to hard-code and pass in an ID for our mutation to successfully execute. If we take a look at our schema and we look at the createTodo mutation, we'll see that the createTodo input has an ID that is required.

    This means that from our client, if we would like to execute this createTodo mutation, we'll have to pass in the ID ourselves. While this is OK in some cases, we'd like to have the option of auto-generating this ID on the server.

    We can do this by updating the createTodo resolver. Most GraphQL types are backed by a function called a resolver, which declare the relationship between the GraphQL schema type and the data source. AppSync is no different. We have the availability to update and have full control over our resolvers.

    On the right-hand side of our dashboard, under data types, we can scroll down and see where resolvers were already created for us. Let's scroll down, find the createTodo resolver, and open it in our dashboard.

    We have three different areas within our resolver. One defines the data source. One defines the request-mapping template. One defines the response-mapping template. In our case, we'd like to update our request-mapping template in order to auto-generate an ID and send it to our database.

    The field that we'd like to update is the ID field. Right now, we're passing in the context.args.input.ID. Instead of passing in context.args.input.ID, we'll replace this with util.autoID. This will automatically generate a unique ID on our server.

    After that's been updated, we can go ahead and click "Save." Next, we'll go to our schema and remove the ID property of our createTodo input.

    Now that our schema's been updated and saved, let's go to queries and create a new createTodo mutation. Here, we'll call createTodo with an input. We'll only have to pass in now the name and the completed values.

    If the mutation's successful, we'll still return the ID. We execute the mutation. You see that an ID was auto-generated for us, without us having to pass it in manually.

    Next, let's go to data sources and view the to-do table. Here, we can see our created to-do. We have our ID that's been automatically generated for us.

    AppSync resolvers are written in something called VTL, or Velocity Templating Language. To learn more about VTL and mapping templates in general, you can go to the AWS AppSync documentation and click on "Resources."

    Then click on "AWS AppSync Developer Guide." In the left-hand menu, click on "Resolver Mapping Template Reference."