Skip to content

Commit 593f69e

Browse files
committed
Allow custom endpoint URL
1 parent cc93f95 commit 593f69e

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

graphiql.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ type graphiqlPage struct {
1515
ResultString string
1616
VariablesString string
1717
OperationName string
18+
EndpointURL template.URL
1819
}
1920

2021
// renderGraphiQL renders the GraphiQL GUI
21-
func renderGraphiQL(w http.ResponseWriter, params graphql.Params) {
22+
func renderGraphiQL(w http.ResponseWriter, params graphql.Params, handler Handler) {
2223
t := template.New("GraphiQL")
2324
t, err := t.Parse(graphiqlTemplate)
2425
if err != nil {
@@ -56,6 +57,7 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params) {
5657
ResultString: resString,
5758
VariablesString: varsString,
5859
OperationName: params.OperationName,
60+
EndpointURL: template.URL(handler.EndpointURL),
5961
}
6062

6163
err = t.ExecuteTemplate(w, "index", p)
@@ -134,7 +136,7 @@ add "&raw" to the end of the URL within a browser.
134136
otherParams[k] = parameters[k];
135137
}
136138
}
137-
var fetchURL = locationQuery(otherParams);
139+
var fetchURL = locationQuery(otherParams, {{ .EndpointURL }});
138140
139141
// Defines a GraphQL fetcher using the fetch API.
140142
function graphQLFetcher(graphQLParams) {

handler.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ const (
1919
)
2020

2121
type Handler struct {
22-
Schema *graphql.Schema
23-
pretty bool
24-
graphiql bool
22+
Schema *graphql.Schema
23+
pretty bool
24+
graphiql bool
25+
EndpointURL string
2526
}
2627
type RequestOptions struct {
2728
Query string `json:"query" url:"query" schema:"query"`
@@ -133,7 +134,7 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
133134
acceptHeader := r.Header.Get("Accept")
134135
_, raw := r.URL.Query()["raw"]
135136
if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") {
136-
renderGraphiQL(w, params)
137+
renderGraphiQL(w, params, *h)
137138
return
138139
}
139140
}
@@ -160,16 +161,18 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
160161
}
161162

162163
type Config struct {
163-
Schema *graphql.Schema
164-
Pretty bool
165-
GraphiQL bool
164+
Schema *graphql.Schema
165+
Pretty bool
166+
GraphiQL bool
167+
EndpointURL string
166168
}
167169

168170
func NewConfig() *Config {
169171
return &Config{
170-
Schema: nil,
171-
Pretty: true,
172-
GraphiQL: true,
172+
Schema: nil,
173+
Pretty: true,
174+
GraphiQL: true,
175+
EndpointURL: "/",
173176
}
174177
}
175178

@@ -182,8 +185,9 @@ func New(p *Config) *Handler {
182185
}
183186

184187
return &Handler{
185-
Schema: p.Schema,
186-
pretty: p.Pretty,
187-
graphiql: p.GraphiQL,
188+
Schema: p.Schema,
189+
pretty: p.Pretty,
190+
graphiql: p.GraphiQL,
191+
EndpointURL: p.EndpointURL,
188192
}
189193
}

0 commit comments

Comments
 (0)