Skip to content

Commit

Permalink
Add middleware example for Todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Jul 26, 2018
1 parent 73a8e3a commit 5dc104e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
57 changes: 50 additions & 7 deletions example/todo/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/todo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ schema {

type MyQuery {
todo(id: Int!): Todo
authenticatedTodo(id: Int!): Todo @isAuthenticated
lastTodo: Todo
todos: [Todo!]!
}
Expand Down
30 changes: 23 additions & 7 deletions example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ import (
"time"

"github.com/mitchellh/mapstructure"
graphql "github.com/vektah/gqlgen/graphql"
)

func New() Config {
r := &resolvers{
todos: []Todo{
{ID: 1, Text: "A todo not to forget", Done: false},
{ID: 2, Text: "This is the most important", Done: false},
{ID: 3, Text: "Please do this or else", Done: false},
c := Config{
Resolvers: &resolvers{
todos: []Todo{
{ID: 1, Text: "A todo not to forget", Done: false},
{ID: 2, Text: "This is the most important", Done: false},
{ID: 3, Text: "Please do this or else", Done: false},
},
lastID: 3,
},
lastID: 3,
}
return Config{Resolvers: r}
c.Directives.IsAuthenticated = func(ctx context.Context, next graphql.Resolver) (interface{}, error) {
rctx := graphql.GetResolverContext(ctx)
idVal := rctx.Field.Arguments.ForName("id").Value
id, _ := idVal.Value(make(map[string]interface{}))
if id.(int64) == 1 {
return nil, nil
}
return next(ctx)
}
return c
}

type resolvers struct {
Expand Down Expand Up @@ -63,6 +75,10 @@ func (r *QueryResolver) Todos(ctx context.Context) ([]Todo, error) {
return r.todos, nil
}

func (r *QueryResolver) AuthenticatedTodo(ctx context.Context, id int) (*Todo, error) {
return r.Todo(ctx, id)
}

type MutationResolver resolvers

func (r *MutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (Todo, error) {
Expand Down
13 changes: 13 additions & 0 deletions example/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ func TestTodo(t *testing.T) {

require.Equal(t, "Completed todo", resp.CreateTodo.Text)
})

t.Run("isAuthenticated directive middleware", func(t *testing.T) {
var resp map[string]interface{}
c.MustPost(`{ authenticatedTodo(id: 1) { __typename } }`, &resp)
val, ok := resp["authenticatedTodo"]
require.True(t, ok)
require.Nil(t, val)

c.MustPost(`{ authenticatedTodo(id: 2) { __typename } }`, &resp)
val, ok = resp["authenticatedTodo"]
require.True(t, ok)
require.NotNil(t, val)
})
}

func TestSkipAndIncludeDirectives(t *testing.T) {
Expand Down

0 comments on commit 5dc104e

Please sign in to comment.