@@ -407,42 +407,36 @@ app.use('/graphql', jsonGraphqlExpress(data));
407407app .listen (PORT );
408408```
409409
410- ## Usage on client
410+ ## Usage in browser with XMLHttpRequest
411411
412- Install the module locally:
413-
414- ``` sh
415- npm install --save json-graphql-server
416- ```
412+ Useful when using XMLHttpRequest directly or libaries such as [ axios] ( https://www.npmjs.com/package/axios ) .
417413
418- Then use the ` graphQLClientServer ` function:
414+ ### Install with a script tag
419415
420- ``` js
421- import { graphQLClientServer } from ' json-graphql-server' ;
416+ Add a ` script ` tag referencing the library:
422417
423- const data = [... ];
418+ ``` html
419+ <script src =" ../lib/json-graphql-server.min.js" ></script >
420+ ```
424421
425- const server = GraphQLClientServer ({
426- data,
427- url: ' http://localhost:3000/graphql'
428- });
422+ It will expose the ` GraphQLClientServer ` as a global object:
429423
430- server .start ();
431- ```
424+ ``` html
425+ <script type =" text/javascript" >
426+ window .addEventListener (' load' , function () {
427+ const data = [... ];
432428
433- This will intercepts all XMLHttpRequests and make them responds like a GraphQL server when the url matches the one specified.
429+ const server = GraphQLClientServer ({
430+ data,
431+ url: ' http://localhost:3000/graphql'
432+ });
434433
435- For example:
434+ server . start ();
436435
437- ``` js
438- window .document
439- .getElementById (' btnLoadPosts' )
440- .addEventListener (' click' , function () {
441- var xhr = new XMLHttpRequest ();
442- xhr .responseType = ' json' ;
443- xhr .open (" POST" , " http://localhost:3000/graphql" , true );
444- xhr .setRequestHeader (" Content-Type" , " application/json" );
445- xhr .setRequestHeader (" Accept" , " application/json" );
436+ const xhr = new XMLHttpRequest ();
437+ xhr .open (' POST' , ' http://localhost:3000/graphql' , true );
438+ xhr .setRequestHeader (' Content-Type' , ' application/json' );
439+ xhr .setRequestHeader (' Accept' , ' application/json' );
446440 xhr .onerror = function (error ) {
447441 console .error (error);
448442 }
@@ -454,31 +448,63 @@ window.document
454448 const body = JSON .stringify ({ query: ' query allPosts { allPosts { id } }' });
455449 xhr .send (body);
456450 });
451+ </script >
457452```
458453
459- ## Usage in browser through script
454+ ### Use with a bundler (webpack)
460455
461- Add a ` script ` tag referencing the library:
462-
463- ``` html
464- <script src =" ../lib/json-graphql-server.min.js" ></script >
456+ ``` sh
457+ npm install json-graphql-server
465458```
466459
467- It will expose the ` GraphQLClientServer ` as a global object:
460+ ``` js
461+ import GraphQLClientServer from ' json-graphql-server' ;
468462
469- ``` html
470- <script type =" text/javascript" >
471- window .addEventListener (' load' , function () {
472- const data = [... ];
463+ const data = [... ];
473464
474- const server = GraphQLClientServer ({
475- data,
476- url: ' http://localhost:3000/graphql'
477- });
465+ const server = GraphQLClientServer ({
466+ data,
467+ url: ' http://localhost:3000/graphql'
468+ });
478469
479- server .start ();
480- });
481- </script >
470+ server .start ();
471+
472+ const xhr = new XMLHttpRequest ();
473+ xhr .open (' POST' , ' http://localhost:3000/graphql' , true );
474+ xhr .setRequestHeader (' Content-Type' , ' application/json' );
475+ xhr .setRequestHeader (' Accept' , ' application/json' );
476+ xhr .onerror = function (error ) {
477+ console .error (error);
478+ }
479+ xhr .onload = function () {
480+ const result = JSON .parse (xhr .responseText );
481+ console .log (' data returned:' , result);
482+ alert (' Found ' + result .data .allPosts .length + ' posts' );
483+ }
484+ const body = JSON .stringify ({ query: ' query allPosts { allPosts { id } }' });
485+ xhr .send (body);
486+ ```
487+
488+ ## Usage in browser with fetch
489+
490+ ``` js
491+ import fetchMock from ' fetch-mock' ;
492+ import GraphQLClientServer from ' json-graphql-server' ;
493+
494+ const data = [... ];
495+ const server = GraphQLClientServer ({ data });
496+
497+ fetchMock .post (' http://localhost:3000/graphql' , server .getHandler ());
498+
499+ fetch ({
500+ url: ' http://localhost:3000/graphql' ,
501+ method: ' POST' ,
502+ body: JSON .stringify ({ query: ' query allPosts { allPosts { id } }' })
503+ })
504+ .then (response => response .json ())
505+ .then (json => {
506+ alert (' Found ' + result .data .allPosts .length + ' posts' );
507+ })
482508```
483509
484510## Adding Authentication, Custom Routes, etc.
0 commit comments