Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Interface #11

Open
ghost opened this issue Jun 22, 2016 · 2 comments
Open

Network Interface #11

ghost opened this issue Jun 22, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 22, 2016

For authorization I need to send tokens inside an optional header configuration exposed on the graphql server. Unless I'm blind, or it's meant for the graphql-go package and not this one, I can't see it anywhere.

@leocavalcante
Copy link

leocavalcante commented Aug 7, 2016

@omgj, you can use a custom handler to have access for *http.Request so you can get the Authorization header. I'll be using this solution for JWT.

Just did a test case, hope it helps:
https://github.com/leocavalcante/GoGraphQwt/blob/master/main.go

@yookoala
Copy link
Contributor

yookoala commented Aug 24, 2017

After #24, you can implement a middleware (something like chi.Middleware or alice.Constructor), parse anything in the http.Request and pass along as context.Context with http.Request.WithContext().

For example,

import (
  "context"
  "net/http"

  "github.com/graphql-go/handler"
)

func main() {
  // define GraphQL schema using relay library helpers
  schema := graphql.NewSchema(...)

  // apply middleware
  h := func(inner http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      // some kind of authentication here
      user := parseJwtUser(r)
      if !user.canDoGraphQL() {
        // do something to tell the user
        // ...
        // ...
        return
      }

      // do normal graphql
      innerCtx := context.WithValue(r.Context(), "user", user)
      inner.ServeHTTP(w, r.WithContext(innerCtx))
    })
  }(handler.New(&handler.Config{
    Schema: &schema,
    Pretty: true,
  }))

  // serve it to port 8080
  http.Handle("/graphql", h)
  http.ListenAndServe(":8080", nil)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants