github.com/shippio/gqlgen@v0.0.0-20220912092219-633ea699ef07/graphql/playground/playground.go (about) 1 package playground 2 3 import ( 4 "html/template" 5 "net/http" 6 "net/url" 7 ) 8 9 var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html> 10 <html> 11 <head> 12 <meta charset="utf-8"> 13 <title>{{.title}}</title> 14 <style> 15 body { 16 height: 100%; 17 margin: 0; 18 width: 100%; 19 overflow: hidden; 20 } 21 22 #graphiql { 23 height: 100vh; 24 } 25 </style> 26 <script 27 src="https://cdn.jsdelivr.net/npm/react@17.0.2/umd/react.production.min.js" 28 integrity="{{.reactSRI}}" 29 crossorigin="anonymous" 30 ></script> 31 <script 32 src="https://cdn.jsdelivr.net/npm/react-dom@17.0.2/umd/react-dom.production.min.js" 33 integrity="{{.reactDOMSRI}}" 34 crossorigin="anonymous" 35 ></script> 36 <link 37 rel="stylesheet" 38 href="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.css" 39 integrity="{{.cssSRI}}" 40 crossorigin="anonymous" 41 /> 42 </head> 43 <body> 44 <div id="graphiql">Loading...</div> 45 46 <script 47 src="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.js" 48 integrity="{{.jsSRI}}" 49 crossorigin="anonymous" 50 ></script> 51 52 <script> 53 {{- if .endpointIsAbsolute}} 54 const url = {{.endpoint}}; 55 const subscriptionUrl = {{.subscriptionEndpoint}}; 56 {{- else}} 57 const url = location.protocol + '//' + location.host + {{.endpoint}}; 58 const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:'; 59 const subscriptionUrl = wsProto + '//' + location.host + {{.endpoint}}; 60 {{- end}} 61 62 const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl }); 63 ReactDOM.render( 64 React.createElement(GraphiQL, { 65 fetcher: fetcher, 66 isHeadersEditorEnabled: true, 67 shouldPersistHeaders: true 68 }), 69 document.getElementById('graphiql'), 70 ); 71 </script> 72 </body> 73 </html> 74 `)) 75 76 // Handler responsible for setting up the playground 77 func Handler(title string, endpoint string) http.HandlerFunc { 78 return func(w http.ResponseWriter, r *http.Request) { 79 w.Header().Add("Content-Type", "text/html; charset=UTF-8") 80 err := page.Execute(w, map[string]interface{}{ 81 "title": title, 82 "endpoint": endpoint, 83 "endpointIsAbsolute": endpointHasScheme(endpoint), 84 "subscriptionEndpoint": getSubscriptionEndpoint(endpoint), 85 "version": "2.0.1", 86 "cssSRI": "sha256-hYUgpHapGug0ucdB5kG0zSipubcQOJcGjclIZke2rl8=", 87 "jsSRI": "sha256-jMXGO5+Y4OhcHPSR34jpzpzlz4OZTlxcvaDXSWmUMRo=", 88 "reactSRI": "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=", 89 "reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=", 90 }) 91 if err != nil { 92 panic(err) 93 } 94 } 95 } 96 97 // endpointHasScheme checks if the endpoint has a scheme. 98 func endpointHasScheme(endpoint string) bool { 99 u, err := url.Parse(endpoint) 100 return err == nil && u.Scheme != "" 101 } 102 103 // getSubscriptionEndpoint returns the subscription endpoint for the given 104 // endpoint if it is parsable as a URL, or an empty string. 105 func getSubscriptionEndpoint(endpoint string) string { 106 u, err := url.Parse(endpoint) 107 if err != nil { 108 return "" 109 } 110 111 switch u.Scheme { 112 case "https": 113 u.Scheme = "wss" 114 default: 115 u.Scheme = "ws" 116 } 117 118 return u.String() 119 }