Serverless GraphQL on AWS - Mapping GraphQL Operations to a Lambda Function

nader dabit
Instructornader dabit
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this video you'll learn how to stand up a fully serverlesss GraphQL API on AWS from scratch using Lambda and AppSync. You'll start by creating a new Lambda function and then using the function in a GraphQL API as a data source. You'll create a schema and then map the GraphQL query in your schema to the Lambda function. You'll then learn how to view and log out the event object that includes metadata about the GraphQL operation.

Nader Dabit: [0:00] To get started, go to the AWS Management Console at console.aws.amazon.com. Here, we'll search for the Lambda service. In the Lambda console, click Create function. Here, give the function a name. Choose Node.js as the runtime, and then click Create function.

[0:29] Once the function is created, open index.js and update the function to return an object with the property of data set to "Hello from Lambda."

[0:49] Next, we'll open up the AWS Management Console in a new window and search for AWS AppSync. In the AppSync console, click Create API, choose Build from scratch, and then click Start. Give the API a name, and then click Create.

[1:17] Now that the API is created, we'll click on Schema to create a new GraphQL schema. Update the schema to have a type of message that has a field of data set to String! And a query of getMessage that returns a Message. Once you've created the schema, click Save schema.

[1:43] Next, we'll need to add a new data source. To do that, click on Data Sources, and then click Create data source. For the data source name, I'll just choose the name of the Lambda function.

[1:54] For the resource type, we'll choose AWS Lambda function. For the region, we'll choose the same region where we created the function. For the Function ARN, I'll paste in the name of the function, and then choose the ARN for that function. Finally, we'll choose New role, and then click Create.

[2:19] Now that the data source has been created, we can map the data source to the query that we've created in our schema. To do so, open the schema editor, and then click attach next to get message in the Resolver's view.

[2:33] We should now be able to choose the data source that we just created, and then click Save Resolver to save the resolver.

[2:43] To test everything out, open the Queries editor, and then create a query of getMessage that returns the data field.

[3:01] Next, let's log out the event that gets sent from the AppSync API into the Lambda function. To do so, we'll open the Lambda function and console.log the event. We'll then run the query one more time.

[3:32] To view the logs, we'll go back into the Lambda console, and then click on Monitoring. Here we'll click on View logs in CloudWatch to open the CloudWatch logs.

[3:50] In this view, we should now see an event from Lambda object in the logs. The arguments object will contain any arguments passed into the GraphQL query or mutation. The info object contains additional metadata about the requests, including the selectionSetList the parentTypeName and the fieldName.

egghead
egghead
~ 2 minutes ago

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

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

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!

Markdown supported.
Become a member to join the discussionEnroll Today