From 67f143b3e5239565a2c7033689c78e3cfdf11df4 Mon Sep 17 00:00:00 2001 From: Jon Saw Date: Fri, 27 Jul 2018 13:50:14 +0800 Subject: [PATCH] make invocation exportable --- dispatch.go | 2 +- handle.go | 6 +++--- invocation.go | 7 ++++--- invocation_test.go | 4 ++-- middleware_test.go | 12 ++++++------ repository.go | 2 +- repository_test.go | 8 ++++---- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/dispatch.go b/dispatch.go index e00f20c..9973eed 100644 --- a/dispatch.go +++ b/dispatch.go @@ -6,7 +6,7 @@ type dispatch struct { repository *Repository } -func (d dispatch) Serve(in invocation) (interface{}, error) { +func (d dispatch) Serve(in Invocation) (interface{}, error) { handler, found := d.repository.resolvers[in.Resolve] if found { diff --git a/handle.go b/handle.go index ac05296..193b17b 100644 --- a/handle.go +++ b/handle.go @@ -2,14 +2,14 @@ package resolvers // Handler responds to requests in resolvers type Handler interface { - Serve(invocation) (interface{}, error) + Serve(Invocation) (interface{}, error) } // The HandlerFunc type is an adapter to allow the use of // ordinary functions -type HandlerFunc func(invocation) (interface{}, error) +type HandlerFunc func(Invocation) (interface{}, error) // Serve calls from resolver -func (f HandlerFunc) Serve(in invocation) (interface{}, error) { +func (f HandlerFunc) Serve(in Invocation) (interface{}, error) { return f(in) } diff --git a/invocation.go b/invocation.go index 78b4515..234e580 100644 --- a/invocation.go +++ b/invocation.go @@ -7,16 +7,17 @@ type context struct { Source json.RawMessage `json:"source"` } -type invocation struct { +// Invocation data received from AppSync +type Invocation struct { Resolve string `json:"resolve"` Context context `json:"context"` } -func (in invocation) isRoot() bool { +func (in Invocation) isRoot() bool { return in.Context.Source == nil || string(in.Context.Source) == "null" } -func (in invocation) payload() json.RawMessage { +func (in Invocation) payload() json.RawMessage { if in.isRoot() { return in.Context.Arguments } diff --git a/invocation_test.go b/invocation_test.go index c7ceccc..8c91fd1 100644 --- a/invocation_test.go +++ b/invocation_test.go @@ -9,7 +9,7 @@ import ( var _ = Describe("Invocation", func() { Context("With Arguments", func() { - data := invocation{ + data := Invocation{ Resolve: "exaple.resolver", Context: context{ Arguments: json.RawMessage(`{ "foo": "bar" }`), @@ -26,7 +26,7 @@ var _ = Describe("Invocation", func() { }) Context("With Source", func() { - data := invocation{ + data := Invocation{ Resolve: "exaple.resolver", Context: context{ Source: json.RawMessage(`{ "bar": "foo" }`), diff --git a/middleware_test.go b/middleware_test.go index 15093d3..2c18160 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -50,7 +50,7 @@ var _ = Describe("Middleware", func() { return response{"bar"}, nil }) r.Use(func(h Handler) Handler { - m := func(in invocation) (interface{}, error) { + m := func(in Invocation) (interface{}, error) { ch <- "before 1" out, err := h.Serve(in) ch <- "after 1" @@ -59,7 +59,7 @@ var _ = Describe("Middleware", func() { return HandlerFunc(m) }) r.Use(func(h Handler) Handler { - m := func(in invocation) (interface{}, error) { + m := func(in Invocation) (interface{}, error) { ch <- "before 2" out, err := h.Serve(in) ch <- "after 2" @@ -67,7 +67,7 @@ var _ = Describe("Middleware", func() { } return HandlerFunc(m) }) - res, err := r.Handle(invocation{ + res, err := r.Handle(Invocation{ Resolve: "example.resolver", Context: context{ Arguments: json.RawMessage(`{"bar":"foo"}`), @@ -102,7 +102,7 @@ var _ = Describe("Middleware", func() { return nil, newGraphQLError("BAD_REQUEST", "Invalid type", response{"bar"}) }) r.Use(func(h Handler) Handler { - m := func(in invocation) (interface{}, error) { + m := func(in Invocation) (interface{}, error) { ch <- "before 1" out, err := h.Serve(in) ch <- "after 1" @@ -111,7 +111,7 @@ var _ = Describe("Middleware", func() { return HandlerFunc(m) }) r.Use(func(h Handler) Handler { - m := func(in invocation) (interface{}, error) { + m := func(in Invocation) (interface{}, error) { out, err := h.Serve(in) if err != nil { if errData, ok := err.(*graphQLError); ok { @@ -122,7 +122,7 @@ var _ = Describe("Middleware", func() { } return HandlerFunc(m) }) - res, err := r.Handle(invocation{ + res, err := r.Handle(Invocation{ Resolve: "example.resolver", Context: context{ Arguments: json.RawMessage(`{"bar":"foo"}`), diff --git a/repository.go b/repository.go index 47d0453..51f511e 100644 --- a/repository.go +++ b/repository.go @@ -27,6 +27,6 @@ func (r *Repository) Add(resolve string, handler interface{}) error { } // Handle responds to the AppSync request -func (r *Repository) Handle(in invocation) (interface{}, error) { +func (r *Repository) Handle(in Invocation) (interface{}, error) { return r.handler.Serve(in) } diff --git a/repository_test.go b/repository_test.go index 0e20fa7..a5eb100 100644 --- a/repository_test.go +++ b/repository_test.go @@ -20,7 +20,7 @@ var _ = Describe("Repository", func() { r.Add("example.resolver.with.error", func(arg arguments) (response, error) { return response{"bar"}, errors.New("Has Error") }) Context("Matching invocation", func() { - res, err := r.Handle(invocation{ + res, err := r.Handle(Invocation{ Resolve: "example.resolver", Context: context{ Arguments: json.RawMessage(`{"bar":"foo"}`), @@ -37,7 +37,7 @@ var _ = Describe("Repository", func() { }) Context("Matching invocation with error", func() { - _, err := r.Handle(invocation{ + _, err := r.Handle(Invocation{ Resolve: "example.resolver.with.error", Context: context{ Arguments: json.RawMessage(`{"bar":"foo"}`), @@ -50,7 +50,7 @@ var _ = Describe("Repository", func() { }) Context("Matching invocation with invalid payload", func() { - _, err := r.Handle(invocation{ + _, err := r.Handle(Invocation{ Resolve: "example.resolver.with.error", Context: context{ Arguments: json.RawMessage(`{"bar:foo"}`), @@ -63,7 +63,7 @@ var _ = Describe("Repository", func() { }) Context("Not matching invocation", func() { - res, err := r.Handle(invocation{ + res, err := r.Handle(Invocation{ Resolve: "example.resolver.not.found", Context: context{ Arguments: json.RawMessage(`{"bar":"foo"}`),