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