diff --git a/graphiql.go b/graphiql.go index d997374..b6987f2 100644 --- a/graphiql.go +++ b/graphiql.go @@ -4,21 +4,27 @@ import ( "encoding/json" "html/template" "net/http" + "strings" "github.com/graphql-go/graphql" ) // page is the page data structure of the rendered GraphiQL page type graphiqlPage struct { - GraphiqlVersion string - QueryString string - ResultString string - VariablesString string - OperationName string + GraphiqlVersion string + SubscriptionTransportVersion string + QueryString string + ResultString string + VariablesString string + OperationName string + Endpoint template.URL + SubscriptionEndpoint template.URL + UsingHTTP bool + UsingWS bool } // renderGraphiQL renders the GraphiQL GUI -func renderGraphiQL(w http.ResponseWriter, params graphql.Params) { +func renderGraphiQL(w http.ResponseWriter, params graphql.Params, handler Handler) { t := template.New("GraphiQL") t, err := t.Parse(graphiqlTemplate) if err != nil { @@ -50,12 +56,29 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params) { resString = string(result) } + isEndpointUsingWS := strings.HasPrefix(handler.Endpoint, "ws://") + UsingHTTP := !isEndpointUsingWS + UsingWS := isEndpointUsingWS || handler.SubscriptionsEndpoint != "" + SubscriptionEndpoint := "" + if UsingWS { + if isEndpointUsingWS { + SubscriptionEndpoint = handler.Endpoint + } else { + SubscriptionEndpoint = handler.SubscriptionsEndpoint + } + } + p := graphiqlPage{ - GraphiqlVersion: graphiqlVersion, - QueryString: params.RequestString, - ResultString: resString, - VariablesString: varsString, - OperationName: params.OperationName, + GraphiqlVersion: graphiqlVersion, + SubscriptionTransportVersion: subscriptionTransportVersion, + QueryString: params.RequestString, + ResultString: resString, + VariablesString: varsString, + OperationName: params.OperationName, + Endpoint: template.URL(handler.Endpoint), + SubscriptionEndpoint: template.URL(SubscriptionEndpoint), + UsingHTTP: UsingHTTP, + UsingWS: UsingWS, } err = t.ExecuteTemplate(w, "index", p) @@ -68,6 +91,9 @@ func renderGraphiQL(w http.ResponseWriter, params graphql.Params) { // graphiqlVersion is the current version of GraphiQL const graphiqlVersion = "0.11.10" +// subscriptionTransportVersion is the current version of the subscription transport of GraphiQL +const subscriptionTransportVersion = "0.8.2" + // tmpl is the page template to render GraphiQL const graphiqlTemplate = ` {{ define "index" }} @@ -94,10 +120,20 @@ add "&raw" to the end of the URL within a browser. } - + + {{ if .UsingHTTP }} + + {{ end }} + {{ if .UsingWS }} + + {{ end }} + {{ if and .UsingWS .UsingHTTP }} + + {{ end }} +