<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gaius</title>
    <description>The latest articles on DEV Community by Gaius (@server_not_found).</description>
    <link>https://dev.to/server_not_found</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F472180%2Fe1252241-c4d9-4f7d-9dbb-3311315a26af.png</url>
      <title>DEV Community: Gaius</title>
      <link>https://dev.to/server_not_found</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/server_not_found"/>
    <language>en</language>
    <item>
      <title>Create a serverless GraphQL API with AWS AppSync and MongoDB</title>
      <dc:creator>Gaius</dc:creator>
      <pubDate>Fri, 25 Sep 2020 17:06:31 +0000</pubDate>
      <link>https://dev.to/server_not_found/create-a-serverless-graphql-api-with-aws-appsync-and-mongodb-1mb8</link>
      <guid>https://dev.to/server_not_found/create-a-serverless-graphql-api-with-aws-appsync-and-mongodb-1mb8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This tutorial will give you an introduction to GraphQL and how you can integrate external data sources like MongoDB using AWS AppSync and AWS Lambda, it is easy to follow even if you aren't familiar with AWS&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  What is GraphQL?
&lt;/h1&gt;

&lt;p&gt;GraphQL is a new standard for API, you can do queries at the API level and retrieve only the data you need, which is a perfect fit for mobile applications where bandwidth is limited and you want to retrieve all the data you need (and nothing more) in a single network request.&lt;/p&gt;




&lt;h1&gt;
  
  
  How does GraphQL compares with REST API then?
&lt;/h1&gt;

&lt;p&gt;Using traditional REST APIs, say when we want to get the title, year released and director of a particular movie, we would need to send a HTTP GET request to the /movies endpoint. Here is the response we get from the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"title": "Star Wars: Episode IV - A New Hope",
"year": 1977.0,
"rated": "PG",
"runtime": 121.0,
"countries": ["USA"],
"genres": ["Action", "Adventure", "Fantasy"],
"director": "George Lucas",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Hmm, but we just want the movie's title, year and director, and the server is sending us a bunch of information we don't need. And how about if we want to retrieve the information about the director? We would need to send another GET request to the /director endpoint again...&lt;/p&gt;


&lt;h2&gt;
  
  
  Can we do better?
&lt;/h2&gt;

&lt;p&gt;With GraphQL, we can select what information we need when we make a request to the server (a.k.a. query), so let's say this time we want the title of a movie, its actors and metacritic rating, then we will send a query like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query{
    getMovieByTitle(title: "'Star Wars: Episode IV - A New Hope\"') {"
        title
        actors
        metacritic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And here is the server response:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "data": {
        "Movies": [
            {
                "title": "Star Wars: Episode IV - A New Hope",
                "actors": ["Mark Hamill", "Harrison Ford", "Carrie Fisher", "Peter Cushing"],
                "metacritic": 92
            }
        ]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Much better, isn't it?&lt;/p&gt;


&lt;h1&gt;
  
  
  Creating our first GraphQL API
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Account setups
&lt;/h2&gt;

&lt;p&gt;For this tutorial, we will be using AWS and MongoDB Atlas, and we will need to create free-tier accounts for both of them. The good news is, all services we use in this tutorial are covered by the free-tier, so we don't have to pay for even a penny.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting up our AWS Account
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://portal.aws.amazon.com/billing/signup#/start" rel="noopener noreferrer"&gt;AWS Console&lt;/a&gt;.
(&lt;strong&gt;Note: a credit card is needed for registration, but we won't incur any charge so no worries&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we have our root account set up, but it is generally a bad idea to do stuffs with that, so we will create a new user to work with.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Follow &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; to create a new IAM user in the console&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once you see the AWS Console, you're good to go! &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frohio1ui1zyimk3m9vvp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frohio1ui1zyimk3m9vvp.png" alt="AWS Console"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Setting up our MongoDB Atlas Account
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Register an account at the &lt;a href="https://www.mongodb.com/try" rel="noopener noreferrer"&gt;MongoDB Atlas Website&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Create an organization &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa7dfkyi7k3kwom7i3s91.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Enter a name for the organization, choose &lt;strong&gt;MongoDB Atlas&lt;/strong&gt;, click 'Next' and create the organization&lt;/li&gt;
&lt;li&gt;Click 'New Project'&lt;/li&gt;
&lt;li&gt;Give this project a name, we will use 'graphql-demo' here, click 'Next' and create the project.&lt;/li&gt;
&lt;li&gt;Now we can build our cluster! Click the green 'Create Cluster' button&lt;/li&gt;
&lt;li&gt;Free cluster is fine for us, and for the deployment option we will be using the following settings:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmas0z39ldj1odnwar6l6.png" alt="Alt Text"&gt;
Ideally, we will want our cluster in the same AWS region we will be using (us-west-2 for this case, and if you want to use another region, &lt;a href="https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/" rel="noopener noreferrer"&gt;just make sure AWS AppSync is available in that region&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Hit 'Create Cluster', and our cluster will be ready in few minutes. Meanwhile, we will set up our development environment in AWS&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Setting up our development environment
&lt;/h2&gt;

&lt;p&gt;We will be using &lt;a href="https://aws.amazon.com/cloud9/?nc2=h_ql_prod_dt_c9" rel="noopener noreferrer"&gt;Cloud9&lt;/a&gt; as our development machine, so it doesn't matter whether we are using Mac or Windows.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can also work on your local machine using VSCode or other IDE as you like, but you may have to install some additional tools required for this tutorial, which Cloud9 has them pre-packaged already.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;In the AWS Console, click 'Service' and search for &lt;strong&gt;Cloud9&lt;/strong&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flm1xx155ymg4wj5fp7hh.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let's change our region to &lt;strong&gt;'Oregon'&lt;/strong&gt; (us-west-2)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can find the region selector near your account number on the upper right corner&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click 'Create Environment'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's call our development machine &lt;strong&gt;'AppSync-Cloud9'&lt;/strong&gt; and click 'Next Step'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;Platform&lt;/strong&gt;, select &lt;strong&gt;Ubuntu Server 18.04 LTS&lt;/strong&gt; and click 'Create Environment'&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let's wait for our IDE to load, and once it is ready we can see the welcome screen with a terminal below it. In the terminal, enter the following command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/gai6948/appsync-mongodb-tutorial.git &amp;amp;&amp;amp; cd appsync-mongodb-tutorial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We have just downloaded the code for this tutorial and we will set up our database first for now.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up our MongoDB database
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Create our database and collection
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Go back to our MongoDB Atlas console, select the project we created previously&lt;/li&gt;
&lt;li&gt;On our cluster, select &lt;strong&gt;'Collections'&lt;/strong&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq15gtze1pd4ez9bwy5l4.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;'Add My Own Data'&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Enter &lt;strong&gt;'appsync-db'&lt;/strong&gt; for database name, and &lt;strong&gt;movies&lt;/strong&gt; for collection name, and click 'Create'&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Configuring Database Access
&lt;/h3&gt;

&lt;p&gt;We will need to create a database user for our Lambda function (to be created later) to talk to the database.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Database Access&lt;/strong&gt; under the section of &lt;strong&gt;Security&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click 'Create Database User'&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enter a username and password, and leave other settings as default and create the user&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Normally, we should limit the user access to just our &lt;em&gt;movies&lt;/em&gt; collection, but for simplicity we will leave the settings as default in our tutorial&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will also need to configure the IP whitelist for our Lambda, click &lt;strong&gt;Network Access&lt;/strong&gt; and &lt;strong&gt;Add IP Address&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;strong&gt;'Allow access from anywhere'&lt;/strong&gt; and &lt;strong&gt;Confirm&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Installing MongoDB tools on our Cloud9 Environment
&lt;/h4&gt;

&lt;p&gt;To import data to our MongoDB Atlas Cluster, we will need to install the mongo tools on our Cloud9 Instance first.&lt;/p&gt;

&lt;p&gt;Switch back to the Cloud9 IDE, and execute the following command in the terminal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install gnupg
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E52529D4
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
sudo apt-get update
sudo apt-get install -y mongocli
sudo apt-get install mongo-tools
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After executing the above command, run &lt;code&gt;mongocli --version&lt;/code&gt; to verify the installation&lt;/p&gt;
&lt;h4&gt;
  
  
  Import our dataset
&lt;/h4&gt;

&lt;p&gt;We will use a trimmed down version of the IMDB movie database as our sample dataset, you can inspect the raw data &lt;a href="https://github.com/gai6948/appsync-mongodb-tutorial/blob/master/movieDetails.json" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go back to the &lt;strong&gt;'Cluster'&lt;/strong&gt; Tab&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;'Command Line Tools'&lt;/strong&gt; tab after the database is created&lt;/li&gt;
&lt;li&gt;Look for the section &lt;strong&gt;'Data Import and Export Tools'&lt;/strong&gt;, and copy the &lt;strong&gt;mongoimport&lt;/strong&gt; command&lt;/li&gt;
&lt;li&gt;Now go back to our &lt;strong&gt;Cloud9&lt;/strong&gt; console, and paste the command into the terminal, but let us change some parameters first:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Change &lt;strong&gt;&amp;lt;PASSWORD&amp;gt;&lt;/strong&gt; to the database user password we just created&lt;/li&gt;
&lt;li&gt;Change &lt;strong&gt;&amp;lt;DATABASE&amp;gt;&lt;/strong&gt; to &lt;strong&gt;appsync-db&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change &lt;strong&gt;&amp;lt;COLLECTION&amp;gt;&lt;/strong&gt; to &lt;strong&gt;movies&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change &lt;strong&gt;&amp;lt;FILETYPE&amp;gt;&lt;/strong&gt; to &lt;strong&gt;json&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Change &lt;strong&gt;&amp;lt;FILENAME&amp;gt;&lt;/strong&gt; to &lt;strong&gt;movieDetails.json&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We should see &lt;strong&gt;'imported 100 documents'&lt;/strong&gt; in the command output&lt;/p&gt;
&lt;h2&gt;
  
  
  Create our GraphQL Schema
&lt;/h2&gt;

&lt;p&gt;Before we start working on our code, let us define our GraphQL Schema first, the schema acts as a contract between the client and the server on the data types client can request.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create our AppSync API
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/appsync/?c=m&amp;amp;sec=srv" rel="noopener noreferrer"&gt;AWS AppSync&lt;/a&gt; is a managed GraphQL service that let us define a GraphQL schema and leverage various data sources such as &lt;a href="https://aws.amazon.com/lambda/?nc2=h_ql_prod_cp_lbd" rel="noopener noreferrer"&gt;Lambda&lt;/a&gt;, &lt;a href="https://aws.amazon.com/dynamodb/?nc2=h_ql_prod_db_ddb" rel="noopener noreferrer"&gt;DynamoDB&lt;/a&gt; and much more. It is fully serverless and we don't have to worry about servers at all. The cool thing about AppSync is you can define your schema and let AppSync creates a DynamoDB table and ElasticSearch cluster for you, without you having to set them up manually. But we will be using Lambda as our data source in this tutorial, so we can connect to our MongoDB database. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the &lt;a href="https://us-west-2.console.aws.amazon.com/appsync/home?region=us-west-2#/" rel="noopener noreferrer"&gt;AppSync Console&lt;/a&gt; in a new tab.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;'Create API'&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;'Start from scratch'&lt;/strong&gt; and click the start at the top&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;'Create'&lt;/strong&gt;, you can also give the API a name but that's optional&lt;/li&gt;
&lt;li&gt;Once the API is created, click &lt;strong&gt;'Edit Schema'&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Work on our GraphQL Schema
&lt;/h3&gt;

&lt;p&gt;A GraphQL Schema consists of three types: &lt;em&gt;query&lt;/em&gt;, &lt;em&gt;mutation&lt;/em&gt; and &lt;em&gt;subscription&lt;/em&gt;. &lt;strong&gt;Query&lt;/strong&gt; allow us to specify what data we want, &lt;strong&gt;mutation&lt;/strong&gt; lets us insert or update new data and get back the result as confirmation, while &lt;strong&gt;subscription&lt;/strong&gt; allows us to listen for data updates and be notified when they happen. We will work with queries in this tutorial, and add mutations later. You can learn more about GraphQL Schema &lt;a href="https://graphql.org/learn/schema/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove everything in the schema first, we won't need them&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the following code at the beginning of the schema, they are the root types that we allow, which is query for now.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;schema {
  query: Query
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the type definition for movies below the root definition:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Movie {
  actors: [Actor!]!
  director: String!
  genres: [String]
  id: ID!
  title: String!
  year: Int!
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Here we define the data types for movies, &lt;strong&gt;!&lt;/strong&gt; behind the data type means the field cannot be null, &lt;strong&gt;gnere: [String]&lt;/strong&gt; means the field genres would return an array of genre, and &lt;strong&gt;[Actor]&lt;/strong&gt; means we expect an array of Actor object to be returned, we will define the Actor object in a moment.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the type definition for actors below movies:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Actor {
    featured_in: [Movie!]
    name: String!
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;An actor can appear in multiple movies, so we define a list of &lt;strong&gt;movies&lt;/strong&gt; for the field &lt;strong&gt;featured_in&lt;/strong&gt;.&lt;br&gt;
Now we have our data types defined, we can define the queries and mutations now.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the query definition below:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Query {
    getMoviesByGenre(genre: String!): [Movie]
    getMoviesByTitle(title: String!): [Movie]
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;We will allow clients to query for movies using &lt;strong&gt;genre&lt;/strong&gt; or &lt;strong&gt;title&lt;/strong&gt; as argument, and return a list of matching movies. The completed schema should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;We can also allow more query patterns like by director or year, and you can add them easily after learning how to write resolvers for queries and fields, which we will cover soon.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create our Lambda resolver function
&lt;/h2&gt;

&lt;p&gt;The magic that we can get whatever data we want is made possible by resolvers, they gather the required data and pass them back to the client in the format defined. Resolvers can be defined at &lt;strong&gt;query&lt;/strong&gt; level or &lt;strong&gt;field&lt;/strong&gt; level. For example we can create a resolver for our &lt;code&gt;getMoviesByGenre&lt;/code&gt; query, which can &lt;strong&gt;'resolve'&lt;/strong&gt; all or part the fields defined in the &lt;code&gt;[Movie]&lt;/code&gt; type. We can also set resolvers at field level, like I can define a resolver &lt;code&gt;getYearInMovie&lt;/code&gt; which only &lt;strong&gt;'resolves'&lt;/strong&gt; the field &lt;code&gt;year&lt;/code&gt;. In this tutorial, we will have 2 query level resolvers to handle &lt;code&gt;getMoviesByGenre&lt;/code&gt; and &lt;code&gt;getMoviesByTitle&lt;/code&gt; respectively, and a field level resolver to resolve the &lt;code&gt;[Actor]&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;In AWS AppSync, there are different types of resolvers:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* [AWS Lambda](https://aws.amazon.com/lambda/?nc2=h_ql_prod_fs_lbd), which is &amp;lt;strong&amp;gt;serverless&amp;lt;/strong&amp;gt; function that only run when being invoked

* [DynamoDB](https://aws.amazon.com/dynamodb/?nc2=h_ql_prod_fs_ddb), which is a serverless NoSQL database in AWS

* [Aurora Serverless](https://aws.amazon.com/rds/aurora/?nc2=h_ql_prod_db_aa), which is also a serverless database but it is relational and compatible with MySQL or PostgresQL

* [Amazon ElasticSearch](https://aws.amazon.com/elasticsearch-service/?nc2=h_ql_prod_an_es), which lets you implement text-based searching for your application

* &amp;lt;strong&amp;gt;HTTP Endpoint&amp;lt;/strong&amp;gt;, which lets you call a REST API to other data sources
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We will create a Lambda function now to act as a resolver for our GraphQL Schema, and we will use &lt;a href="https://aws.amazon.com/serverless/sam/" rel="noopener noreferrer"&gt;AWS SAM&lt;/a&gt; for that.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create our Lambda function via SAM template
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Let us go back to our Cloud9 IDE and run &lt;code&gt;sam init&lt;/code&gt; on the terminal&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Choose &lt;strong&gt;AWS Quickstart Template&lt;/strong&gt; for template source, &lt;strong&gt;nodejs12.x&lt;/strong&gt; for runtime, we will name our project &lt;strong&gt;appsync-demo&lt;/strong&gt; and we will choose &lt;strong&gt;Hello World Example&lt;/strong&gt; as a starting point&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once finished, a new directory will be created with the required files like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzp5h1dw7cpjhez9fg9x3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzp5h1dw7cpjhez9fg9x3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
We will define our code in &lt;code&gt;hello-world/app.js&lt;/code&gt;, and &lt;code&gt;template.yaml&lt;/code&gt; is where we specify properties of our Lambda function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Double click the &lt;code&gt;template.yaml&lt;/code&gt; file to edit it, and replace the whole content with the following one:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Here is what we changed: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set the function timeout to &lt;strong&gt;30 seconds&lt;/strong&gt; so that would allow enough time for our lambda to retrieve data from our MongoDB cluster.&lt;/li&gt;
&lt;li&gt;Removed the default API Gateway which is not required in our scenario, our function will be invoked by AWS AppSync.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Added an environment variables &lt;strong&gt;MONGODB_URI&lt;/strong&gt; which will be our connection string to MongoDB Atlas, to get the connection string please follow the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go back to the home page of your MongoDB Atlas Cluster.&lt;/li&gt;
&lt;li&gt;Click the 'Connect' Label
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F79a4wihv04oyw2l28kei.png" alt="Atlas Connect"&gt;
&lt;/li&gt;
&lt;li&gt;Select 'Connect your application'&lt;/li&gt;
&lt;li&gt;Copy the connection string and paste it as the value of &lt;strong&gt;MONGODB_URI&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Remember to change them according to the database and password you defined in MongoDB Atlas (*without enclosing &amp;lt;&amp;gt;&lt;/em&gt;)*&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Press &lt;strong&gt;CMD+S&lt;/strong&gt; (Mac) or &lt;strong&gt;CTL+S&lt;/strong&gt;(Windows) to save the template file&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Work with our Lambda code
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Double click the &lt;code&gt;app.js&lt;/code&gt; file in the &lt;code&gt;hello-world&lt;/code&gt; directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Paste the following code first, we will go through them by section. Also in case you did not name your MongoDB Atlas database as &lt;code&gt;'appsync-db'&lt;/code&gt; and collection as &lt;code&gt;movies&lt;/code&gt;, you have to change them in the code to match your database/collection name.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In our code, we first imported the mongodb driver for connecting to our database, then for our async lambda handler (&lt;strong&gt;Yes it is async so we supply a callback so whenever we finish execution we pass in either error message if any, or the result&lt;/strong&gt;), we initialize connection to our Mongo database using the URI you just set as environment variable in the template.&lt;/p&gt;

&lt;p&gt;This function will serve as multiple resolvers depending on which query pattern our client use (by movie title or genre in our case). The resolver for actor is to resolve nested array of movie objects representing the movies that an actor had featured in. We switch between different functions depending on the field supplied as event when AppSync resolves our GraphQL queries. You can consider event is something like arguments in a function. (&lt;strong&gt;We will set how AppSync resolves fields by passing different values into event later so no worries if it makes no sense to you now&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;For each resolver function, we extract the argument from event and use it as query parameter in our MongoDB queries. Once we get the results from our database we will append each result into an array of &lt;code&gt;Movies&lt;/code&gt; object which is returned to AppSync.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;It is important that we enclose the async database query inside a try-catch block so we can return errors if there is any. &lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save the file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command &lt;strong&gt;one-by-one&lt;/strong&gt; to initialize npm and install the MongoDB driver we need for our code.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/environment/appsync-demo/hello-world
npm init
npm install -y mongodb
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Deploy our code to AWS Lambda
&lt;/h3&gt;

&lt;p&gt;We can now build our function and deploy it to Lambda using &lt;strong&gt;SAM CLI&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Let's go back to where our &lt;code&gt;template.yaml&lt;/code&gt; is defined and build the function code with our dependencies&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ..
sam build
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;(&lt;code&gt;Optional&lt;/code&gt;) We can test our Lambda function locally before pushing to production, using SAM's local testing feature. If you want to test the function, replace the &lt;code&gt;events/event.json&lt;/code&gt; file with the following one:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This event simulates that a client query for movie by title "Wild Wild Test". For our GraphQL API the actual event is created by AppSync once we define how AppSync resolves the fields, we will do that later.&lt;/p&gt;

&lt;p&gt;Now &lt;strong&gt;execute this command&lt;/strong&gt; to test our function using our newly modified event file&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sam local invoke -e events/event.json
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If your function runs correctly it should produce this result:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[{"id":"5f3149e44ea473d321c2a796","title":"Wild Wild West","year":1999,"director":"Barry Sonnenfeld","genres":["Action","Western","Comedy"],"actors":[{"name":"Will Smith"},{"name":"Kevin Kline"},{"name":"Kenneth Branagh"},{"name":"Salma Hayek"}]}]
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If your function goes into error go check if you have provided a correct connection URI to MongoDB.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every time you modify your code remember to save it and run &lt;code&gt;sam build&lt;/code&gt; again before invoking it&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we will create an &lt;a href="https://aws.amazon.com/s3/" rel="noopener noreferrer"&gt;S3&lt;/a&gt; bucket, and package our code to it so SAM can deploy the code for us&lt;/p&gt;

&lt;p&gt;Execute the following command on Cloud9 terminal:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SAM_BUCKET=sam-staging-bucket-$(uuidgen)
aws s3 mb s3://$SAM_BUCKET --region us-west-2
sam package --s3-bucket $SAM_BUCKET --output-template-file output.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally we can deploy our code to Lambda by running &lt;code&gt;sam deploy --template-file output.yaml --stack-name appsync-demo --capabilities CAPABILITY_IAM&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we go to the &lt;a href="https://us-west-2.console.aws.amazon.com/lambda/home?region=us-west-2#/functions" rel="noopener noreferrer"&gt;Lambda Console&lt;/a&gt;, we can see our function is created.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F23ynkpx67guh8pu5dbhb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F23ynkpx67guh8pu5dbhb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Add our Lambda resolver to our AppSync Schema
&lt;/h2&gt;

&lt;p&gt;Now we have our Lambda function ready, it's time for us to tell AppSync how to &lt;strong&gt;interpret GraphQL queries or mutations&lt;/strong&gt; and use our Lambda to resolve the fields for the client. The way it works is by passing different values in the &lt;strong&gt;event&lt;/strong&gt; to Lambda using &lt;a href="https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-overview.html" rel="noopener noreferrer"&gt;VTL Templates&lt;/a&gt;. You can tell the template to query against &lt;em&gt;DynamoDB tables, Aurora cluster, make REST API calls&lt;/em&gt;, etc. &lt;strong&gt;But now we will set the template to transform client query into event for Lambda&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add our Lambda function as data source for AppSync
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go back to the &lt;a href="https://us-west-2.console.aws.amazon.com/appsync/home?region=us-west-2#/apis" rel="noopener noreferrer"&gt;AppSync Console&lt;/a&gt; and find the API you created earlier&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;strong&gt;'Data Sources'&lt;/strong&gt; on the left and click &lt;strong&gt;'Create Data Source'&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;strong&gt;Lambda data source&lt;/strong&gt; like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwku8ixjc4fu9n4hnhng1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwku8ixjc4fu9n4hnhng1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit &lt;strong&gt;'Create'&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Setting up the resolvers
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to &lt;strong&gt;Schema&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should see the &lt;strong&gt;'Query'&lt;/strong&gt; section for &lt;strong&gt;'Resolvers'&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswh487zcd1r8jd1p6bdz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswh487zcd1r8jd1p6bdz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click &lt;strong&gt;'Attach'&lt;/strong&gt; for &lt;code&gt;getMoviesByGenre&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select our only data source and &lt;strong&gt;enable&lt;/strong&gt; both &lt;em&gt;Request and Response mapping template&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace the default &lt;strong&gt;request&lt;/strong&gt; mapping template with this one:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "field": "getMoviesByGenre",
    "arguments": $util.toJson($context.args)
  }
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;On the &lt;code&gt;operation&lt;/code&gt; field we specify &lt;strong&gt;'Invoke'&lt;/strong&gt;, which means calling a &lt;em&gt;Lambda Function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;payload&lt;/code&gt; field lets us specify what values to put in the &lt;strong&gt;event&lt;/strong&gt; for our Lambda function, remember we set a switch on &lt;code&gt;field&lt;/code&gt;? We will tell AppSync to pass in the value &lt;strong&gt;'getMoviesByGenre'&lt;/strong&gt; to our Lambda if the query is taking genre as argument, and for the actual argument we will use the default utility function to transform the format that is readable by our Lambda function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember to click &lt;strong&gt;Save Resolver&lt;/strong&gt; at the top&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Attach a resolver for &lt;code&gt;getMoviesByTitle&lt;/code&gt; this time&lt;/p&gt;

&lt;p&gt;The steps are the same as before, but for the &lt;strong&gt;request&lt;/strong&gt; mapping template we will be using this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
"field": "getMoviesByTitle",
    "arguments": $util.toJson($context.args)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we have two &lt;strong&gt;root-level&lt;/strong&gt; resolvers set up. In the AppSync console we can set resolvers at field-level, but for most of the fields we don't have to do so because &lt;strong&gt;if the parent resolver returns an object with keys matching the child field names, then most GraphQL engines (like AppSync) will use the fields returned from parents unless you explicitly override.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We only have 1 &lt;strong&gt;field-level&lt;/strong&gt; resolver to set, and that is &lt;code&gt;featured_in&lt;/code&gt; for the &lt;code&gt;Actor&lt;/code&gt; object&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Do the same steps as how you set the resolver for the parents, but the &lt;strong&gt;request&lt;/strong&gt; mapping template will be this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
"field": "getMoviesByActor",
    "arguments": $util.toJson($context.source)
  }
}
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Our GraphQL API is ready for now&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Playing around with our GraphQL API
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;We can now go to the &amp;lt;strong&amp;gt;Queries&amp;lt;/strong&amp;gt; tab and play around with our API. Click the &amp;lt;strong&amp;gt;+&amp;lt;/strong&amp;gt; icon near &lt;code&gt;Query&lt;/code&gt; to add a query, and select what fields you want for your query. You can also type your query in the IDE if you prefer it.

&lt;p&gt;The result should be something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk7hg5yv3iikn5tq33i4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk7hg5yv3iikn5tq33i4a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may notice we can go a few layers deep and answer questions like given a movie title, find the movies that its actors have featured in. This is not easy to achieve using traditional REST API. Also we have the freedom to choose how much data we need, no more no less, and this is the power of GraphQL&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  What's next?&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;🎉🎉🎉 &lt;strong&gt;Hurray, we now created our first GraphQL API! &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have only showed you a subset of &lt;strong&gt;AppSync&lt;/strong&gt;'s capabilities, in fact, combining AppSync with &lt;a href="https://aws.amazon.com/amplify/" rel="noopener noreferrer"&gt;AWS Amplify&lt;/a&gt; can give you the ability to create a full-stack web/mobile app in days.&lt;/p&gt;

&lt;p&gt;I suggest you read &lt;a href="https://aws.amazon.com/blogs/mobile/building-scalable-graphql-apis-on-aws-with-cdk-and-aws-appsync/" rel="noopener noreferrer"&gt;Nader Dabit's tutorial&lt;/a&gt; on building a full-stack app with &lt;strong&gt;AWS AppSync and TypeScript&lt;/strong&gt;, and also &lt;a href="https://www.youtube.com/watch?v=w0p7ywfHesw&amp;amp;t=7378s" rel="noopener noreferrer"&gt;Traversy Media's Youtube Tutorial&lt;/a&gt; on building a full-stack app using &lt;strong&gt;AWS Amplify and AWS AppSync.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stay tuned for more content 🔥🔥🔥🔥&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>aws</category>
      <category>serverless</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
