Skip to content

Instantly share code, notes, and snippets.

@koingdev
Last active August 16, 2019 06:50
Show Gist options
  • Save koingdev/16084d28b64575e488bb216109ec2554 to your computer and use it in GitHub Desktop.
Save koingdev/16084d28b64575e488bb216109ec2554 to your computer and use it in GitHub Desktop.
Make AWS Amplify support custom fields (Mutation/Query)
  1. Add new custom schema to schema.graphql file

  2. Create custom request/response mapping resolvers file in resolvers folder. The file should look like this:

    • REQUEST_MAPPING: Mutation.putUser.req.vtl
    • RESPONSE_MAPPING: Mutation.putUser.res.vtl
  3. Update CustomResources.json file in stack folder

type User @model {
id: ID!
displayName: String!
}
type Mutation {
# THIS IS OUR CUSTOM FIELD
putUser(displayName: String!): User
}
## [Start] Prepare DynamoDB PutItem Request. **
{
"version": "2017-02-28",
"operation": "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.args.id, $util.autoId())),
},
"attributeValues": {
"displayName": $util.dynamodb.toDynamoDBJson($ctx.args.displayName)
}
}
## [End] Prepare DynamoDB PutItem Request. **
$util.toJson($context.result)
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "An auto-generated nested stack.",
"Metadata": {},
"Parameters": {
"AppSyncApiId": {
"Type": "String",
"Description": "The id of the AppSync API associated with this project."
},
"AppSyncApiName": {
"Type": "String",
"Description": "The name of the AppSync API",
"Default": "AppSyncSimpleTransform"
},
"env": {
"Type": "String",
"Description": "The environment name. e.g. Dev, Test, or Production",
"Default": "NONE"
},
"S3DeploymentBucket": {
"Type": "String",
"Description": "The S3 bucket containing all deployment assets for the project."
},
"S3DeploymentRootKey": {
"Type": "String",
"Description": "An S3 key relative to the S3DeploymentBucket that points to the root\nof the deployment directory."
}
},
"Resources": {
"_comment": "JUST PUT YOUR CUSTOM QUERIES OR MUTATIONS IN RESOURCES BLOCK............",
"MutationPutUserResolver": {
"Type": "AWS::AppSync::Resolver",
"Properties": {
"ApiId": {
"Ref": "AppSyncApiId"
},
"DataSourceName": "UserTable",
"TypeName": "Mutation",
"FieldName": "putUser",
"RequestMappingTemplateS3Location": {
"Fn::Sub": [
"s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.putUser.req.vtl",
{
"S3DeploymentBucket": {
"Ref": "S3DeploymentBucket"
},
"S3DeploymentRootKey": {
"Ref": "S3DeploymentRootKey"
}
}
]
},
"ResponseMappingTemplateS3Location": {
"Fn::Sub": [
"s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.putUser.res.vtl",
{
"S3DeploymentBucket": {
"Ref": "S3DeploymentBucket"
},
"S3DeploymentRootKey": {
"Ref": "S3DeploymentRootKey"
}
}
]
}
}
},
"EmptyResource": {
"Type": "Custom::EmptyResource",
"Condition": "AlwaysFalse"
}
},
"Conditions": {
"HasEnvironmentParameter": {
"Fn::Not": [
{
"Fn::Equals": [
{
"Ref": "env"
},
"NONE"
]
}
]
},
"AlwaysFalse": {
"Fn::Equals": [
"true",
"false"
]
}
},
"Outputs": {
"EmptyOutput": {
"Description": "An empty output. You may delete this if you have at least one resource above.",
"Value": ""
}
}
}
@ohsik
Copy link

ohsik commented Aug 16, 2019

@koingdev
Hi, how are you? Thank you for helping me out for all of this. I'm finally making some progress and got to the point that I have to query all public journals from all the users on the site. I believe I will have to use custom resolver for this and am not still clear how to set this up.

I just copied over the files you shared here on my project and wanted to see if you can just check really quick.

I assume I will have to edit Query.getPublicJournals.req.vtl to query all journals privacy = PUBLIC on the Journal table?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment