Skip to content

Commit

Permalink
Resolvers from yml
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd999 committed Nov 15, 2018
1 parent ee7fc85 commit ba20795
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 55 deletions.
31 changes: 5 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,11 @@
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
.credentials.yml
coverage.out
/Dockerfile
/resource-template
fetch.json
/docs/_site/
# packages and build executables
vendor
bin
out/test

# test file
out
input.json
test
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ A Concourse resource to update AppSync schema. Written in Go.

### `out`: Update or Create schema.

Given a schema specified by `schemaFile`, to update/create AppSync schema Or/And Given a resolvers JSON specified by `resolversContent`, to update AppSync existing schema resolvers.
Given a schema specified by `schema_file`, to update/create AppSync schema Or/And Given a resolvers JSON specified by `resolvers_file`, to update AppSync existing schema resolvers.

#### Parameters

* `schemaFile`: *Optional.* .grapqh schema File provided by an output of a task, if you didn't specify `resolversContent` this field is *Required.*.
* `schema_file`: *Optional.* .grapqh schema File provided by an output of a task, if you didn't specify `resolvers_file` this field is *Required.*.

* `resolversContent`: *Optional.* .json resolver String provided by an output of a task, if you didn't specify `schemaFile` this field is *Required.*.
* `resolvers_file`: *Optional.* .yml resolver String provided by an output of a task, if you didn't specify `schema_file` this field is *Required.*.
.

