|
1 | 1 | <!doctype html> |
2 | 2 | <html lang="en"> |
3 | 3 | <head> |
4 | | - <meta charset="UTF-8"> |
| 4 | + <meta charset="utf-8" /> |
5 | 5 | </head> |
6 | 6 | <body> |
7 | 7 | <button id="button">Load posts</button> |
8 | | - <script src="../lib/json-graphql-server.min.js"></script> |
| 8 | + <script src="../lib/json-graphql-server.client.min.js"></script> |
9 | 9 | <script type="text/javascript"> |
10 | 10 | window.addEventListener('load', function() { |
11 | 11 | const data = { |
|
35 | 35 | ], |
36 | 36 | }; |
37 | 37 |
|
38 | | - try { |
39 | | - const server = GraphQLClientServer({ data, url: "http://localhost:3000/graphql" }); |
40 | | - server.start(); |
41 | | - } catch(error) { |
42 | | - console.error({error}); |
43 | | - } |
44 | | - |
45 | | - window.document.getElementById('button').addEventListener('click', function () { |
46 | | - const xhr = new XMLHttpRequest(); |
47 | | - xhr.open("POST", "http://localhost:3000/graphql", true); |
48 | | - xhr.setRequestHeader("Content-Type", "application/json"); |
49 | | - xhr.setRequestHeader("Accept", "application/json"); |
50 | | - xhr.onerror = function(error) { |
51 | | - console.error(error); |
52 | | - } |
53 | | - xhr.onload = function() { |
54 | | - const result = JSON.parse(xhr.responseText); |
55 | | - console.log('data returned:', result); |
56 | | - alert('Found ' + result.data.allPosts.length + ' posts'); |
57 | | - } |
58 | | - const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' }); |
59 | | - xhr.send(body); |
| 38 | + const server = JsonGraphqlServer({ |
| 39 | + data, |
| 40 | + url: 'http://localhost:3000/graphql' |
60 | 41 | }); |
| 42 | + |
| 43 | + server.start(); |
| 44 | + }); |
| 45 | + window.document.getElementById('button').addEventListener('click', function () { |
| 46 | + const xhr = new XMLHttpRequest(); |
| 47 | + xhr.open("POST", "http://localhost:3000/graphql", true); |
| 48 | + xhr.setRequestHeader("Content-Type", "application/json"); |
| 49 | + xhr.setRequestHeader("Accept", "application/json"); |
| 50 | + xhr.onerror = function(error) { |
| 51 | + console.error(error); |
| 52 | + } |
| 53 | + xhr.onload = function() { |
| 54 | + const result = JSON.parse(xhr.responseText); |
| 55 | + console.log('data returned:', result); |
| 56 | + alert('Found ' + result.data.allPosts.length + ' posts'); |
| 57 | + } |
| 58 | + const body = JSON.stringify({ query: 'query allPosts { allPosts { id } }' }); |
| 59 | + xhr.send(body); |
61 | 60 | }); |
62 | 61 | </script> |
63 | 62 | </body> |
|
0 commit comments