diff --git a/README.md b/README.md index 1ffde4f..0d46413 100644 --- a/README.md +++ b/README.md @@ -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 }, } diff --git a/examples/auth/README.md b/examples/auth/README.md index 60e2dfe..e024339 100644 --- a/examples/auth/README.md +++ b/examples/auth/README.md @@ -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 diff --git a/examples/auth/gateway.go b/examples/auth/gateway.go index 81b4988..5aaf92a 100644 --- a/examples/auth/gateway.go +++ b/examples/auth/gateway.go @@ -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 @@ -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( @@ -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) }