I understand that context is what ever name you define in your Lambda functions but when it comes to Appsync resolvers I'm a bit confused. I've seen both $ctx and $context being used in AppSync resolvers including in AWS docs. Some of AWS's own code generation tools like AWS Amplify CLI create resolvers that use both in the same code! I can't find anything in the docs explaining this. What's going on here?
(AWS AppSync dev here)
$ctx and $context in AWS AppSync refer to the same Resolver Context. We added $ctx as an alias for $context to reduce the number of characters users have to type ($ctx is 50% shorter than $context!!)
Choosing $context or $ctx is a personal preference. I prefer $ctx when I'm authoring AWS AppSync resolvers.
Related
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
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.
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
Here is the official documentation for AWS Amplify GraphQL Client: https://aws-amplify.github.io/amplify-js/media/api_guide.html. The section supplies an example for basic String inputs though.
For AWS Mobile Appsync SDK for Javascript, there is a detailed doc here: https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-react.html. However, I do not want to add another configuration for it -I already have one for Amplify.
So, how to upload files to S3 storage by using AWS Amplify and AWS AppSync as the backend, what extra configuration is needed for Amplify -if there is?
Http Endpoints are added as an option to be used as datasources to AppSync schemata, but as of time, there is no S3 bucket as an option. There are solutions like [this](
https://stackoverflow.com/a/50218870/4636715), but they require AWSAppSyncClient at Javascript side which would add complexity to the client code as Amplify is set up there already imho.
So, I ended up with using Storage
of AWS Amplify -independent of AppSync. Then, I wait for the upload to be successful and call an AppSync mutation to store the key for the uploaded file in DynamoDB using regular datasource resolvers.
Is there a way for GraphQL resolver to resolve data source from endpoint exposed by a lambda function generated by CodeStar?
The examples provided in the AppSync docs shows how I resolve data from single lambda function, but in my case, I'm using AWS Codestar to generate a REST API using lambda, with several endpoints that returns different set of data.
For example, when I want to resolve a fetchPosts, i need to resolve it from https://my-prod-example-url/posts endpoint, not just invoke the lambda function.
I am on the AWS AppSync team. Http data sources are not available yet but they're very much on our roadmap.