async_graphql/http/
graphiql_plugin.rs

1//! A simplified html for GraphiQL v2 with explorer plugin
2//!
3//! ```html
4//! <!doctype html>
5//! <html lang="en">
6//!   <head>
7//!     <title>GraphiQL</title>
8//!     <!-- simplified ... -->
9//!     <!-- [head_assets]
10//!       These are imports for the GraphIQL Explorer plugin.
11//!      -->
12//!     <link rel="stylesheet" href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css" />
13//!     <!-- END [head_assets] -->
14//!   </head>
15//!   <body>
16//!     <!-- simplified ... -->
17//!
18//!     <!-- [body_assets]
19//!       These are imports for the GraphIQL Explorer plugin.
20//!     -->
21//!     <script
22//!       src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
23//!       crossorigin
24//!     ></script>
25//!     <!-- END [body_assets] -->
26//!
27//!     <script>
28//!       <!-- simplified ... -->
29//!
30//!       <!-- plugins block -->
31//!       const plugins = []
32//!
33//!       <!-- [pre_configs] -->
34//!       <!-- END [ppre_configs] -->
35//!
36//!       <!-- [constructor and props] -->
37//!       For explorerPlugin without any props:
38//!       const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin();
39//!       where `GraphiQLPluginExplorer.explorerPlugin` is the constructor of plugin
40//!
41//!       For explorerPlugin with props `{hideActions: false}`:
42//!       const explorerPlugin = GraphiQLPluginExplorer.explorerPlugin({hideActions: false});
43//!       -->
44//!       plugins.push(GraphiQLPluginExplorer.explorerPlugin());
45//!       <!-- END [constructor and props] -->
46//!       <!-- END plugins block -->
47//!
48//!       root.render(
49//!         React.createElement(GraphiQL, {
50//!           fetcher,
51//!           defaultEditorToolsVisibility: true,
52//!           plugins,
53//!         }),
54//!       );
55//!     </script>
56//!   </body>
57//! </html>
58//! ```
59//!
60//! Example for explorer plugin
61//!
62//! ```rust, ignore
63//! GraphiQLPlugin {
64//!     name: "GraphiQLPluginExplorer",
65//!     constructor: "GraphiQLPluginExplorer.explorerPlugin",
66//!     head_assets: Some(
67//!         r#"<link rel="stylesheet" href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css" />"#,
68//!     ),
69//!     body_assets: Some(
70//!         r#"<script
71//!   src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
72//!   crossorigin
73//! ></script>"#,
74//!     ),
75//!     ..Default::default()
76//! }
77//! ```
78
79use serde::Serialize;
80
81#[allow(missing_docs)]
82#[derive(Debug, Default, Serialize)]
83pub struct GraphiQLPlugin<'a> {
84    pub name: &'a str,
85    pub constructor: &'a str,
86    /// assets which would be placed in head
87    pub head_assets: Option<&'a str>,
88    /// assets which would be placed in body
89    pub body_assets: Option<&'a str>,
90    /// related configs which would be placed before loading plugin
91    pub pre_configs: Option<&'a str>,
92    /// props which would be passed to the plugin's constructor
93    pub props: Option<&'a str>,
94}
95
96/// Generate simple explorer plugin for GraphiQL (v2)
97pub fn graphiql_plugin_explorer<'a>() -> GraphiQLPlugin<'a> {
98    GraphiQLPlugin {
99        name: "GraphiQLPluginExplorer",
100        constructor: "GraphiQLPluginExplorer.explorerPlugin",
101        head_assets: Some(
102            r#"<link rel="stylesheet" href="https://unpkg.com/@graphiql/plugin-explorer/dist/style.css" />"#,
103        ),
104        body_assets: Some(
105            r#"<script
106      src="https://unpkg.com/@graphiql/plugin-explorer/dist/index.umd.js"
107      crossorigin
108    ></script>"#,
109        ),
110        ..Default::default()
111    }
112}