@@ -99,6 +99,7 @@ <h3>Edit GraphQL Query</h3>
99
99
< button id ="loadComplexExample "> Load Complex Example</ button >
100
100
< a id ="endpointLink " href ="" target ="_blank "> View JSON on Endpoint</ a >
101
101
</ div >
102
+ < textarea id ="output " readonly placeholder ="Messages appear here "> </ textarea >
102
103
</ div >
103
104
< div id ="map "> </ div >
104
105
@@ -195,6 +196,8 @@ <h3>Edit GraphQL Query</h3>
195
196
196
197
const initialQuery = simpleQuery ;
197
198
199
+ const output = document . getElementById ( 'output' ) ;
200
+
198
201
// Initialize the Leaflet map
199
202
const map = L . map ( 'map' ) . setView ( [ 0 , 0 ] , 2 ) ;
200
203
@@ -205,17 +208,29 @@ <h3>Edit GraphQL Query</h3>
205
208
206
209
// Fetch data from the GraphQL API
207
210
async function fetchData ( query ) {
208
- const response = await fetch ( GRAPHQL_URL , {
209
- method : 'POST' ,
210
- headers : {
211
- 'Content-Type' : 'application/json' ,
212
- } ,
213
- body : JSON . stringify ( { query } )
214
- } ) ;
211
+ output . value = '[LOADING...]' ;
212
+ try {
213
+ const response = await fetch ( GRAPHQL_URL , {
214
+ method : 'POST' ,
215
+ headers : {
216
+ 'Content-Type' : 'application/json' ,
217
+ } ,
218
+ body : JSON . stringify ( { query } )
219
+ } ) ;
215
220
216
- const json = await response . json ( ) ;
217
- const locations = json . data . locations ;
218
- return locations ;
221
+ if ( ! response . ok ) {
222
+ const msg = await response . text ( ) ;
223
+ throw new Error ( `[ERROR] Server message: ${ msg } ` ) ;
224
+ }
225
+
226
+ const json = await response . json ( ) ;
227
+ output . value = '[SUCCESS]' ;
228
+ const locations = json . data . locations ;
229
+ return locations ;
230
+ } catch ( error ) {
231
+ output . value = error . message ;
232
+ throw new Error ( error ) ;
233
+ }
219
234
}
220
235
221
236
// Function to create HTML for the popup with all JSON keys and values
0 commit comments