AWS Appsync + DynamoDB with business logic - amazon-web-services

Is it possible to have business logic on my AppSync's mutation when the datasource is dynamoDB?
I'm fairly new to GraphQL and Appsync. My understanding is when you're using plain GraphQL you can have business logic inside your resolver to validate before updating. How would you achieve the same thing with AWS AppSync when you pass it the GraphQL schema with DynamoDB as the datasource?

In AWS AppSync, you would attach a resolver to the field that is mapped to the DynamoDB DataSource. You can then use the Velocity template to have your custom business logic inside the resolver for any validations.
Please refer to the following documentation on the Resolver Mapping Template programming guide from AWS.
Here is the mapping template reference for DynamoDB resolvers.

According to AWS AppSync's Website: With AppSync, your app can access data in Amazon DynamoDB, trigger AWS Lambda functions, or run Amazon Elasticsearch queries.
You can think of it as a gateway for clients to access different backends (data sources), defined by mapping templates attached to GraphQL fields (resolvers).
AppSync supports DynamoDB and ElasitcSearch queries natively, but if you want to perform business logic you will have to add a AWS Lambda data source and then use AWS SDKs to R/W DynamoDB or anything else such as another API or maybe even an excel file!
Additionally, you can use Apache VTL along with AppSync's available helpers such as $context to help you perform authorization or field data access based on permissions. Keep in mind that your DynamoDB or ES resolver can only perform 1 operation at the end, Apache VTL only helps you build the resolver that will be run by AppSync.
Here are two diagrams that compare a traditional approach vs appsync

Related

How can I set up ui-less external API auth using AWS?

I'm trying to create an external API using AWS API Gateway that will give users access to data stored in multiple databases. The APIs will mostly be accessed through scripts rather than through a web UI.
Are there any AWS services I can use to manage user access to my API?
I've read a little bit about Amazon Cognito and OAuth 2 but at a glance it seems like those might be more targeted towards cases with a UI for users to interact with. Is there a way to create and manage API keys with AWS?
Thanks in advance for your help!
You can use API Gateway Lambda Authorizer to write your custom login integration. For example a lambda that check in one Database if the user:password (passed as authorization header) exists in table in DynamoDB or SQL.

aws amplify work with documentdb or document type nosql database

So I am relatively new to AWS and AWS Amplify helped me write my backend app a lot. I know AWS Amplify does offer a NoSQL database direct connection, but that's connected to DynamoDB, which is also a NoSQL database, but it's not the type of NoSQL database I want. I want document type BSON NoSQL database. I was wondering is there any easy way to connect to AWS DocumentDB or MongoDB using AWS Amplify or do I need to use aws-sdk or something else for that? Thanks
You can create a GraphQL API for Amazon DocumentDB and use this API as your amplify backend application as explained in this documentation.
To create a GraphQL API for Amazon DocumentDB, you can use Lambda resolvers. Please refer to tutorial on using Lambda Resolvers here.
Amazon DocumentDB connection and related queries will be defined in the lambda function.

Creating AWS AppSync using AWS Lambda (NodeJS)

In my project, we create tables in AWS dynamodb based on the user id using AWS lambda (NodeJS).
Once the table is created the lambda function will create a graphql endpoint for that particular table so that the user can get real-time data for that specific table.
Can we create graphql endpoints which will also be seen in AWS appsync from AWS lambda (NodeJs)?
I searched the web but didn't find any tutorial from AWS nor any articles regarding it.
Well to be frank its will be very hard to achieve such functionality but if you still required then yes you can do that using AWS AppSync SDK
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AppSync.html
From you tags assuming that you are using javascript.
Just my suggestion :- Dont try to do such things as it wont be easy to manage multiple app async and cost will increased x10

Web UI Wrapper for DynamoDB

Are there any resources out there for wrapping access to DynamoDB in a web UI?
Currently, I have a Lambda function that will populate a Dynamo table via S3 trigger. However, I would like to allow my team access to items in a table without needing to go into DynamoDB or upload a file to the S3 console. I'm not sure what's needed to fulfill this requirement, so any references would be greatly appreciated.
If it were me, I would create API Gateway endpoints for each method you want to expose, have the API Gateway call your Lambda function to retrieve the data you want, and return that as JSON thru the API to your browser - and then use the JS framework of your choice to present it to the user (I use Angular and Bootstrap, but any JS/CSS would work as well).

How do I export AWS AppSync resolvers?

I have setup AppSync with a Schema and Resolvers. I can export the Schema to a file, but I cannot see how to export the Resolvers.
I want to store these in a file so that I can source control them. They contain plenty of SQL code that I don't want to lose.
Command template: TYPE_NAME values: Mutation, Query and Subscription.
aws appsync list-resolvers --api-id YOUR_API_ID --type-name TYPE_NAME >> YOUR_FILE.txt
Examples: With YOUR_API_ID = d5gebysm3 (The original length is 26 in my case)
aws appsync list-resolvers --api-id d5gebysm3 --type-name Mutation >> Mutation.txt
aws appsync list-resolvers --api-id d5gebysm3 --type-name Query >> Query.txt
aws appsync list-resolvers --api-id d5gebysm3 --type-name Subscription >> Subscription.txt
Before you go any farther, I would recommend you look into managing your AppSync resources with CloudFormation. CloudFormation templates can easily be saved in source control.
AppSync & CloudFormation Tutorials:
https://read.acloud.guru/deploy-a-graphql-service-on-aws-with-the-serverless-framework-7af8fc22a01d
https://read.acloud.guru/deploy-an-aws-appsync-graphql-api-with-amazon-cloudformation-9a783fdd8491
Or if your resolvers aren't doing anything custom, you can use Amplify's GraphQL Transformer. This allows you to annotate your schema and it will automatically generate resolvers from the annotations. Then you can put the annotated schema into source control. Documentation:
https://aws-amplify.github.io/docs/js/api#using-graphql-transformers
There is a node.js package specifically made for this:
export-appsync.
But in the long term, it's easier to source control your schema and resolvers of you work from either serverless framework (serverless.com), from cloudformation or the AWS amplify framework.
The nice thing about AWS is that there's probably an API endpoint for what you're looking for.
In this case, you can access the list of resolvers via the ListResolvers API endpoint, and you can retrieve a specific resolver via the GetResolver API endpoint.