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

Add Config support for specifying a PlaygroundEndpoint #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions graphcoolPlayground.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type playgroundData struct {
}

// renderPlayground renders the Playground GUI
func renderPlayground(w http.ResponseWriter, r *http.Request) {
func renderPlayground(w http.ResponseWriter, r *http.Request, endpoint string) {
t := template.New("Playground")
t, err := t.Parse(graphcoolPlaygroundTemplate)
if err != nil {
Expand All @@ -24,7 +24,7 @@ func renderPlayground(w http.ResponseWriter, r *http.Request) {

d := playgroundData{
PlaygroundVersion: graphcoolPlaygroundVersion,
Endpoint: r.URL.Path,
Endpoint: endpoint,
SubscriptionEndpoint: fmt.Sprintf("ws://%v/subscriptions", r.Host),
SetTitle: true,
}
Expand Down
51 changes: 29 additions & 22 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const (
type ResultCallbackFn func(ctx context.Context, params *graphql.Params, result *graphql.Result, responseBody []byte)

type Handler struct {
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
rootObjectFn RootObjectFn
resultCallbackFn ResultCallbackFn
formatErrorFn func(err error) gqlerrors.FormattedError
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
playgroundEndpoint string
rootObjectFn RootObjectFn
resultCallbackFn ResultCallbackFn
formatErrorFn func(err error) gqlerrors.FormattedError
}

type RequestOptions struct {
Expand Down Expand Up @@ -162,7 +163,11 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
acceptHeader := r.Header.Get("Accept")
_, raw := r.URL.Query()["raw"]
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
renderPlayground(w, r)
endpoint := h.playgroundEndpoint
if endpoint == "" {
endpoint = r.URL.Path
}
renderPlayground(w, r, endpoint)
return
}
}
Expand Down Expand Up @@ -197,13 +202,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type RootObjectFn func(ctx context.Context, r *http.Request) map[string]interface{}

type Config struct {
Schema *graphql.Schema
Pretty bool
GraphiQL bool
Playground bool
RootObjectFn RootObjectFn
ResultCallbackFn ResultCallbackFn
FormatErrorFn func(err error) gqlerrors.FormattedError
Schema *graphql.Schema
Pretty bool
GraphiQL bool
Playground bool
PlaygroundEndpoint string
RootObjectFn RootObjectFn
ResultCallbackFn ResultCallbackFn
FormatErrorFn func(err error) gqlerrors.FormattedError
}

func NewConfig() *Config {
Expand All @@ -225,12 +231,13 @@ func New(p *Config) *Handler {
}

return &Handler{
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
rootObjectFn: p.RootObjectFn,
resultCallbackFn: p.ResultCallbackFn,
formatErrorFn: p.FormatErrorFn,
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
playgroundEndpoint: p.PlaygroundEndpoint,
rootObjectFn: p.RootObjectFn,
resultCallbackFn: p.ResultCallbackFn,
formatErrorFn: p.FormatErrorFn,
}
}