Skip to content

Commit

Permalink
Updated example in docs to use handler.GetInitPayload instead of grap…
Browse files Browse the repository at this point in the history
…hql.GetInitPayload
  • Loading branch information
gissleh committed Sep 20, 2018
1 parent 32f0b84 commit 3147d91
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions docs/content/recipes/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ func (r *queryResolver) Hero(ctx context.Context, episode Episode) (Character, e
}
```

Things are different with websockets, and if you do things in the vein of the above example, you have to compute this at every call to `auth.ForContext`. It's a bit inefficient if you have multiple calls to this function. What you might do to mitigate that is to have a session manager on the context and have that check the vailidity in the first `auth.ForContext` call.
Things are different with websockets, and if you do things in the vein of the above example, you have to compute this at every call to `auth.ForContext`.

```golang
// ForContext finds the user from the context. REQUIRES Middleware to have run.
func ForContext(ctx context.Context) *User {
raw, ok := ctx.Value(userCtxKey).(*User)

if !ok {
payload := graphql.GetInitPayload(ctx)
payload := handler.GetInitPayload(ctx)
if payload == nil {
return nil
}
Expand All @@ -137,4 +137,6 @@ func ForContext(ctx context.Context) *User {

return raw
}
```
```

It's a bit inefficient if you have multiple calls to this function (e.g. on a field resolver), but what you might do to mitigate that is to have a session object set on the http request and only populate it upon the first check.

0 comments on commit 3147d91

Please sign in to comment.