Skip to content

Commit

Permalink
make invocation exportable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsaw committed Jul 27, 2018
1 parent 94f46b4 commit 67f143b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions invocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" }`),
Expand All @@ -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" }`),
Expand Down
12 changes: 6 additions & 6 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -59,15 +59,15 @@ 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"
return out, err
}
return HandlerFunc(m)
})
res, err := r.Handle(invocation{
res, err := r.Handle(Invocation{
Resolve: "example.resolver",
Context: context{
Arguments: json.RawMessage(`{"bar":"foo"}`),
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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"}`),
Expand Down
2 changes: 1 addition & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`),
Expand All @@ -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"}`),
Expand All @@ -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"}`),
Expand All @@ -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"}`),
Expand Down

0 comments on commit 67f143b

Please sign in to comment.