@@ -69,6 +69,11 @@ function graphQLFetcher(graphQLParams) {
6969 } )
7070}
7171
72+ const fetchFragments = async ( ) =>
73+ fetch ( `/___graphiql/fragments` )
74+ . catch ( err => console . error ( `Error fetching external fragments: \n${ err } ` ) )
75+ . then ( response => response . json ( ) )
76+
7277// When the query and variables string is edited, update the URL bar so
7378// that it can be easily shared.
7479function onEditVariables ( newVariables ) {
@@ -166,6 +171,7 @@ const storedCodeExporterPaneState =
166171class App extends React . Component {
167172 state = {
168173 schema : null ,
174+ externalFragments : null ,
169175 query : DEFAULT_QUERY ,
170176 variables : DEFAULT_VARIABLES ,
171177 explorerIsOpen : storedExplorerPaneState ,
@@ -316,11 +322,21 @@ class App extends React.Component {
316322 Authorization : this . state . refreshToken ,
317323 }
318324 }
325+ fetchFragments ( ) . then ( externalFragments => {
326+ this . setState ( { externalFragments } )
327+ } )
328+
319329 return fetch ( `/__refresh` , options )
320330 }
321331
322332 render ( ) {
323- const { query, variables, schema, codeExporterIsOpen } = this . state
333+ const {
334+ query,
335+ variables,
336+ schema,
337+ codeExporterIsOpen,
338+ externalFragments : externalFragmentsState ,
339+ } = this . state
324340 const { externalFragments } = this . props
325341 const codeExporter = codeExporterIsOpen ? (
326342 < CodeExporter
@@ -352,7 +368,7 @@ class App extends React.Component {
352368 onEditQuery = { this . _handleEditQuery }
353369 onEditVariables = { onEditVariables }
354370 onEditOperationName = { onEditOperationName }
355- externalFragments = { externalFragments }
371+ externalFragments = { externalFragmentsState || externalFragments }
356372 >
357373 < GraphiQL . Toolbar >
358374 < GraphiQL . Button
@@ -393,11 +409,9 @@ class App extends React.Component {
393409// crude way to fetch fragments on boot time
394410// it won't hot reload fragments (refresh requires)
395411// but good enough for initial integration
396- fetch ( `/___graphiql/fragments` )
397- . then ( response => response . json ( ) )
398- . then ( externalFragments => {
399- ReactDOM . render (
400- < App externalFragments = { externalFragments } /> ,
401- document . getElementById ( `root` )
402- )
403- } )
412+ fetchFragments ( ) . then ( externalFragments => {
413+ ReactDOM . render (
414+ < App externalFragments = { externalFragments } /> ,
415+ document . getElementById ( `root` )
416+ )
417+ } )
0 commit comments