DEV Community

Lambda Function GraphQL Resolvers

Nader Dabit on June 11, 2019

The Amplify CLI recently added support for deploying Lambda GraphQL resolvers directly from your Amplify environment for your AppSync APIs. Lambd...
Collapse
 
jimcalliu profile image
𝓙𝓲𝓢 𝓛𝓲𝓾

Followup: I like to change the name of project/function when follow tutorial, this time, however, I discover if I don't use function in lambda name, the cloudformation could not actually create the stack.

I went ahead and redo the first part of tutorial, but run into an issue when running amplify push after I run amplify update api (instead of amplify add api I had to use update because the api has already been created.)

Current Environment: dev

| Category | Resource name    | Operation | Provider plugin   |
| -------- | ---------------- | --------- | ----------------- |
| Function | currencyfunction | Create    | awscloudformation |
| Function | currency         | No Change | awscloudformation |
| Api      | currencyapi      | No Change | awscloudformation |
? Are you sure you want to continue? Yes
βœ– An error occurred when pushing the resources to the cloud

Inaccessible host: `gql-lambda-dev-20190623145233-deployment.s3.us-west-2.amazonaws.com'. This service may not be available in the `us-west-2' region.

Any pointer would be appreciated!

Collapse
 
nubpro profile image
nubpro

Hi Nader, few questions:

  1. What will happen if add @model directives into type Coin?
  2. Lets say I want to add a resolver for updateCoin, how would I differentiate between createCoin and updateCoin resolvers since they have the same operation?
Collapse
 
yiksanchan profile image
Yik San Chan

Is it possible to access s3/dynamodb configured in an existing amplify project via @function directive? github.com/aws-amplify/amplify-cli...

Collapse
 
diego profile image
Diego

It doesn't seem to be possible at the moment. I wonder if we can expect this functionality in future versions.

Collapse
 
jimcalliu profile image
𝓙𝓲𝓢 𝓛𝓲𝓾

Hey Nader,

Excellent tutorial. I was following along and use amplify push to deploy the Lambda. After I type Y to "Are you sure you want to continue", I saw the following question and stuck on the last one.

? Do you want to generate code for your newly created GraphQL API Yes
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions (src/gra
phql/**/*.js)

Any recommendation? Where can I find the documentation for the Amplify API and its behavior?

Really excited about what AWS Mobile team has build. Please keep up the great work.

Collapse
 
boricic profile image
Milan Boricic • Edited

This is awesome. There is still one part that I haven't been able to figure out.

How does STORAGE_CURRENCYTABLE_NAME environment variable get created? I've looked at the Github repo, but couldn't find a place where it is defined. I can just see that it is accessed within Lambda code.

I assume it was created through Amplify Console, right?

Collapse
 
karldanninger profile image
Karl Danninger πŸš€

I think I found it here: console.aws.amazon.com/lambda/home there's a section halfway down for environment variables that you can add. Nader must've made a custom one in the console.

Collapse
 
cedricindra profile image
Cedric Indra • Edited

Doing this tutorial and things have changed in the amplify CLI so I'm trying to work around that because I like the tone and pace of these tuts.
When updating function, cannot figure out how to "Select the operations you want to permit for currencytable:" Seems that is deprecated.
If anyone can point me to a fix.

Collapse
 
lucidoapps profile image
lucido-apps

Hi Nader,

Thank you for this post! Most of the tutorials and documentation I find about Lambda resolvers involve creating new queries and mutations to use the Lambdas. However, I think it would be very helpful to use Lambda resolvers on queries and mutations that are generated automatically from the GraphQL schema by Amplify.

For example, let's say I have a schema that defines a Person with a phoneNumber field. Amplify automatically generates getPerson, createPerson, etc. How can I create a lambda resolver that is triggered on createPerson (for example to validate the phoneNumber)?

I tried many different approaches but I keep getting errors, it seems that Amplify doesn't play well with adding to or modifying automatically generated resolvers.

I know I can perform phoneNumber validation inside the VTL resolver, but what if I want to use a Node.js validation library for more concise and thorough data validation for all my default mutations?

Thanks again,
Santiago

Collapse
 
ganapathiraju profile image
ganapathiraju

Hello Nader Dabit,
Good article from the rest of the articles w.r.t resolving graphql and lamda function. Appreciate it.
After following your steps, I got blocked at the first query step from console as the query is throwing exception Lambda unhandler. Therefore I did not go ahead to complete the application. Where could I go wrong?
Running the query listCoins from console->queries is resulting as below.
if the url 'api.coinlore.com/api/tickers/?star...' is executed from "Postman" client then the result is proper.
{
"data": {
"getCoins": null
},
"errors": [
{
"path": [
"getCoins"
],
"data": null,
"errorType": "Lambda:Unhandled",
"errorInfo": null,
"locations": [
{
"line": 31,
"column": 3,
"sourceName": null
}
],
"message": "SyntaxError: Unexpected token 'export'"
}
]
}

Collapse
 
ganapathiraju profile image
ganapathiraju

Problem resolved. There was error in the lambda function.
Kindly ignore the query

Collapse
 
rigalpatel001 profile image
Rigal Patel

@dabit3
I've added auth and API. It created the dynamo DB table etc in the cloud. The AWS App sync queries are running fine. I want to create a lambda function to Insert new records in a table.

Tables are already existing how I can add a lambda function to API generated tables?

Would you please guide me or provides steps to Integrate Lamda with GraphQL API generated tables?

Thanks

Collapse
 
imewish profile image
Vishnu Prassad

Hi @dabit3 ,

Nice article. I was able to set up my first Appsync API today. How can we return a custom error messages in a mutation call with lambda resolver?

Collapse
 
mjmaix profile image
MJ Abadilla

"currencyfunction-${env}" nice trick. Are there more variables available inside schema.graphql? Like project name?

Collapse
 
indatawetrust profile image
Ahmet Simsek

Thanks

Collapse
 
eranda profile image
Eranda Hettiarachchi • Edited

Hey @dabit3 , Do you have an example of how to connect to RDS from Lambda and provision all the resorces (including rds database) with cloudformation/amplify?

Collapse
 
plukkemx profile image
Plukke

Is it possible connect an existing DynamoDB table to noSQL storage for using it with functions?