## Example Configuration
Expand Down Expand Up @@ -60,7 +60,12 @@ resource:
- put: appsync-resource
params:
schema_file: "path/to/schema.graphql"
resolvers: "[{\"dataSourceName\": \"test\", \"fieldName\": \"getTodos\", \"requestMappingTemplate\": {\"version\": \"2017-02-28\", \"operation\": \"Invoke\", \"payload\": \"$util.toJson($context.args)\"}, \"responseMapping\": \"$util.toJson($context.result)\", \"typeName\": \"Query\"}, {\"dataSourceName\": \"test\", \"fieldName\": \"name\", \"requestMappingTemplate\": {\"version\": \"2017-02-28\", \"operation\": \"Invoke\", \"payload\": \"$util.toJson($context.args)\"}, \"responseMapping\": \"$util.toJson($context.result)\", \"typeName\": \"Todo\"}]"
resolvers_file: "path/to/resolvers.yml"
```
### Params file example
[schema.graphql] (https://github.com/telia-oss/appsync-resource/blob/master/schema.graphql)
[resolvers.yml] (https://github.com/telia-oss/appsync-resource/blob/master/resolvers.yml)
55 changes: 30 additions & 25 deletions out/out.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package out

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -10,6 +9,8 @@ import (
"strconv"
"time"

yaml "gopkg.in/yaml.v2"

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -38,17 +39,16 @@ type (
}
outOutputJSON inOutputJSON

RequestMappingTemplate struct {
Version string
Operation string
Payload string
Resolvers struct {
Resolvers []Resolver `yaml:"resolvers"`
}

Resolver struct {
DataSourceName string
FieldName string
RequestMappingTemplate RequestMappingTemplate
ResponseMapping string
TypeName string
DataSourceName string `yaml:"dataSource"`
FieldName string `yaml:"fieldName"`
RequestMappingTemplate string `yaml:"requestMappingTemplate"`
ResponseMappingTemplate string `yaml:"responseMappingTemplate"`
TypeName string `yaml:"typeName"`
}
)

Expand Down Expand Up @@ -77,7 +77,10 @@ func NewAwsConfig(

return awsConfig
}
func createOrUpdateResolvers(appsyncClient *appsync.AppSync, resolvers []Resolver, apiID string, logger *log.Logger) (string, string, string, string) {

// TODO refactor functions into service

func createOrUpdateResolvers(appsyncClient *appsync.AppSync, resolvers Resolvers, apiID string, logger *log.Logger) (string, string, string, string) {
// number of resolvers successfully created
var nResolversSuccessfullyCreated = 0
// number of resolver successfully updated
Expand All @@ -87,7 +90,7 @@ func createOrUpdateResolvers(appsyncClient *appsync.AppSync, resolvers []Resolve
// number of resolver fail to update
var nResolversfailUpdate = 0

for _, resolver := range resolvers {
for _, resolver := range resolvers.Resolvers {
resolverResp, err := getResolver(appsyncClient, &appsync.GetResolverInput{
ApiId: aws.String(apiID),
FieldName: aws.String(resolver.FieldName),
Expand All @@ -103,7 +106,7 @@ func createOrUpdateResolvers(appsyncClient *appsync.AppSync, resolvers []Resolve
DataSourceName: aws.String(resolver.DataSourceName),
FieldName: aws.String(resolver.FieldName),
RequestMappingTemplate: aws.String(fmt.Sprintf("%s", resolver.RequestMappingTemplate)),
ResponseMappingTemplate: aws.String(resolver.ResponseMapping),
ResponseMappingTemplate: aws.String(resolver.ResponseMappingTemplate),
TypeName: aws.String(resolver.TypeName),
}
_, err := updateResolver(appsyncClient, params)
Expand All @@ -118,7 +121,7 @@ func createOrUpdateResolvers(appsyncClient *appsync.AppSync, resolvers []Resolve
DataSourceName: aws.String(resolver.DataSourceName),
FieldName: aws.String(resolver.FieldName),
RequestMappingTemplate: aws.String(fmt.Sprintf("%s", resolver.RequestMappingTemplate)),
ResponseMappingTemplate: aws.String(resolver.ResponseMapping),
ResponseMappingTemplate: aws.String(resolver.ResponseMappingTemplate),
TypeName: aws.String(resolver.TypeName),
}
_, err := createResolver(appsyncClient, params)
Expand Down Expand Up @@ -222,7 +225,7 @@ func Out(input InputJSON, logger *log.Logger) (outOutputJSON, error) {

schemaFile, _ := input.Params["schema_file"]

resolvers, _ := input.Params["resolvers"]
resolversFile, _ := input.Params["resolvers_file"]

var ref = input.Version.Ref
var output outOutputJSON
Expand All @@ -237,8 +240,8 @@ func Out(input InputJSON, logger *log.Logger) (outOutputJSON, error) {
regionName,
)

if schemaFile == "" && resolvers == "" {
return outOutputJSON{}, errors.New("resolvers and schemaFile both are not set")
if schemaFile == "" && resolversFile == "" {
return outOutputJSON{}, errors.New("resolversFile and schemaFile both are not set")
}

session, err := session.NewSession(awsConfig)
Expand Down Expand Up @@ -280,14 +283,16 @@ func Out(input InputJSON, logger *log.Logger) (outOutputJSON, error) {
}
}
// update Resolvers
if resolvers != "" {
resolverJSONTpl := fmt.Sprintf("`%s`", resolvers)
var resolversArr []Resolver
val := []byte(resolverJSONTpl)
s, _ := strconv.Unquote(string(val))
json.Unmarshal([]byte(s), &resolversArr)

nResolversSuccessfullyCreated, nResolversfailCreated, nResolversSuccessfullyUpdated, nResolversfailUpdate := createOrUpdateResolvers(appsyncClient, resolversArr, apiID, logger)
if resolversFile != "" {
resolversFilePath := fmt.Sprintf("%s/%s", os.Args[1], resolversFile)
resolversFile, _ := ioutil.ReadFile(resolversFilePath)
var resolvers Resolvers
err = yaml.Unmarshal(resolversFile, &resolvers)
if err != nil {
panic(err)
}

nResolversSuccessfullyCreated, nResolversfailCreated, nResolversSuccessfullyUpdated, nResolversfailUpdate := createOrUpdateResolvers(appsyncClient, resolvers, apiID, logger)
// OUTPUT
resolverOutput = []metadata{
{Name: "number of resolvers successfully created", Value: nResolversSuccessfullyCreated},
Expand Down
15 changes: 15 additions & 0 deletions resolvers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
resolvers:
- dataSource: test
typeName: Query
fieldName: getTodos
requestMappingTemplate: >
\#set( $payload = {
"bar" : $context.request.foo
} )
{
"version" : "2017-02-28",
"operation": "Invoke",
"payload": $util.toJson($payload)
}
responseMappingTemplate: $util.toJson($context.result)
14 changes: 14 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Query {
getTodos: [Todo]
}

type Todo {
id: ID!
name: String
description: Int
priority: Int
}

schema {
query: Query
}

0 comments on commit ba20795

Please sign in to comment.