1- // Parse the search string to get url parameters.
2- var search = window . location . search ;
3- var parameters = { } ;
4- search . substr ( 1 ) . split ( '&' ) . forEach ( function ( entry ) {
5- var eq = entry . indexOf ( '=' ) ;
6- if ( eq >= 0 ) {
7- parameters [ decodeURIComponent ( entry . slice ( 0 , eq ) ) ] =
8- decodeURIComponent ( entry . slice ( eq + 1 ) ) ;
9- }
10- } ) ;
11-
12- // If variables was provided, try to format it.
13- if ( parameters . variables ) {
14- try {
15- parameters . variables =
16- JSON . stringify ( JSON . parse ( parameters . variables ) , null , 2 ) ;
17- } catch ( e ) {
18- // Do nothing, we want to display the invalid JSON as a string, rather
19- // than present an error.
20- }
21- }
1+ var initParameters = { } ;
2+ var entrypoint = null ;
223
23- // When the query and variables string is edited, update the URL bar so
24- // that it can be easily shared
254function onEditQuery ( newQuery ) {
26- parameters . query = newQuery ;
5+ initParameters . query = newQuery ;
276 updateURL ( ) ;
287}
298
309function onEditVariables ( newVariables ) {
31- parameters . variables = newVariables ;
10+ initParameters . variables = newVariables ;
3211 updateURL ( ) ;
3312}
3413
3514function onEditOperationName ( newOperationName ) {
36- parameters . operationName = newOperationName ;
15+ initParameters . operationName = newOperationName ;
3716 updateURL ( ) ;
3817}
3918
4019function updateURL ( ) {
41- var newSearch = '?' + Object . keys ( parameters ) . filter ( function ( key ) {
42- return Boolean ( parameters [ key ] ) ;
20+ var newSearch = '?' + Object . keys ( initParameters ) . filter ( function ( key ) {
21+ return Boolean ( initParameters [ key ] ) ;
4322 } ) . map ( function ( key ) {
44- return encodeURIComponent ( key ) + '=' +
45- encodeURIComponent ( parameters [ key ] ) ;
23+ return encodeURIComponent ( key ) + '=' + encodeURIComponent ( initParameters [ key ] ) ;
4624 } ) . join ( '&' ) ;
4725 history . replaceState ( null , null , newSearch ) ;
4826}
4927
50- // Defines a GraphQL fetcher using the fetch API.
5128function graphQLFetcher ( graphQLParams ) {
52- return fetch ( window . location . pathname , {
29+ return fetch ( entrypoint , {
5330 method : 'post' ,
5431 headers : {
5532 'Accept' : 'application/json' ,
56- 'Content-Type' : 'application/json' ,
33+ 'Content-Type' : 'application/json'
5734 } ,
5835 body : JSON . stringify ( graphQLParams ) ,
59- credentials : 'include' ,
36+ credentials : 'include'
6037 } ) . then ( function ( response ) {
6138 return response . text ( ) ;
6239 } ) . then ( function ( responseBody ) {
@@ -68,16 +45,36 @@ function graphQLFetcher(graphQLParams) {
6845 } ) ;
6946}
7047
71- // Render <GraphiQL /> into the body.
72- ReactDOM . render (
73- React . createElement ( GraphiQL , {
74- fetcher : graphQLFetcher ,
75- query : parameters . query ,
76- variables : parameters . variables ,
77- operationName : parameters . operationName ,
78- onEditQuery : onEditQuery ,
79- onEditVariables : onEditVariables ,
80- onEditOperationName : onEditOperationName
81- } ) ,
82- document . getElementById ( 'graphiql' )
83- ) ;
48+ window . onload = function ( ) {
49+ var data = JSON . parse ( document . getElementById ( 'graphiql-data' ) . innerText ) ;
50+ entrypoint = data . entrypoint ;
51+
52+ var search = window . location . search ;
53+ search . substr ( 1 ) . split ( '&' ) . forEach ( function ( entry ) {
54+ var eq = entry . indexOf ( '=' ) ;
55+ if ( eq >= 0 ) {
56+ initParameters [ decodeURIComponent ( entry . slice ( 0 , eq ) ) ] = decodeURIComponent ( entry . slice ( eq + 1 ) ) ;
57+ }
58+ } ) ;
59+
60+ if ( initParameters . variables ) {
61+ try {
62+ initParameters . variables = JSON . stringify ( JSON . parse ( initParameters . variables ) , null , 2 ) ;
63+ } catch ( e ) {
64+ // Do nothing, we want to display the invalid JSON as a string, rather than present an error.
65+ }
66+ }
67+
68+ ReactDOM . render (
69+ React . createElement ( GraphiQL , {
70+ fetcher : graphQLFetcher ,
71+ query : initParameters . query ,
72+ variables : initParameters . variables ,
73+ operationName : initParameters . operationName ,
74+ onEditQuery : onEditQuery ,
75+ onEditVariables : onEditVariables ,
76+ onEditOperationName : onEditOperationName
77+ } ) ,
78+ document . getElementById ( 'graphiql' )
79+ ) ;
80+ }
0 commit comments