Skip to content

Commit

Permalink
added viewer field to auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Feb 10, 2019
1 parent 569eb4a commit 9bdc8d3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ import (
viewerField := &gateway.QueryField{
Name: "viewer",
Type: ast.NamedType("User", &ast.Position{}),
Resolver: func(ctx context.Context, args ast.ArgumentList, variables map[string]interface{}) (string, error) {
Resolver: func(ctx context.Context, args variables map[string]interface{}) (string, error) {
// for now just return the value in context
return context.value("user-id").(string), nil
return ctx.Value("user-id").(string), nil
},
}

Expand Down
4 changes: 2 additions & 2 deletions examples/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ The general flow goes something like:

- The other services [uses the header value](https://github.com/nautilus/gateway/blob/master/examples/auth/todo.go#L89) to perform whatever user-specific logic is
required.
- The current user can query for their User record with the [viewer gateway field]() (coming soon)

- The current user can query for their User record with the [viewer gateway field](https://github.com/nautilus/gateway/blob/master/examples/auth/gateway.go#L50-L58) (coming soon)

Keep in mind that this demo should not be taken as an example of a secure
authorization system. Its purpose is just to illustrate how one can pass
Expand Down
14 changes: 13 additions & 1 deletion examples/auth/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/nautilus/gateway"
"github.com/nautilus/graphql"
"github.com/vektah/gqlparser/ast"
)

// the first thing we need to define is a middleware for our handler
Expand Down Expand Up @@ -44,6 +45,17 @@ var forwardUserID = gateway.RequestMiddleware(func(r *http.Request) error {
return nil
})

// we can also define a field at the root of the API in order to resolve fields
// for the current user
var viewerField = &gateway.QueryField{
Name: "viewer",
Type: ast.NamedType("User", &ast.Position{}),
Resolver: func(ctx context.Context, args map[string]interface{}) (string, error) {
// for now just return the value in context
return ctx.Value("user-id").(string), nil
},
}

func main() {
// introspect the apis
schemas, err := graphql.IntrospectRemoteSchemas(
Expand All @@ -55,7 +67,7 @@ func main() {
}

// create the gateway instance
gw, err := gateway.New(schemas, gateway.WithMiddlewares(forwardUserID))
gw, err := gateway.New(schemas, gateway.WithMiddlewares(forwardUserID), gateway.WithQueryFields(viewerField))
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 9bdc8d3

Please sign in to comment.