From ba207954d91881fee100b35aebe28e4b82435920 Mon Sep 17 00:00:00 2001 From: Mohamad al moualem Date: Thu, 15 Nov 2018 12:18:51 +0100 Subject: [PATCH] Resolvers from yml --- .gitignore | 31 +++++----------------------- Gopkg.lock | 9 +++++++++ README.md | 13 ++++++++---- out/out.go | 55 +++++++++++++++++++++++++++----------------------- resolvers.yml | 15 ++++++++++++++ schema.graphql | 14 +++++++++++++ 6 files changed, 82 insertions(+), 55 deletions(-) create mode 100644 resolvers.yml create mode 100644 schema.graphql diff --git a/.gitignore b/.gitignore index bc81174..a2ad39f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Gopkg.lock b/Gopkg.lock index b06b04c..a5a4dbc 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -49,6 +49,14 @@ pruneopts = "UT" revision = "0b12d6b5" +[[projects]] + digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" + version = "v2.2.1" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 @@ -57,6 +65,7 @@ "github.com/aws/aws-sdk-go/aws/credentials", "github.com/aws/aws-sdk-go/aws/session", "github.com/aws/aws-sdk-go/service/appsync", + "gopkg.in/yaml.v2", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/README.md b/README.md index d31aa36..211737e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/out/out.go b/out/out.go index 089952d..c8314b8 100644 --- a/out/out.go +++ b/out/out.go @@ -1,7 +1,6 @@ package out import ( - "encoding/json" "errors" "fmt" "io/ioutil" @@ -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" @@ -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"` } ) @@ -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 @@ -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), @@ -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) @@ -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) @@ -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 @@ -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) @@ -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}, diff --git a/resolvers.yml b/resolvers.yml new file mode 100644 index 0000000..27c763f --- /dev/null +++ b/resolvers.yml @@ -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) \ No newline at end of file diff --git a/schema.graphql b/schema.graphql new file mode 100644 index 0000000..a76acc0 --- /dev/null +++ b/schema.graphql @@ -0,0 +1,14 @@ +type Query { + getTodos: [Todo] +} + +type Todo { + id: ID! + name: String + description: Int + priority: Int +} + +schema { + query: Query +} \ No newline at end of file