router

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2018 License: MIT Imports: 4 Imported by: 0

README

AppSync Router

GitHub release MIT License GoDoc Go Report Card Build Status Test Coverage

Wrapper for routing AWS AppSync resolvers with AWS Lambda in Go. See appsync-router-example for an example project and how to set up a complete GraphQL API using the Serverless Application Model.

Usage

Installation
$ > go get github.com/sbstjn/appsync-router
Routing
import (
  "github.com/sbstjn/appsync-router"
)

func resolverA(args struct {
	Foo string `json:"foo"`
}) (interface{}, error) {
	return nil, fmt.Errorf("Nothing here in resolver A: %s", args.Foo)
}

func resolverB(args struct {
	Bar string `json:"bar"`
}) (interface{}, error) {
	return nil, fmt.Errorf("Nothing here in resolver B: %s", args.Bar)
}

var (
	r = router.New()
)

func init() {
	r.Add("fieldA", resolverA)
	r.Add("fieldB", resolverB)
}

func main() {
	lambda.Start(r.Handle)
}
AppSync Configuration

Routing is based on a field property in your RequestMappingTemplate, which can be configured using the AWS Console or CloudFormation as well. This approach works fine with the recommended AWS setup with multiple custom resolvers and AWS Lambda:

  AppSyncDataSource:
    Type: AWS::AppSync::DataSource
    Properties:
      ApiId: !GetAtt [ AppSyncAPI, ApiId ]
      Name: resolver
      Type: AWS_LAMBDA
      LambdaConfig:
        LambdaFunctionArn: !GetAtt [ Lambda, Arn ]
      ServiceRoleArn: !GetAtt [ Role, Arn ]

  AppSyncResolverA:
    Type: AWS::AppSync::Resolver
    Properties:
      ApiId: !GetAtt [ AppSyncAPI, ApiId ]
      TypeName: Query
      FieldName: fieldA
      DataSourceName: !GetAtt [ AppSyncDataSource, Name ]
      RequestMappingTemplate: '{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "field": "fieldA", "arguments": $utils.toJson($context.arguments) } }'
      ResponseMappingTemplate: $util.toJson($context.result)

  AppSyncResolverB:
    Type: AWS::AppSync::Resolver
    Properties:
      ApiId: !GetAtt [ AppSyncAPI, ApiId ]
      TypeName: Query
      FieldName: fieldB
      DataSourceName: !GetAtt [ AppSyncDataSource, Name ]
      RequestMappingTemplate: '{ "version" : "2017-02-28", "operation": "Invoke", "payload": { "field": "fieldB", "arguments": $utils.toJson($context.arguments) } }'
      ResponseMappingTemplate: $util.toJson($context.result)

See appsync-router-example for a full working example how to use appsync-router and the [Amazon Serverless Application Model] to deploy a GraphQL API using AppSync.

License

Feel free to use the code, it's released using the MIT license.

Contribution

You are welcome to contribute to this project! 😘

To make sure you have a pleasant experience, please read the code of conduct. It outlines core values and beliefs and will make working together a happier experience.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler added in v0.0.2

type Handler struct {
	// contains filtered or unexported fields
}

Handler is an abstract function

func (*Handler) Call added in v0.0.2

func (h *Handler) Call(payload json.RawMessage) (interface{}, error)

Call the handler and pass event

func (*Handler) Prepare added in v0.0.2

func (h *Handler) Prepare(payload json.RawMessage) ([]reflect.Value, error)

Prepare parses event payload

func (Handler) Validate added in v0.1.0

func (h Handler) Validate() error

Validate checks if passed handler is valid

type Request

type Request struct {
	Field     string          `json:"field"`
	Arguments json.RawMessage `json:"arguments"`
}

Request stores all information from AppSync request

type Router

type Router map[string]Handler

Router stores all routes and handlers

func New

func New() Router

New returns a new Router

func (Router) Add

func (r Router) Add(route string, function interface{}) error

Add stores a new route with handler

func (Router) Get added in v0.0.2

func (r Router) Get(route string) (*Handler, error)

Get return handler for route or error

func (Router) Handle added in v0.1.0

func (r Router) Handle(req Request) (interface{}, error)

Handle responds to the AppSync request

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL