async_graphql/http/
graphiql_source.rs

1/// Generate the page for GraphIQL
2pub fn graphiql_source(graphql_endpoint_url: &str, subscription_endpoint: Option<&str>) -> String {
3    r#"
4    <html>
5  <head>
6    <title>Simple GraphiQL Example</title>
7    <link href="https://unpkg.com/graphiql@1/graphiql.min.css" rel="stylesheet" />
8  </head>
9  <body style="margin: 0;">
10    <div id="graphiql" style="height: 100vh;"></div>
11
12    <script src="//unpkg.com/subscriptions-transport-ws@0.8.3/browser/client.js"></script>
13    <script src="//unpkg.com/graphiql-subscriptions-fetcher@0.0.2/browser/client.js"></script>
14    <script
15      crossorigin
16      src="https://unpkg.com/react@17/umd/react.production.min.js"
17    ></script>
18    <script
19      crossorigin
20      src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"
21    ></script>
22    <script
23      crossorigin
24      src="https://unpkg.com/graphiql@1/graphiql.min.js"
25    ></script>
26
27    <script>
28      var fetcher = graphQLParams =>
29        fetch('GRAPHQL_URL', {
30          method: 'post',
31          headers: { 'Content-Type': 'application/json' },
32          body: JSON.stringify(graphQLParams),
33        })
34          .then(response => response.json())
35          .catch(() => response.text());
36
37      var subscription_url = GRAPHQL_SUBSCRIPTION_URL;
38
39      if (subscription_url) {
40        var subscriptionClient = new window.SubscriptionsTransportWs.SubscriptionClient(GRAPHQL_SUBSCRIPTION_URL, { reconnect: true });
41        fetcher = window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionClient, fetcher);
42      }
43
44      ReactDOM.render(
45        React.createElement(GraphiQL, { fetcher }),
46        document.getElementById('graphiql'),
47      );
48    </script>
49  </body>
50</html>
51    "#
52    .replace("GRAPHQL_URL", graphql_endpoint_url)
53    .replace(
54        "GRAPHQL_SUBSCRIPTION_URL",
55        &match subscription_endpoint {
56            Some(url) => format!("'{}'", url),
57            None => "null".to_string(),
58        },
59    )
60}