Help us shape the future of Go! Take the 2026 Go Developer Survey

router

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2018 License: MIT Imports: 2 Imported by: 0

README

AppSync Router

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

Wrapper for routing AppSync resolvers with AWS Lambda using Go.

Usage

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

func handleRouteA(req json.RawMessage) (interface{}, error) {
	return nil, errors.New("Nothing here in route A")
}

func handleRouteB(req json.RawMessage) (interface{}, error) {
	return nil, errors.New("Nothing here in route B")
}

var (
	r = router.New()
)

func init() {
	r.Add("fieldA", handleRouteA)
	r.Add("fieldB", handleRouteB)
}

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

Routing is based on a field property in your RequestMappingTemplate, which can be configured using the AWS Console or CloudFormation as well.

  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 [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 Request

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

Request stores all information from AppSync request

type RouteHandler

type RouteHandler func(req json.RawMessage) (interface{}, error)

RouteHandler defines the function to handle the request

type Router

type Router struct {
	Routes map[string]RouteHandler
}

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, handler RouteHandler)

Add stores a new route with handler

func (*Router) Serve

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

Serve parses the AppSync request

Jump to

Keyboard shortcuts

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