rusty_gql/
playground_html.rs

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