Skip to content

Instantly share code, notes, and snippets.

@dtelaroli
Created October 23, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtelaroli/2a44b0f9dc39b0c42acfe6a410080961 to your computer and use it in GitHub Desktop.
Save dtelaroli/2a44b0f9dc39b0c42acfe6a410080961 to your computer and use it in GitHub Desktop.
Amplify Cloudformation Example - AppSync HTTP Datasource
{
...
"Resources": {
"EmptyResource": {
"Type": "Custom::EmptyResource",
"Condition": "AlwaysFalse"
},
"MyDataSource": {
"Type": "AWS::AppSync::DataSource",
"Properties": {
"ApiId": {
"Ref": "AppSyncApiId"
},
"Type": "HTTP",
"Name": "MyDataSource",
"HttpConfig": {
"Endpoint": "https://myapi.domain.com"
}
}
},
"QueryMyDataSourceResolver": {
"Type": "AWS::AppSync::Resolver",
"Properties": {
"ApiId": {
"Ref": "AppSyncApiId"
},
"DataSourceName": {
"Fn::GetAtt": [
"MyDataSource",
"Name"
]
},
"TypeName": "Query",
"FieldName": "myFieldName",
"RequestMappingTemplateS3Location": {
"Fn::Sub": [
"s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Query.myFieldName.req.vtl",
{
"S3DeploymentBucket": {
"Ref": "S3DeploymentBucket"
},
"S3DeploymentRootKey": {
"Ref": "S3DeploymentRootKey"
}
}
]
},
"ResponseMappingTemplateS3Location": {
"Fn::Sub": [
"s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Query.myFieldName.res.vtl",
{
"S3DeploymentBucket": {
"Ref": "S3DeploymentBucket"
},
"S3DeploymentRootKey": {
"Ref": "S3DeploymentRootKey"
}
}
]
}
}
}
}
...
}
#**
Inside folder api/apiname/resolvers
Make an arbitrary HTTP call when this field is listed in a query selection set.
The "relativePath" is the resource path relative to the root URL provided when you
created the HTTP data source. Pass a "params" object to forward headers, body,
and query parameters to the HTTP endpoint.
*#
{
"version": "2018-05-29",
"method": "GET",
## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
"resourcePath": "/path",
"params": {
"query":$util.toJson($ctx.args),
"headers": {
"Authorization": "$ctx.request.headers.Authorization"
}
}
}
## Inside folder api/apiname/resolvers
## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#end
## If the response is not 200 then return an error. Else return the body **
#if($ctx.result.statusCode == 200)
$ctx.result
#else
$utils.appendError($ctx.result, $ctx.result.statusCode)
#end
type MyModel {
statusCode: Int!
body: String!
}
type Query {
myFieldName: MyModel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment