deploy: a3fda7363d
This commit is contained in:
parent
c591e35435
commit
7ab19efb9d
109 changed files with 5199 additions and 20535 deletions
0
.nojekyll
Normal file
0
.nojekyll
Normal file
|
@ -1 +0,0 @@
|
||||||
This file makes sure that Github Pages doesn't process mdBook's output.
|
|
4
current/FontAwesome/css/font-awesome.css
vendored
4
current/FontAwesome/css/font-awesome.css
vendored
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load diff
Before Width: | Height: | Size: 434 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
||||||
0.15.0
|
|
|
@ -1,79 +0,0 @@
|
||||||
/*
|
|
||||||
Based off of the Ayu theme
|
|
||||||
Original by Dempfi (https://github.com/dempfi/ayu)
|
|
||||||
*/
|
|
||||||
|
|
||||||
.hljs {
|
|
||||||
display: block;
|
|
||||||
overflow-x: auto;
|
|
||||||
background: #191f26;
|
|
||||||
color: #e6e1cf;
|
|
||||||
padding: 0.5em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-comment,
|
|
||||||
.hljs-quote {
|
|
||||||
color: #5c6773;
|
|
||||||
font-style: italic;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-variable,
|
|
||||||
.hljs-template-variable,
|
|
||||||
.hljs-attribute,
|
|
||||||
.hljs-attr,
|
|
||||||
.hljs-regexp,
|
|
||||||
.hljs-link,
|
|
||||||
.hljs-selector-id,
|
|
||||||
.hljs-selector-class {
|
|
||||||
color: #ff7733;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-number,
|
|
||||||
.hljs-meta,
|
|
||||||
.hljs-builtin-name,
|
|
||||||
.hljs-literal,
|
|
||||||
.hljs-type,
|
|
||||||
.hljs-params {
|
|
||||||
color: #ffee99;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-string,
|
|
||||||
.hljs-bullet {
|
|
||||||
color: #b8cc52;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-title,
|
|
||||||
.hljs-built_in,
|
|
||||||
.hljs-section {
|
|
||||||
color: #ffb454;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-keyword,
|
|
||||||
.hljs-selector-tag,
|
|
||||||
.hljs-symbol {
|
|
||||||
color: #ff7733;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-name {
|
|
||||||
color: #36a3d9;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-tag {
|
|
||||||
color: #00568d;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-emphasis {
|
|
||||||
font-style: italic;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-strong {
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-addition {
|
|
||||||
color: #91b362;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-deletion {
|
|
||||||
color: #d96c75;
|
|
||||||
}
|
|
660
current/book.js
660
current/book.js
|
@ -1,660 +0,0 @@
|
||||||
"use strict";
|
|
||||||
|
|
||||||
// Fix back button cache problem
|
|
||||||
window.onunload = function () { };
|
|
||||||
|
|
||||||
// Global variable, shared between modules
|
|
||||||
function playground_text(playground) {
|
|
||||||
let code_block = playground.querySelector("code");
|
|
||||||
|
|
||||||
if (window.ace && code_block.classList.contains("editable")) {
|
|
||||||
let editor = window.ace.edit(code_block);
|
|
||||||
return editor.getValue();
|
|
||||||
} else {
|
|
||||||
return code_block.textContent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(function codeSnippets() {
|
|
||||||
function fetch_with_timeout(url, options, timeout = 6000) {
|
|
||||||
return Promise.race([
|
|
||||||
fetch(url, options),
|
|
||||||
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout))
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var playgrounds = Array.from(document.querySelectorAll(".playground"));
|
|
||||||
if (playgrounds.length > 0) {
|
|
||||||
fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': "application/json",
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
})
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(response => {
|
|
||||||
// get list of crates available in the rust playground
|
|
||||||
let playground_crates = response.crates.map(item => item["id"]);
|
|
||||||
playgrounds.forEach(block => handle_crate_list_update(block, playground_crates));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function handle_crate_list_update(playground_block, playground_crates) {
|
|
||||||
// update the play buttons after receiving the response
|
|
||||||
update_play_button(playground_block, playground_crates);
|
|
||||||
|
|
||||||
// and install on change listener to dynamically update ACE editors
|
|
||||||
if (window.ace) {
|
|
||||||
let code_block = playground_block.querySelector("code");
|
|
||||||
if (code_block.classList.contains("editable")) {
|
|
||||||
let editor = window.ace.edit(code_block);
|
|
||||||
editor.addEventListener("change", function (e) {
|
|
||||||
update_play_button(playground_block, playground_crates);
|
|
||||||
});
|
|
||||||
// add Ctrl-Enter command to execute rust code
|
|
||||||
editor.commands.addCommand({
|
|
||||||
name: "run",
|
|
||||||
bindKey: {
|
|
||||||
win: "Ctrl-Enter",
|
|
||||||
mac: "Ctrl-Enter"
|
|
||||||
},
|
|
||||||
exec: _editor => run_rust_code(playground_block)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// updates the visibility of play button based on `no_run` class and
|
|
||||||
// used crates vs ones available on http://play.rust-lang.org
|
|
||||||
function update_play_button(pre_block, playground_crates) {
|
|
||||||
var play_button = pre_block.querySelector(".play-button");
|
|
||||||
|
|
||||||
// skip if code is `no_run`
|
|
||||||
if (pre_block.querySelector('code').classList.contains("no_run")) {
|
|
||||||
play_button.classList.add("hidden");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get list of `extern crate`'s from snippet
|
|
||||||
var txt = playground_text(pre_block);
|
|
||||||
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
|
|
||||||
var snippet_crates = [];
|
|
||||||
var item;
|
|
||||||
while (item = re.exec(txt)) {
|
|
||||||
snippet_crates.push(item[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if all used crates are available on play.rust-lang.org
|
|
||||||
var all_available = snippet_crates.every(function (elem) {
|
|
||||||
return playground_crates.indexOf(elem) > -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (all_available) {
|
|
||||||
play_button.classList.remove("hidden");
|
|
||||||
} else {
|
|
||||||
play_button.classList.add("hidden");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function run_rust_code(code_block) {
|
|
||||||
var result_block = code_block.querySelector(".result");
|
|
||||||
if (!result_block) {
|
|
||||||
result_block = document.createElement('code');
|
|
||||||
result_block.className = 'result hljs language-bash';
|
|
||||||
|
|
||||||
code_block.append(result_block);
|
|
||||||
}
|
|
||||||
|
|
||||||
let text = playground_text(code_block);
|
|
||||||
let classes = code_block.querySelector('code').classList;
|
|
||||||
let has_2018 = classes.contains("edition2018");
|
|
||||||
let edition = has_2018 ? "2018" : "2015";
|
|
||||||
|
|
||||||
var params = {
|
|
||||||
version: "stable",
|
|
||||||
optimize: "0",
|
|
||||||
code: text,
|
|
||||||
edition: edition
|
|
||||||
};
|
|
||||||
|
|
||||||
if (text.indexOf("#![feature") !== -1) {
|
|
||||||
params.version = "nightly";
|
|
||||||
}
|
|
||||||
|
|
||||||
result_block.innerText = "Running...";
|
|
||||||
|
|
||||||
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
|
|
||||||
headers: {
|
|
||||||
'Content-Type': "application/json",
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
body: JSON.stringify(params)
|
|
||||||
})
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(response => result_block.innerText = response.result)
|
|
||||||
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Syntax highlighting Configuration
|
|
||||||
hljs.configure({
|
|
||||||
tabReplace: ' ', // 4 spaces
|
|
||||||
languages: [], // Languages used for auto-detection
|
|
||||||
});
|
|
||||||
|
|
||||||
let code_nodes = Array
|
|
||||||
.from(document.querySelectorAll('code'))
|
|
||||||
// Don't highlight `inline code` blocks in headers.
|
|
||||||
.filter(function (node) {return !node.parentElement.classList.contains("header"); });
|
|
||||||
|
|
||||||
if (window.ace) {
|
|
||||||
// language-rust class needs to be removed for editable
|
|
||||||
// blocks or highlightjs will capture events
|
|
||||||
Array
|
|
||||||
.from(document.querySelectorAll('code.editable'))
|
|
||||||
.forEach(function (block) { block.classList.remove('language-rust'); });
|
|
||||||
|
|
||||||
Array
|
|
||||||
.from(document.querySelectorAll('code:not(.editable)'))
|
|
||||||
.forEach(function (block) { hljs.highlightBlock(block); });
|
|
||||||
} else {
|
|
||||||
code_nodes.forEach(function (block) { hljs.highlightBlock(block); });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adding the hljs class gives code blocks the color css
|
|
||||||
// even if highlighting doesn't apply
|
|
||||||
code_nodes.forEach(function (block) { block.classList.add('hljs'); });
|
|
||||||
|
|
||||||
Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
|
|
||||||
|
|
||||||
var lines = Array.from(block.querySelectorAll('.boring'));
|
|
||||||
// If no lines were hidden, return
|
|
||||||
if (!lines.length) { return; }
|
|
||||||
block.classList.add("hide-boring");
|
|
||||||
|
|
||||||
var buttons = document.createElement('div');
|
|
||||||
buttons.className = 'buttons';
|
|
||||||
buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
|
|
||||||
|
|
||||||
// add expand button
|
|
||||||
var pre_block = block.parentNode;
|
|
||||||
pre_block.insertBefore(buttons, pre_block.firstChild);
|
|
||||||
|
|
||||||
pre_block.querySelector('.buttons').addEventListener('click', function (e) {
|
|
||||||
if (e.target.classList.contains('fa-eye')) {
|
|
||||||
e.target.classList.remove('fa-eye');
|
|
||||||
e.target.classList.add('fa-eye-slash');
|
|
||||||
e.target.title = 'Hide lines';
|
|
||||||
e.target.setAttribute('aria-label', e.target.title);
|
|
||||||
|
|
||||||
block.classList.remove('hide-boring');
|
|
||||||
} else if (e.target.classList.contains('fa-eye-slash')) {
|
|
||||||
e.target.classList.remove('fa-eye-slash');
|
|
||||||
e.target.classList.add('fa-eye');
|
|
||||||
e.target.title = 'Show hidden lines';
|
|
||||||
e.target.setAttribute('aria-label', e.target.title);
|
|
||||||
|
|
||||||
block.classList.add('hide-boring');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (window.playground_copyable) {
|
|
||||||
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
|
|
||||||
var pre_block = block.parentNode;
|
|
||||||
if (!pre_block.classList.contains('playground')) {
|
|
||||||
var buttons = pre_block.querySelector(".buttons");
|
|
||||||
if (!buttons) {
|
|
||||||
buttons = document.createElement('div');
|
|
||||||
buttons.className = 'buttons';
|
|
||||||
pre_block.insertBefore(buttons, pre_block.firstChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
var clipButton = document.createElement('button');
|
|
||||||
clipButton.className = 'fa fa-copy clip-button';
|
|
||||||
clipButton.title = 'Copy to clipboard';
|
|
||||||
clipButton.setAttribute('aria-label', clipButton.title);
|
|
||||||
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
|
|
||||||
|
|
||||||
buttons.insertBefore(clipButton, buttons.firstChild);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process playground code blocks
|
|
||||||
Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) {
|
|
||||||
// Add play button
|
|
||||||
var buttons = pre_block.querySelector(".buttons");
|
|
||||||
if (!buttons) {
|
|
||||||
buttons = document.createElement('div');
|
|
||||||
buttons.className = 'buttons';
|
|
||||||
pre_block.insertBefore(buttons, pre_block.firstChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
var runCodeButton = document.createElement('button');
|
|
||||||
runCodeButton.className = 'fa fa-play play-button';
|
|
||||||
runCodeButton.hidden = true;
|
|
||||||
runCodeButton.title = 'Run this code';
|
|
||||||
runCodeButton.setAttribute('aria-label', runCodeButton.title);
|
|
||||||
|
|
||||||
buttons.insertBefore(runCodeButton, buttons.firstChild);
|
|
||||||
runCodeButton.addEventListener('click', function (e) {
|
|
||||||
run_rust_code(pre_block);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (window.playground_copyable) {
|
|
||||||
var copyCodeClipboardButton = document.createElement('button');
|
|
||||||
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
|
|
||||||
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
|
|
||||||
copyCodeClipboardButton.title = 'Copy to clipboard';
|
|
||||||
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
|
|
||||||
|
|
||||||
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
let code_block = pre_block.querySelector("code");
|
|
||||||
if (window.ace && code_block.classList.contains("editable")) {
|
|
||||||
var undoChangesButton = document.createElement('button');
|
|
||||||
undoChangesButton.className = 'fa fa-history reset-button';
|
|
||||||
undoChangesButton.title = 'Undo changes';
|
|
||||||
undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
|
|
||||||
|
|
||||||
buttons.insertBefore(undoChangesButton, buttons.firstChild);
|
|
||||||
|
|
||||||
undoChangesButton.addEventListener('click', function () {
|
|
||||||
let editor = window.ace.edit(code_block);
|
|
||||||
editor.setValue(editor.originalCode);
|
|
||||||
editor.clearSelection();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function themes() {
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var themeToggleButton = document.getElementById('theme-toggle');
|
|
||||||
var themePopup = document.getElementById('theme-list');
|
|
||||||
var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
|
|
||||||
var stylesheets = {
|
|
||||||
ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
|
|
||||||
tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
|
|
||||||
highlight: document.querySelector("[href$='highlight.css']"),
|
|
||||||
};
|
|
||||||
|
|
||||||
function showThemes() {
|
|
||||||
themePopup.style.display = 'block';
|
|
||||||
themeToggleButton.setAttribute('aria-expanded', true);
|
|
||||||
themePopup.querySelector("button#" + get_theme()).focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
function hideThemes() {
|
|
||||||
themePopup.style.display = 'none';
|
|
||||||
themeToggleButton.setAttribute('aria-expanded', false);
|
|
||||||
themeToggleButton.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_theme() {
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
|
|
||||||
if (theme === null || theme === undefined) {
|
|
||||||
return default_theme;
|
|
||||||
} else {
|
|
||||||
return theme;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function set_theme(theme, store = true) {
|
|
||||||
let ace_theme;
|
|
||||||
|
|
||||||
if (theme == 'coal' || theme == 'navy') {
|
|
||||||
stylesheets.ayuHighlight.disabled = true;
|
|
||||||
stylesheets.tomorrowNight.disabled = false;
|
|
||||||
stylesheets.highlight.disabled = true;
|
|
||||||
|
|
||||||
ace_theme = "ace/theme/tomorrow_night";
|
|
||||||
} else if (theme == 'ayu') {
|
|
||||||
stylesheets.ayuHighlight.disabled = false;
|
|
||||||
stylesheets.tomorrowNight.disabled = true;
|
|
||||||
stylesheets.highlight.disabled = true;
|
|
||||||
ace_theme = "ace/theme/tomorrow_night";
|
|
||||||
} else {
|
|
||||||
stylesheets.ayuHighlight.disabled = true;
|
|
||||||
stylesheets.tomorrowNight.disabled = true;
|
|
||||||
stylesheets.highlight.disabled = false;
|
|
||||||
ace_theme = "ace/theme/dawn";
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(function () {
|
|
||||||
themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor;
|
|
||||||
}, 1);
|
|
||||||
|
|
||||||
if (window.ace && window.editors) {
|
|
||||||
window.editors.forEach(function (editor) {
|
|
||||||
editor.setTheme(ace_theme);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var previousTheme = get_theme();
|
|
||||||
|
|
||||||
if (store) {
|
|
||||||
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
html.classList.remove(previousTheme);
|
|
||||||
html.classList.add(theme);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set theme
|
|
||||||
var theme = get_theme();
|
|
||||||
|
|
||||||
set_theme(theme, false);
|
|
||||||
|
|
||||||
themeToggleButton.addEventListener('click', function () {
|
|
||||||
if (themePopup.style.display === 'block') {
|
|
||||||
hideThemes();
|
|
||||||
} else {
|
|
||||||
showThemes();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
themePopup.addEventListener('click', function (e) {
|
|
||||||
var theme = e.target.id || e.target.parentElement.id;
|
|
||||||
set_theme(theme);
|
|
||||||
});
|
|
||||||
|
|
||||||
themePopup.addEventListener('focusout', function(e) {
|
|
||||||
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
|
|
||||||
if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) {
|
|
||||||
hideThemes();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
|
|
||||||
document.addEventListener('click', function(e) {
|
|
||||||
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
|
|
||||||
hideThemes();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
document.addEventListener('keydown', function (e) {
|
|
||||||
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
|
|
||||||
if (!themePopup.contains(e.target)) { return; }
|
|
||||||
|
|
||||||
switch (e.key) {
|
|
||||||
case 'Escape':
|
|
||||||
e.preventDefault();
|
|
||||||
hideThemes();
|
|
||||||
break;
|
|
||||||
case 'ArrowUp':
|
|
||||||
e.preventDefault();
|
|
||||||
var li = document.activeElement.parentElement;
|
|
||||||
if (li && li.previousElementSibling) {
|
|
||||||
li.previousElementSibling.querySelector('button').focus();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'ArrowDown':
|
|
||||||
e.preventDefault();
|
|
||||||
var li = document.activeElement.parentElement;
|
|
||||||
if (li && li.nextElementSibling) {
|
|
||||||
li.nextElementSibling.querySelector('button').focus();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'Home':
|
|
||||||
e.preventDefault();
|
|
||||||
themePopup.querySelector('li:first-child button').focus();
|
|
||||||
break;
|
|
||||||
case 'End':
|
|
||||||
e.preventDefault();
|
|
||||||
themePopup.querySelector('li:last-child button').focus();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function sidebar() {
|
|
||||||
var html = document.querySelector("html");
|
|
||||||
var sidebar = document.getElementById("sidebar");
|
|
||||||
var sidebarLinks = document.querySelectorAll('#sidebar a');
|
|
||||||
var sidebarToggleButton = document.getElementById("sidebar-toggle");
|
|
||||||
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
|
|
||||||
var firstContact = null;
|
|
||||||
|
|
||||||
function showSidebar() {
|
|
||||||
html.classList.remove('sidebar-hidden')
|
|
||||||
html.classList.add('sidebar-visible');
|
|
||||||
Array.from(sidebarLinks).forEach(function (link) {
|
|
||||||
link.setAttribute('tabIndex', 0);
|
|
||||||
});
|
|
||||||
sidebarToggleButton.setAttribute('aria-expanded', true);
|
|
||||||
sidebar.setAttribute('aria-hidden', false);
|
|
||||||
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
|
|
||||||
|
|
||||||
function toggleSection(ev) {
|
|
||||||
ev.currentTarget.parentElement.classList.toggle('expanded');
|
|
||||||
}
|
|
||||||
|
|
||||||
Array.from(sidebarAnchorToggles).forEach(function (el) {
|
|
||||||
el.addEventListener('click', toggleSection);
|
|
||||||
});
|
|
||||||
|
|
||||||
function hideSidebar() {
|
|
||||||
html.classList.remove('sidebar-visible')
|
|
||||||
html.classList.add('sidebar-hidden');
|
|
||||||
Array.from(sidebarLinks).forEach(function (link) {
|
|
||||||
link.setAttribute('tabIndex', -1);
|
|
||||||
});
|
|
||||||
sidebarToggleButton.setAttribute('aria-expanded', false);
|
|
||||||
sidebar.setAttribute('aria-hidden', true);
|
|
||||||
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Toggle sidebar
|
|
||||||
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
|
|
||||||
if (html.classList.contains("sidebar-hidden")) {
|
|
||||||
var current_width = parseInt(
|
|
||||||
document.documentElement.style.getPropertyValue('--sidebar-width'), 10);
|
|
||||||
if (current_width < 150) {
|
|
||||||
document.documentElement.style.setProperty('--sidebar-width', '150px');
|
|
||||||
}
|
|
||||||
showSidebar();
|
|
||||||
} else if (html.classList.contains("sidebar-visible")) {
|
|
||||||
hideSidebar();
|
|
||||||
} else {
|
|
||||||
if (getComputedStyle(sidebar)['transform'] === 'none') {
|
|
||||||
hideSidebar();
|
|
||||||
} else {
|
|
||||||
showSidebar();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
sidebarResizeHandle.addEventListener('mousedown', initResize, false);
|
|
||||||
|
|
||||||
function initResize(e) {
|
|
||||||
window.addEventListener('mousemove', resize, false);
|
|
||||||
window.addEventListener('mouseup', stopResize, false);
|
|
||||||
html.classList.add('sidebar-resizing');
|
|
||||||
}
|
|
||||||
function resize(e) {
|
|
||||||
var pos = (e.clientX - sidebar.offsetLeft);
|
|
||||||
if (pos < 20) {
|
|
||||||
hideSidebar();
|
|
||||||
} else {
|
|
||||||
if (html.classList.contains("sidebar-hidden")) {
|
|
||||||
showSidebar();
|
|
||||||
}
|
|
||||||
pos = Math.min(pos, window.innerWidth - 100);
|
|
||||||
document.documentElement.style.setProperty('--sidebar-width', pos + 'px');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//on mouseup remove windows functions mousemove & mouseup
|
|
||||||
function stopResize(e) {
|
|
||||||
html.classList.remove('sidebar-resizing');
|
|
||||||
window.removeEventListener('mousemove', resize, false);
|
|
||||||
window.removeEventListener('mouseup', stopResize, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('touchstart', function (e) {
|
|
||||||
firstContact = {
|
|
||||||
x: e.touches[0].clientX,
|
|
||||||
time: Date.now()
|
|
||||||
};
|
|
||||||
}, { passive: true });
|
|
||||||
|
|
||||||
document.addEventListener('touchmove', function (e) {
|
|
||||||
if (!firstContact)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var curX = e.touches[0].clientX;
|
|
||||||
var xDiff = curX - firstContact.x,
|
|
||||||
tDiff = Date.now() - firstContact.time;
|
|
||||||
|
|
||||||
if (tDiff < 250 && Math.abs(xDiff) >= 150) {
|
|
||||||
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
|
|
||||||
showSidebar();
|
|
||||||
else if (xDiff < 0 && curX < 300)
|
|
||||||
hideSidebar();
|
|
||||||
|
|
||||||
firstContact = null;
|
|
||||||
}
|
|
||||||
}, { passive: true });
|
|
||||||
|
|
||||||
// Scroll sidebar to current active section
|
|
||||||
var activeSection = document.getElementById("sidebar").querySelector(".active");
|
|
||||||
if (activeSection) {
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
|
|
||||||
activeSection.scrollIntoView({ block: 'center' });
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function chapterNavigation() {
|
|
||||||
document.addEventListener('keydown', function (e) {
|
|
||||||
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
|
|
||||||
if (window.search && window.search.hasFocus()) { return; }
|
|
||||||
|
|
||||||
switch (e.key) {
|
|
||||||
case 'ArrowRight':
|
|
||||||
e.preventDefault();
|
|
||||||
var nextButton = document.querySelector('.nav-chapters.next');
|
|
||||||
if (nextButton) {
|
|
||||||
window.location.href = nextButton.href;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'ArrowLeft':
|
|
||||||
e.preventDefault();
|
|
||||||
var previousButton = document.querySelector('.nav-chapters.previous');
|
|
||||||
if (previousButton) {
|
|
||||||
window.location.href = previousButton.href;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function clipboard() {
|
|
||||||
var clipButtons = document.querySelectorAll('.clip-button');
|
|
||||||
|
|
||||||
function hideTooltip(elem) {
|
|
||||||
elem.firstChild.innerText = "";
|
|
||||||
elem.className = 'fa fa-copy clip-button';
|
|
||||||
}
|
|
||||||
|
|
||||||
function showTooltip(elem, msg) {
|
|
||||||
elem.firstChild.innerText = msg;
|
|
||||||
elem.className = 'fa fa-copy tooltipped';
|
|
||||||
}
|
|
||||||
|
|
||||||
var clipboardSnippets = new ClipboardJS('.clip-button', {
|
|
||||||
text: function (trigger) {
|
|
||||||
hideTooltip(trigger);
|
|
||||||
let playground = trigger.closest("pre");
|
|
||||||
return playground_text(playground);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Array.from(clipButtons).forEach(function (clipButton) {
|
|
||||||
clipButton.addEventListener('mouseout', function (e) {
|
|
||||||
hideTooltip(e.currentTarget);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
clipboardSnippets.on('success', function (e) {
|
|
||||||
e.clearSelection();
|
|
||||||
showTooltip(e.trigger, "Copied!");
|
|
||||||
});
|
|
||||||
|
|
||||||
clipboardSnippets.on('error', function (e) {
|
|
||||||
showTooltip(e.trigger, "Clipboard error!");
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function scrollToTop () {
|
|
||||||
var menuTitle = document.querySelector('.menu-title');
|
|
||||||
|
|
||||||
menuTitle.addEventListener('click', function () {
|
|
||||||
document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
|
|
||||||
(function controllMenu() {
|
|
||||||
var menu = document.getElementById('menu-bar');
|
|
||||||
|
|
||||||
(function controllPosition() {
|
|
||||||
var scrollTop = document.scrollingElement.scrollTop;
|
|
||||||
var prevScrollTop = scrollTop;
|
|
||||||
var minMenuY = -menu.clientHeight - 50;
|
|
||||||
// When the script loads, the page can be at any scroll (e.g. if you reforesh it).
|
|
||||||
menu.style.top = scrollTop + 'px';
|
|
||||||
// Same as parseInt(menu.style.top.slice(0, -2), but faster
|
|
||||||
var topCache = menu.style.top.slice(0, -2);
|
|
||||||
menu.classList.remove('sticky');
|
|
||||||
var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster
|
|
||||||
document.addEventListener('scroll', function () {
|
|
||||||
scrollTop = Math.max(document.scrollingElement.scrollTop, 0);
|
|
||||||
// `null` means that it doesn't need to be updated
|
|
||||||
var nextSticky = null;
|
|
||||||
var nextTop = null;
|
|
||||||
var scrollDown = scrollTop > prevScrollTop;
|
|
||||||
var menuPosAbsoluteY = topCache - scrollTop;
|
|
||||||
if (scrollDown) {
|
|
||||||
nextSticky = false;
|
|
||||||
if (menuPosAbsoluteY > 0) {
|
|
||||||
nextTop = prevScrollTop;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (menuPosAbsoluteY > 0) {
|
|
||||||
nextSticky = true;
|
|
||||||
} else if (menuPosAbsoluteY < minMenuY) {
|
|
||||||
nextTop = prevScrollTop + minMenuY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (nextSticky === true && stickyCache === false) {
|
|
||||||
menu.classList.add('sticky');
|
|
||||||
stickyCache = true;
|
|
||||||
} else if (nextSticky === false && stickyCache === true) {
|
|
||||||
menu.classList.remove('sticky');
|
|
||||||
stickyCache = false;
|
|
||||||
}
|
|
||||||
if (nextTop !== null) {
|
|
||||||
menu.style.top = nextTop + 'px';
|
|
||||||
topCache = nextTop;
|
|
||||||
}
|
|
||||||
prevScrollTop = scrollTop;
|
|
||||||
}, { passive: true });
|
|
||||||
})();
|
|
||||||
(function controllBorder() {
|
|
||||||
menu.classList.remove('bordered');
|
|
||||||
document.addEventListener('scroll', function () {
|
|
||||||
if (menu.offsetTop === 0) {
|
|
||||||
menu.classList.remove('bordered');
|
|
||||||
} else {
|
|
||||||
menu.classList.add('bordered');
|
|
||||||
}
|
|
||||||
}, { passive: true });
|
|
||||||
})();
|
|
||||||
})();
|
|
7
current/clipboard.min.js
vendored
7
current/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,495 +0,0 @@
|
||||||
/* CSS for UI elements (a.k.a. chrome) */
|
|
||||||
|
|
||||||
@import 'variables.css';
|
|
||||||
|
|
||||||
::-webkit-scrollbar {
|
|
||||||
background: var(--bg);
|
|
||||||
}
|
|
||||||
::-webkit-scrollbar-thumb {
|
|
||||||
background: var(--scrollbar);
|
|
||||||
}
|
|
||||||
html {
|
|
||||||
scrollbar-color: var(--scrollbar) var(--bg);
|
|
||||||
}
|
|
||||||
#searchresults a,
|
|
||||||
.content a:link,
|
|
||||||
a:visited,
|
|
||||||
a > .hljs {
|
|
||||||
color: var(--links);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Menu Bar */
|
|
||||||
|
|
||||||
#menu-bar,
|
|
||||||
#menu-bar-hover-placeholder {
|
|
||||||
z-index: 101;
|
|
||||||
margin: auto calc(0px - var(--page-padding));
|
|
||||||
}
|
|
||||||
#menu-bar {
|
|
||||||
position: relative;
|
|
||||||
display: flex;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
background-color: var(--bg);
|
|
||||||
border-bottom-color: var(--bg);
|
|
||||||
border-bottom-width: 1px;
|
|
||||||
border-bottom-style: solid;
|
|
||||||
}
|
|
||||||
#menu-bar.sticky,
|
|
||||||
.js #menu-bar-hover-placeholder:hover + #menu-bar,
|
|
||||||
.js #menu-bar:hover,
|
|
||||||
.js.sidebar-visible #menu-bar {
|
|
||||||
position: -webkit-sticky;
|
|
||||||
position: sticky;
|
|
||||||
top: 0 !important;
|
|
||||||
}
|
|
||||||
#menu-bar-hover-placeholder {
|
|
||||||
position: sticky;
|
|
||||||
position: -webkit-sticky;
|
|
||||||
top: 0;
|
|
||||||
height: var(--menu-bar-height);
|
|
||||||
}
|
|
||||||
#menu-bar.bordered {
|
|
||||||
border-bottom-color: var(--table-border-color);
|
|
||||||
}
|
|
||||||
#menu-bar i, #menu-bar .icon-button {
|
|
||||||
position: relative;
|
|
||||||
padding: 0 8px;
|
|
||||||
z-index: 10;
|
|
||||||
line-height: var(--menu-bar-height);
|
|
||||||
cursor: pointer;
|
|
||||||
transition: color 0.5s;
|
|
||||||
}
|
|
||||||
@media only screen and (max-width: 420px) {
|
|
||||||
#menu-bar i, #menu-bar .icon-button {
|
|
||||||
padding: 0 5px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.icon-button {
|
|
||||||
border: none;
|
|
||||||
background: none;
|
|
||||||
padding: 0;
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
.icon-button i {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.right-buttons {
|
|
||||||
margin: 0 15px;
|
|
||||||
}
|
|
||||||
.right-buttons a {
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.left-buttons {
|
|
||||||
display: flex;
|
|
||||||
margin: 0 5px;
|
|
||||||
}
|
|
||||||
.no-js .left-buttons {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.menu-title {
|
|
||||||
display: inline-block;
|
|
||||||
font-weight: 200;
|
|
||||||
font-size: 2rem;
|
|
||||||
line-height: var(--menu-bar-height);
|
|
||||||
text-align: center;
|
|
||||||
margin: 0;
|
|
||||||
flex: 1;
|
|
||||||
white-space: nowrap;
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
}
|
|
||||||
.js .menu-title {
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.menu-bar,
|
|
||||||
.menu-bar:visited,
|
|
||||||
.nav-chapters,
|
|
||||||
.nav-chapters:visited,
|
|
||||||
.mobile-nav-chapters,
|
|
||||||
.mobile-nav-chapters:visited,
|
|
||||||
.menu-bar .icon-button,
|
|
||||||
.menu-bar a i {
|
|
||||||
color: var(--icons);
|
|
||||||
}
|
|
||||||
|
|
||||||
.menu-bar i:hover,
|
|
||||||
.menu-bar .icon-button:hover,
|
|
||||||
.nav-chapters:hover,
|
|
||||||
.mobile-nav-chapters i:hover {
|
|
||||||
color: var(--icons-hover);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Nav Icons */
|
|
||||||
|
|
||||||
.nav-chapters {
|
|
||||||
font-size: 2.5em;
|
|
||||||
text-align: center;
|
|
||||||
text-decoration: none;
|
|
||||||
|
|
||||||
position: fixed;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
margin: 0;
|
|
||||||
max-width: 150px;
|
|
||||||
min-width: 90px;
|
|
||||||
|
|
||||||
display: flex;
|
|
||||||
justify-content: center;
|
|
||||||
align-content: center;
|
|
||||||
flex-direction: column;
|
|
||||||
|
|
||||||
transition: color 0.5s, background-color 0.5s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.nav-chapters:hover {
|
|
||||||
text-decoration: none;
|
|
||||||
background-color: var(--theme-hover);
|
|
||||||
transition: background-color 0.15s, color 0.15s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.nav-wrapper {
|
|
||||||
margin-top: 50px;
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.mobile-nav-chapters {
|
|
||||||
font-size: 2.5em;
|
|
||||||
text-align: center;
|
|
||||||
text-decoration: none;
|
|
||||||
width: 90px;
|
|
||||||
border-radius: 5px;
|
|
||||||
background-color: var(--sidebar-bg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.previous {
|
|
||||||
float: left;
|
|
||||||
}
|
|
||||||
|
|
||||||
.next {
|
|
||||||
float: right;
|
|
||||||
right: var(--page-padding);
|
|
||||||
}
|
|
||||||
|
|
||||||
@media only screen and (max-width: 1080px) {
|
|
||||||
.nav-wide-wrapper { display: none; }
|
|
||||||
.nav-wrapper { display: block; }
|
|
||||||
}
|
|
||||||
|
|
||||||
@media only screen and (max-width: 1380px) {
|
|
||||||
.sidebar-visible .nav-wide-wrapper { display: none; }
|
|
||||||
.sidebar-visible .nav-wrapper { display: block; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Inline code */
|
|
||||||
|
|
||||||
:not(pre) > .hljs {
|
|
||||||
display: inline;
|
|
||||||
padding: 0.1em 0.3em;
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
|
|
||||||
:not(pre):not(a) > .hljs {
|
|
||||||
color: var(--inline-code-color);
|
|
||||||
overflow-x: initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
a:hover > .hljs {
|
|
||||||
text-decoration: underline;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre {
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
pre > .buttons {
|
|
||||||
position: absolute;
|
|
||||||
z-index: 100;
|
|
||||||
right: 5px;
|
|
||||||
top: 5px;
|
|
||||||
|
|
||||||
color: var(--sidebar-fg);
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
pre > .buttons :hover {
|
|
||||||
color: var(--sidebar-active);
|
|
||||||
}
|
|
||||||
pre > .buttons i {
|
|
||||||
margin-left: 8px;
|
|
||||||
}
|
|
||||||
pre > .buttons button {
|
|
||||||
color: inherit;
|
|
||||||
background: transparent;
|
|
||||||
border: none;
|
|
||||||
cursor: inherit;
|
|
||||||
}
|
|
||||||
pre > .result {
|
|
||||||
margin-top: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Search */
|
|
||||||
|
|
||||||
#searchresults a {
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
mark {
|
|
||||||
border-radius: 2px;
|
|
||||||
padding: 0 3px 1px 3px;
|
|
||||||
margin: 0 -3px -1px -3px;
|
|
||||||
background-color: var(--search-mark-bg);
|
|
||||||
transition: background-color 300ms linear;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
mark.fade-out {
|
|
||||||
background-color: rgba(0,0,0,0) !important;
|
|
||||||
cursor: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.searchbar-outer {
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
max-width: var(--content-max-width);
|
|
||||||
}
|
|
||||||
|
|
||||||
#searchbar {
|
|
||||||
width: 100%;
|
|
||||||
margin: 5px auto 0px auto;
|
|
||||||
padding: 10px 16px;
|
|
||||||
transition: box-shadow 300ms ease-in-out;
|
|
||||||
border: 1px solid var(--searchbar-border-color);
|
|
||||||
border-radius: 3px;
|
|
||||||
background-color: var(--searchbar-bg);
|
|
||||||
color: var(--searchbar-fg);
|
|
||||||
}
|
|
||||||
#searchbar:focus,
|
|
||||||
#searchbar.active {
|
|
||||||
box-shadow: 0 0 3px var(--searchbar-shadow-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.searchresults-header {
|
|
||||||
font-weight: bold;
|
|
||||||
font-size: 1em;
|
|
||||||
padding: 18px 0 0 5px;
|
|
||||||
color: var(--searchresults-header-fg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.searchresults-outer {
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
max-width: var(--content-max-width);
|
|
||||||
border-bottom: 1px dashed var(--searchresults-border-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
ul#searchresults {
|
|
||||||
list-style: none;
|
|
||||||
padding-left: 20px;
|
|
||||||
}
|
|
||||||
ul#searchresults li {
|
|
||||||
margin: 10px 0px;
|
|
||||||
padding: 2px;
|
|
||||||
border-radius: 2px;
|
|
||||||
}
|
|
||||||
ul#searchresults li.focus {
|
|
||||||
background-color: var(--searchresults-li-bg);
|
|
||||||
}
|
|
||||||
ul#searchresults span.teaser {
|
|
||||||
display: block;
|
|
||||||
clear: both;
|
|
||||||
margin: 5px 0 0 20px;
|
|
||||||
font-size: 0.8em;
|
|
||||||
}
|
|
||||||
ul#searchresults span.teaser em {
|
|
||||||
font-weight: bold;
|
|
||||||
font-style: normal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Sidebar */
|
|
||||||
|
|
||||||
.sidebar {
|
|
||||||
position: fixed;
|
|
||||||
left: 0;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
width: var(--sidebar-width);
|
|
||||||
font-size: 0.875em;
|
|
||||||
box-sizing: border-box;
|
|
||||||
-webkit-overflow-scrolling: touch;
|
|
||||||
overscroll-behavior-y: contain;
|
|
||||||
background-color: var(--sidebar-bg);
|
|
||||||
color: var(--sidebar-fg);
|
|
||||||
}
|
|
||||||
.sidebar-resizing {
|
|
||||||
-moz-user-select: none;
|
|
||||||
-webkit-user-select: none;
|
|
||||||
-ms-user-select: none;
|
|
||||||
user-select: none;
|
|
||||||
}
|
|
||||||
.js:not(.sidebar-resizing) .sidebar {
|
|
||||||
transition: transform 0.3s; /* Animation: slide away */
|
|
||||||
}
|
|
||||||
.sidebar code {
|
|
||||||
line-height: 2em;
|
|
||||||
}
|
|
||||||
.sidebar .sidebar-scrollbox {
|
|
||||||
overflow-y: auto;
|
|
||||||
position: absolute;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
padding: 10px 10px;
|
|
||||||
}
|
|
||||||
.sidebar .sidebar-resize-handle {
|
|
||||||
position: absolute;
|
|
||||||
cursor: col-resize;
|
|
||||||
width: 0;
|
|
||||||
right: 0;
|
|
||||||
top: 0;
|
|
||||||
bottom: 0;
|
|
||||||
}
|
|
||||||
.js .sidebar .sidebar-resize-handle {
|
|
||||||
cursor: col-resize;
|
|
||||||
width: 5px;
|
|
||||||
}
|
|
||||||
.sidebar-hidden .sidebar {
|
|
||||||
transform: translateX(calc(0px - var(--sidebar-width)));
|
|
||||||
}
|
|
||||||
.sidebar::-webkit-scrollbar {
|
|
||||||
background: var(--sidebar-bg);
|
|
||||||
}
|
|
||||||
.sidebar::-webkit-scrollbar-thumb {
|
|
||||||
background: var(--scrollbar);
|
|
||||||
}
|
|
||||||
|
|
||||||
.sidebar-visible .page-wrapper {
|
|
||||||
transform: translateX(var(--sidebar-width));
|
|
||||||
}
|
|
||||||
@media only screen and (min-width: 620px) {
|
|
||||||
.sidebar-visible .page-wrapper {
|
|
||||||
transform: none;
|
|
||||||
margin-left: var(--sidebar-width);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter {
|
|
||||||
list-style: none outside none;
|
|
||||||
padding-left: 0;
|
|
||||||
line-height: 2.2em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter ol {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li {
|
|
||||||
display: flex;
|
|
||||||
color: var(--sidebar-non-existant);
|
|
||||||
}
|
|
||||||
.chapter li a {
|
|
||||||
display: block;
|
|
||||||
padding: 0;
|
|
||||||
text-decoration: none;
|
|
||||||
color: var(--sidebar-fg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li a:hover {
|
|
||||||
color: var(--sidebar-active);
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li a.active {
|
|
||||||
color: var(--sidebar-active);
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li > a.toggle {
|
|
||||||
cursor: pointer;
|
|
||||||
display: block;
|
|
||||||
margin-left: auto;
|
|
||||||
padding: 0 10px;
|
|
||||||
user-select: none;
|
|
||||||
opacity: 0.68;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li > a.toggle div {
|
|
||||||
transition: transform 0.5s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* collapse the section */
|
|
||||||
.chapter li:not(.expanded) + li > ol {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li.chapter-item {
|
|
||||||
line-height: 1.5em;
|
|
||||||
margin-top: 0.6em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li.expanded > a.toggle div {
|
|
||||||
transform: rotate(90deg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.spacer {
|
|
||||||
width: 100%;
|
|
||||||
height: 3px;
|
|
||||||
margin: 5px 0px;
|
|
||||||
}
|
|
||||||
.chapter .spacer {
|
|
||||||
background-color: var(--sidebar-spacer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (-moz-touch-enabled: 1), (pointer: coarse) {
|
|
||||||
.chapter li a { padding: 5px 0; }
|
|
||||||
.spacer { margin: 10px 0; }
|
|
||||||
}
|
|
||||||
|
|
||||||
.section {
|
|
||||||
list-style: none outside none;
|
|
||||||
padding-left: 20px;
|
|
||||||
line-height: 1.9em;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Theme Menu Popup */
|
|
||||||
|
|
||||||
.theme-popup {
|
|
||||||
position: absolute;
|
|
||||||
left: 10px;
|
|
||||||
top: var(--menu-bar-height);
|
|
||||||
z-index: 1000;
|
|
||||||
border-radius: 4px;
|
|
||||||
font-size: 0.7em;
|
|
||||||
color: var(--fg);
|
|
||||||
background: var(--theme-popup-bg);
|
|
||||||
border: 1px solid var(--theme-popup-border);
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
list-style: none;
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.theme-popup .default {
|
|
||||||
color: var(--icons);
|
|
||||||
}
|
|
||||||
.theme-popup .theme {
|
|
||||||
width: 100%;
|
|
||||||
border: 0;
|
|
||||||
margin: 0;
|
|
||||||
padding: 2px 10px;
|
|
||||||
line-height: 25px;
|
|
||||||
white-space: nowrap;
|
|
||||||
text-align: left;
|
|
||||||
cursor: pointer;
|
|
||||||
color: inherit;
|
|
||||||
background: inherit;
|
|
||||||
font-size: inherit;
|
|
||||||
}
|
|
||||||
.theme-popup .theme:hover {
|
|
||||||
background-color: var(--theme-hover);
|
|
||||||
}
|
|
||||||
.theme-popup .theme:hover:first-child,
|
|
||||||
.theme-popup .theme:hover:last-child {
|
|
||||||
border-top-left-radius: inherit;
|
|
||||||
border-top-right-radius: inherit;
|
|
||||||
}
|
|
|
@ -1,174 +0,0 @@
|
||||||
/* Base styles and content styles */
|
|
||||||
|
|
||||||
@import 'variables.css';
|
|
||||||
|
|
||||||
:root {
|
|
||||||
/* Browser default font-size is 16px, this way 1 rem = 10px */
|
|
||||||
font-size: 62.5%;
|
|
||||||
}
|
|
||||||
|
|
||||||
html {
|
|
||||||
font-family: "Open Sans", sans-serif;
|
|
||||||
color: var(--fg);
|
|
||||||
background-color: var(--bg);
|
|
||||||
text-size-adjust: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
font-size: 1.6rem;
|
|
||||||
overflow-x: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
font-family: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace !important;
|
|
||||||
font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Don't change font size in headers. */
|
|
||||||
h1 code, h2 code, h3 code, h4 code, h5 code, h6 code {
|
|
||||||
font-size: unset;
|
|
||||||
}
|
|
||||||
|
|
||||||
.left { float: left; }
|
|
||||||
.right { float: right; }
|
|
||||||
.boring { opacity: 0.6; }
|
|
||||||
.hide-boring .boring { display: none; }
|
|
||||||
.hidden { display: none !important; }
|
|
||||||
|
|
||||||
h2, h3 { margin-top: 2.5em; }
|
|
||||||
h4, h5 { margin-top: 2em; }
|
|
||||||
|
|
||||||
.header + .header h3,
|
|
||||||
.header + .header h4,
|
|
||||||
.header + .header h5 {
|
|
||||||
margin-top: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 a.header:target::before,
|
|
||||||
h2 a.header:target::before,
|
|
||||||
h3 a.header:target::before,
|
|
||||||
h4 a.header:target::before {
|
|
||||||
display: inline-block;
|
|
||||||
content: "»";
|
|
||||||
margin-left: -30px;
|
|
||||||
width: 30px;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 a.header:target,
|
|
||||||
h2 a.header:target,
|
|
||||||
h3 a.header:target,
|
|
||||||
h4 a.header:target {
|
|
||||||
scroll-margin-top: calc(var(--menu-bar-height) + 0.5em);
|
|
||||||
}
|
|
||||||
|
|
||||||
.page {
|
|
||||||
outline: 0;
|
|
||||||
padding: 0 var(--page-padding);
|
|
||||||
margin-top: calc(0px - var(--menu-bar-height)); /* Compensate for the #menu-bar-hover-placeholder */
|
|
||||||
}
|
|
||||||
.page-wrapper {
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
.js:not(.sidebar-resizing) .page-wrapper {
|
|
||||||
transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
|
|
||||||
}
|
|
||||||
|
|
||||||
.content {
|
|
||||||
overflow-y: auto;
|
|
||||||
padding: 0 15px;
|
|
||||||
padding-bottom: 50px;
|
|
||||||
}
|
|
||||||
.content main {
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
max-width: var(--content-max-width);
|
|
||||||
}
|
|
||||||
.content p { line-height: 1.45em; }
|
|
||||||
.content ol { line-height: 1.45em; }
|
|
||||||
.content ul { line-height: 1.45em; }
|
|
||||||
.content a { text-decoration: none; }
|
|
||||||
.content a:hover { text-decoration: underline; }
|
|
||||||
.content img { max-width: 100%; }
|
|
||||||
.content .header:link,
|
|
||||||
.content .header:visited {
|
|
||||||
color: var(--fg);
|
|
||||||
}
|
|
||||||
.content .header:link,
|
|
||||||
.content .header:visited:hover {
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
table {
|
|
||||||
margin: 0 auto;
|
|
||||||
border-collapse: collapse;
|
|
||||||
}
|
|
||||||
table td {
|
|
||||||
padding: 3px 20px;
|
|
||||||
border: 1px var(--table-border-color) solid;
|
|
||||||
}
|
|
||||||
table thead {
|
|
||||||
background: var(--table-header-bg);
|
|
||||||
}
|
|
||||||
table thead td {
|
|
||||||
font-weight: 700;
|
|
||||||
border: none;
|
|
||||||
}
|
|
||||||
table thead th {
|
|
||||||
padding: 3px 20px;
|
|
||||||
}
|
|
||||||
table thead tr {
|
|
||||||
border: 1px var(--table-header-bg) solid;
|
|
||||||
}
|
|
||||||
/* Alternate background colors for rows */
|
|
||||||
table tbody tr:nth-child(2n) {
|
|
||||||
background: var(--table-alternate-bg);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
blockquote {
|
|
||||||
margin: 20px 0;
|
|
||||||
padding: 0 20px;
|
|
||||||
color: var(--fg);
|
|
||||||
background-color: var(--quote-bg);
|
|
||||||
border-top: .1em solid var(--quote-border);
|
|
||||||
border-bottom: .1em solid var(--quote-border);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
:not(.footnote-definition) + .footnote-definition,
|
|
||||||
.footnote-definition + :not(.footnote-definition) {
|
|
||||||
margin-top: 2em;
|
|
||||||
}
|
|
||||||
.footnote-definition {
|
|
||||||
font-size: 0.9em;
|
|
||||||
margin: 0.5em 0;
|
|
||||||
}
|
|
||||||
.footnote-definition p {
|
|
||||||
display: inline;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tooltiptext {
|
|
||||||
position: absolute;
|
|
||||||
visibility: hidden;
|
|
||||||
color: #fff;
|
|
||||||
background-color: #333;
|
|
||||||
transform: translateX(-50%); /* Center by moving tooltip 50% of its width left */
|
|
||||||
left: -8px; /* Half of the width of the icon */
|
|
||||||
top: -35px;
|
|
||||||
font-size: 0.8em;
|
|
||||||
text-align: center;
|
|
||||||
border-radius: 6px;
|
|
||||||
padding: 5px 8px;
|
|
||||||
margin: 5px;
|
|
||||||
z-index: 1000;
|
|
||||||
}
|
|
||||||
.tooltipped .tooltiptext {
|
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chapter li.part-title {
|
|
||||||
color: var(--sidebar-fg);
|
|
||||||
margin: 5px 0px;
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
|
@ -1,54 +0,0 @@
|
||||||
|
|
||||||
#sidebar,
|
|
||||||
#menu-bar,
|
|
||||||
.nav-chapters,
|
|
||||||
.mobile-nav-chapters {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
#page-wrapper.page-wrapper {
|
|
||||||
transform: none;
|
|
||||||
margin-left: 0px;
|
|
||||||
overflow-y: initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
#content {
|
|
||||||
max-width: none;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.page {
|
|
||||||
overflow-y: initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background-color: #666666;
|
|
||||||
border-radius: 5px;
|
|
||||||
|
|
||||||
/* Force background to be printed in Chrome */
|
|
||||||
-webkit-print-color-adjust: exact;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre > .buttons {
|
|
||||||
z-index: 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
a, a:visited, a:active, a:hover {
|
|
||||||
color: #4183c4;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1, h2, h3, h4, h5, h6 {
|
|
||||||
page-break-inside: avoid;
|
|
||||||
page-break-after: avoid;
|
|
||||||
}
|
|
||||||
|
|
||||||
pre, code {
|
|
||||||
page-break-inside: avoid;
|
|
||||||
white-space: pre-wrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.fa {
|
|
||||||
display: none !important;
|
|
||||||
}
|
|
|
@ -1,253 +0,0 @@
|
||||||
|
|
||||||
/* Globals */
|
|
||||||
|
|
||||||
:root {
|
|
||||||
--sidebar-width: 300px;
|
|
||||||
--page-padding: 15px;
|
|
||||||
--content-max-width: 750px;
|
|
||||||
--menu-bar-height: 50px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Themes */
|
|
||||||
|
|
||||||
.ayu {
|
|
||||||
--bg: hsl(210, 25%, 8%);
|
|
||||||
--fg: #c5c5c5;
|
|
||||||
|
|
||||||
--sidebar-bg: #14191f;
|
|
||||||
--sidebar-fg: #c8c9db;
|
|
||||||
--sidebar-non-existant: #5c6773;
|
|
||||||
--sidebar-active: #ffb454;
|
|
||||||
--sidebar-spacer: #2d334f;
|
|
||||||
|
|
||||||
--scrollbar: var(--sidebar-fg);
|
|
||||||
|
|
||||||
--icons: #737480;
|
|
||||||
--icons-hover: #b7b9cc;
|
|
||||||
|
|
||||||
--links: #0096cf;
|
|
||||||
|
|
||||||
--inline-code-color: #ffb454;
|
|
||||||
|
|
||||||
--theme-popup-bg: #14191f;
|
|
||||||
--theme-popup-border: #5c6773;
|
|
||||||
--theme-hover: #191f26;
|
|
||||||
|
|
||||||
--quote-bg: hsl(226, 15%, 17%);
|
|
||||||
--quote-border: hsl(226, 15%, 22%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(210, 25%, 13%);
|
|
||||||
--table-header-bg: hsl(210, 25%, 28%);
|
|
||||||
--table-alternate-bg: hsl(210, 25%, 11%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #848484;
|
|
||||||
--searchbar-bg: #424242;
|
|
||||||
--searchbar-fg: #fff;
|
|
||||||
--searchbar-shadow-color: #d4c89f;
|
|
||||||
--searchresults-header-fg: #666;
|
|
||||||
--searchresults-border-color: #888;
|
|
||||||
--searchresults-li-bg: #252932;
|
|
||||||
--search-mark-bg: #e3b171;
|
|
||||||
}
|
|
||||||
|
|
||||||
.coal {
|
|
||||||
--bg: hsl(200, 7%, 8%);
|
|
||||||
--fg: #98a3ad;
|
|
||||||
|
|
||||||
--sidebar-bg: #292c2f;
|
|
||||||
--sidebar-fg: #a1adb8;
|
|
||||||
--sidebar-non-existant: #505254;
|
|
||||||
--sidebar-active: #3473ad;
|
|
||||||
--sidebar-spacer: #393939;
|
|
||||||
|
|
||||||
--scrollbar: var(--sidebar-fg);
|
|
||||||
|
|
||||||
--icons: #43484d;
|
|
||||||
--icons-hover: #b3c0cc;
|
|
||||||
|
|
||||||
--links: #2b79a2;
|
|
||||||
|
|
||||||
--inline-code-color: #c5c8c6;;
|
|
||||||
|
|
||||||
--theme-popup-bg: #141617;
|
|
||||||
--theme-popup-border: #43484d;
|
|
||||||
--theme-hover: #1f2124;
|
|
||||||
|
|
||||||
--quote-bg: hsl(234, 21%, 18%);
|
|
||||||
--quote-border: hsl(234, 21%, 23%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(200, 7%, 13%);
|
|
||||||
--table-header-bg: hsl(200, 7%, 28%);
|
|
||||||
--table-alternate-bg: hsl(200, 7%, 11%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
|
||||||
--searchbar-bg: #b7b7b7;
|
|
||||||
--searchbar-fg: #000;
|
|
||||||
--searchbar-shadow-color: #aaa;
|
|
||||||
--searchresults-header-fg: #666;
|
|
||||||
--searchresults-border-color: #98a3ad;
|
|
||||||
--searchresults-li-bg: #2b2b2f;
|
|
||||||
--search-mark-bg: #355c7d;
|
|
||||||
}
|
|
||||||
|
|
||||||
.light {
|
|
||||||
--bg: hsl(0, 0%, 100%);
|
|
||||||
--fg: #333333;
|
|
||||||
|
|
||||||
--sidebar-bg: #fafafa;
|
|
||||||
--sidebar-fg: #364149;
|
|
||||||
--sidebar-non-existant: #aaaaaa;
|
|
||||||
--sidebar-active: #008cff;
|
|
||||||
--sidebar-spacer: #f4f4f4;
|
|
||||||
|
|
||||||
--scrollbar: #cccccc;
|
|
||||||
|
|
||||||
--icons: #cccccc;
|
|
||||||
--icons-hover: #333333;
|
|
||||||
|
|
||||||
--links: #4183c4;
|
|
||||||
|
|
||||||
--inline-code-color: #6e6b5e;
|
|
||||||
|
|
||||||
--theme-popup-bg: #fafafa;
|
|
||||||
--theme-popup-border: #cccccc;
|
|
||||||
--theme-hover: #e6e6e6;
|
|
||||||
|
|
||||||
--quote-bg: hsl(197, 37%, 96%);
|
|
||||||
--quote-border: hsl(197, 37%, 91%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(0, 0%, 95%);
|
|
||||||
--table-header-bg: hsl(0, 0%, 80%);
|
|
||||||
--table-alternate-bg: hsl(0, 0%, 97%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
|
||||||
--searchbar-bg: #fafafa;
|
|
||||||
--searchbar-fg: #000;
|
|
||||||
--searchbar-shadow-color: #aaa;
|
|
||||||
--searchresults-header-fg: #666;
|
|
||||||
--searchresults-border-color: #888;
|
|
||||||
--searchresults-li-bg: #e4f2fe;
|
|
||||||
--search-mark-bg: #a2cff5;
|
|
||||||
}
|
|
||||||
|
|
||||||
.navy {
|
|
||||||
--bg: hsl(226, 23%, 11%);
|
|
||||||
--fg: #bcbdd0;
|
|
||||||
|
|
||||||
--sidebar-bg: #282d3f;
|
|
||||||
--sidebar-fg: #c8c9db;
|
|
||||||
--sidebar-non-existant: #505274;
|
|
||||||
--sidebar-active: #2b79a2;
|
|
||||||
--sidebar-spacer: #2d334f;
|
|
||||||
|
|
||||||
--scrollbar: var(--sidebar-fg);
|
|
||||||
|
|
||||||
--icons: #737480;
|
|
||||||
--icons-hover: #b7b9cc;
|
|
||||||
|
|
||||||
--links: #2b79a2;
|
|
||||||
|
|
||||||
--inline-code-color: #c5c8c6;;
|
|
||||||
|
|
||||||
--theme-popup-bg: #161923;
|
|
||||||
--theme-popup-border: #737480;
|
|
||||||
--theme-hover: #282e40;
|
|
||||||
|
|
||||||
--quote-bg: hsl(226, 15%, 17%);
|
|
||||||
--quote-border: hsl(226, 15%, 22%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(226, 23%, 16%);
|
|
||||||
--table-header-bg: hsl(226, 23%, 31%);
|
|
||||||
--table-alternate-bg: hsl(226, 23%, 14%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
|
||||||
--searchbar-bg: #aeaec6;
|
|
||||||
--searchbar-fg: #000;
|
|
||||||
--searchbar-shadow-color: #aaa;
|
|
||||||
--searchresults-header-fg: #5f5f71;
|
|
||||||
--searchresults-border-color: #5c5c68;
|
|
||||||
--searchresults-li-bg: #242430;
|
|
||||||
--search-mark-bg: #a2cff5;
|
|
||||||
}
|
|
||||||
|
|
||||||
.rust {
|
|
||||||
--bg: hsl(60, 9%, 87%);
|
|
||||||
--fg: #262625;
|
|
||||||
|
|
||||||
--sidebar-bg: #3b2e2a;
|
|
||||||
--sidebar-fg: #c8c9db;
|
|
||||||
--sidebar-non-existant: #505254;
|
|
||||||
--sidebar-active: #e69f67;
|
|
||||||
--sidebar-spacer: #45373a;
|
|
||||||
|
|
||||||
--scrollbar: var(--sidebar-fg);
|
|
||||||
|
|
||||||
--icons: #737480;
|
|
||||||
--icons-hover: #262625;
|
|
||||||
|
|
||||||
--links: #2b79a2;
|
|
||||||
|
|
||||||
--inline-code-color: #6e6b5e;
|
|
||||||
|
|
||||||
--theme-popup-bg: #e1e1db;
|
|
||||||
--theme-popup-border: #b38f6b;
|
|
||||||
--theme-hover: #99908a;
|
|
||||||
|
|
||||||
--quote-bg: hsl(60, 5%, 75%);
|
|
||||||
--quote-border: hsl(60, 5%, 70%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(60, 9%, 82%);
|
|
||||||
--table-header-bg: #b3a497;
|
|
||||||
--table-alternate-bg: hsl(60, 9%, 84%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
|
||||||
--searchbar-bg: #fafafa;
|
|
||||||
--searchbar-fg: #000;
|
|
||||||
--searchbar-shadow-color: #aaa;
|
|
||||||
--searchresults-header-fg: #666;
|
|
||||||
--searchresults-border-color: #888;
|
|
||||||
--searchresults-li-bg: #dec2a2;
|
|
||||||
--search-mark-bg: #e69f67;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
|
||||||
.light.no-js {
|
|
||||||
--bg: hsl(200, 7%, 8%);
|
|
||||||
--fg: #98a3ad;
|
|
||||||
|
|
||||||
--sidebar-bg: #292c2f;
|
|
||||||
--sidebar-fg: #a1adb8;
|
|
||||||
--sidebar-non-existant: #505254;
|
|
||||||
--sidebar-active: #3473ad;
|
|
||||||
--sidebar-spacer: #393939;
|
|
||||||
|
|
||||||
--scrollbar: var(--sidebar-fg);
|
|
||||||
|
|
||||||
--icons: #43484d;
|
|
||||||
--icons-hover: #b3c0cc;
|
|
||||||
|
|
||||||
--links: #2b79a2;
|
|
||||||
|
|
||||||
--inline-code-color: #c5c8c6;;
|
|
||||||
|
|
||||||
--theme-popup-bg: #141617;
|
|
||||||
--theme-popup-border: #43484d;
|
|
||||||
--theme-hover: #1f2124;
|
|
||||||
|
|
||||||
--quote-bg: hsl(234, 21%, 18%);
|
|
||||||
--quote-border: hsl(234, 21%, 23%);
|
|
||||||
|
|
||||||
--table-border-color: hsl(200, 7%, 13%);
|
|
||||||
--table-header-bg: hsl(200, 7%, 28%);
|
|
||||||
--table-alternate-bg: hsl(200, 7%, 11%);
|
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
|
||||||
--searchbar-bg: #b7b7b7;
|
|
||||||
--searchbar-fg: #000;
|
|
||||||
--searchbar-shadow-color: #aaa;
|
|
||||||
--searchresults-header-fg: #666;
|
|
||||||
--searchresults-border-color: #98a3ad;
|
|
||||||
--searchresults-li-bg: #2b2b2f;
|
|
||||||
--search-mark-bg: #355c7d;
|
|
||||||
}
|
|
||||||
}
|
|
10
current/elasticlunr.min.js
vendored
10
current/elasticlunr.min.js
vendored
File diff suppressed because one or more lines are too long
Binary file not shown.
Before Width: | Height: | Size: 5.5 KiB |
|
@ -1,79 +0,0 @@
|
||||||
/* Base16 Atelier Dune Light - Theme */
|
|
||||||
/* by Bram de Haan (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune) */
|
|
||||||
/* Original Base16 color scheme by Chris Kempson (https://github.com/chriskempson/base16) */
|
|
||||||
|
|
||||||
/* Atelier-Dune Comment */
|
|
||||||
.hljs-comment,
|
|
||||||
.hljs-quote {
|
|
||||||
color: #AAA;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Atelier-Dune Red */
|
|
||||||
.hljs-variable,
|
|
||||||
.hljs-template-variable,
|
|
||||||
.hljs-attribute,
|
|
||||||
.hljs-tag,
|
|
||||||
.hljs-name,
|
|
||||||
.hljs-regexp,
|
|
||||||
.hljs-link,
|
|
||||||
.hljs-name,
|
|
||||||
.hljs-selector-id,
|
|
||||||
.hljs-selector-class {
|
|
||||||
color: #d73737;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Atelier-Dune Orange */
|
|
||||||
.hljs-number,
|
|
||||||
.hljs-meta,
|
|
||||||
.hljs-built_in,
|
|
||||||
.hljs-builtin-name,
|
|
||||||
.hljs-literal,
|
|
||||||
.hljs-type,
|
|
||||||
.hljs-params {
|
|
||||||
color: #b65611;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Atelier-Dune Green */
|
|
||||||
.hljs-string,
|
|
||||||
.hljs-symbol,
|
|
||||||
.hljs-bullet {
|
|
||||||
color: #60ac39;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Atelier-Dune Blue */
|
|
||||||
.hljs-title,
|
|
||||||
.hljs-section {
|
|
||||||
color: #6684e1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Atelier-Dune Purple */
|
|
||||||
.hljs-keyword,
|
|
||||||
.hljs-selector-tag {
|
|
||||||
color: #b854d4;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs {
|
|
||||||
display: block;
|
|
||||||
overflow-x: auto;
|
|
||||||
background: #f1f1f1;
|
|
||||||
color: #6e6b5e;
|
|
||||||
padding: 0.5em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-emphasis {
|
|
||||||
font-style: italic;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-strong {
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-addition {
|
|
||||||
color: #22863a;
|
|
||||||
background-color: #f0fff4;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-deletion {
|
|
||||||
color: #b31d28;
|
|
||||||
background-color: #ffeef0;
|
|
||||||
}
|
|
File diff suppressed because one or more lines are too long
|
@ -1,271 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Introduction - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/variables.css">
|
|
||||||
<link rel="stylesheet" href="css/general.css">
|
|
||||||
<link rel="stylesheet" href="css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="highlight.css">
|
|
||||||
<link rel="stylesheet" href="tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html" class="active">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#juniper" id="juniper">Juniper</a></h1>
|
|
||||||
<p>Juniper is a <a href="http://graphql.org">GraphQL</a> server library for Rust. Build type-safe and fast API
|
|
||||||
servers with minimal boilerplate and configuration.</p>
|
|
||||||
<p><a href="http://graphql.org">GraphQL</a> is a data query language developed by Facebook intended to
|
|
||||||
serve mobile and web application frontends.</p>
|
|
||||||
<p><em>Juniper</em> makes it possible to write GraphQL servers in Rust that are
|
|
||||||
type-safe and blazingly fast. We also try to make declaring and resolving
|
|
||||||
GraphQL schemas as convenient as possible as Rust will allow.</p>
|
|
||||||
<p>Juniper does not include a web server - instead it provides building blocks to
|
|
||||||
make integration with existing servers straightforward. It optionally provides a
|
|
||||||
pre-built integration for the <a href="https://hyper.rs">Hyper</a>, <a href="http://ironframework.io">Iron</a>, <a href="https://rocket.rs">Rocket</a>, and <a href="https://github.com/seanmonstar/warp">Warp</a> frameworks, including
|
|
||||||
embedded <a href="https://github.com/graphql/graphiql">Graphiql</a> for easy debugging.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://crates.io/crates/juniper">Cargo crate</a></li>
|
|
||||||
<li><a href="https://docs.rs/juniper">API Reference</a></li>
|
|
||||||
</ul>
|
|
||||||
<h2><a class="header" href="#features" id="features">Features</a></h2>
|
|
||||||
<p>Juniper supports the full GraphQL query language according to the
|
|
||||||
<a href="http://facebook.github.io/graphql">specification</a>, including interfaces, unions, schema
|
|
||||||
introspection, and validations.
|
|
||||||
It does not, however, support the schema language.</p>
|
|
||||||
<p>As an exception to other GraphQL libraries for other languages, Juniper builds
|
|
||||||
non-null types by default. A field of type <code>Vec<Episode></code> will be converted into
|
|
||||||
<code>[Episode!]!</code>. The corresponding Rust type for e.g. <code>[Episode]</code> would be
|
|
||||||
<code>Option<Vec<Option<Episode>>></code>.</p>
|
|
||||||
<h2><a class="header" href="#integrations" id="integrations">Integrations</a></h2>
|
|
||||||
<h3><a class="header" href="#data-types" id="data-types">Data types</a></h3>
|
|
||||||
<p>Juniper has automatic integration with some very common Rust crates to make
|
|
||||||
building schemas a breeze. The types from these crates will be usable in
|
|
||||||
your Schemas automatically.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://crates.io/crates/uuid">uuid</a></li>
|
|
||||||
<li><a href="https://crates.io/crates/url">url</a></li>
|
|
||||||
<li><a href="https://crates.io/crates/chrono">chrono</a></li>
|
|
||||||
<li><a href="https://crates.io/crates/bson">bson</a></li>
|
|
||||||
</ul>
|
|
||||||
<h3><a class="header" href="#web-frameworks" id="web-frameworks">Web Frameworks</a></h3>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://hyper.rs">hyper</a></li>
|
|
||||||
<li><a href="https://rocket.rs">rocket</a></li>
|
|
||||||
<li><a href="http://ironframework.io">iron</a></li>
|
|
||||||
<li><a href="https://github.com/seanmonstar/warp">warp</a></li>
|
|
||||||
</ul>
|
|
||||||
<h2><a class="header" href="#api-stability" id="api-stability">API Stability</a></h2>
|
|
||||||
<p>Juniper has not reached 1.0 yet, thus some API instability should be expected.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="quickstart.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="quickstart.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
7
current/mark.min.js
vendored
7
current/mark.min.js
vendored
File diff suppressed because one or more lines are too long
2362
current/print.html
2362
current/print.html
File diff suppressed because it is too large
Load diff
|
@ -1,420 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Quickstart - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/variables.css">
|
|
||||||
<link rel="stylesheet" href="css/general.css">
|
|
||||||
<link rel="stylesheet" href="css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="highlight.css">
|
|
||||||
<link rel="stylesheet" href="tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html" class="active">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#quickstart" id="quickstart">Quickstart</a></h1>
|
|
||||||
<p>This page will give you a short introduction to the concepts in Juniper.</p>
|
|
||||||
<p>Juniper follows a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">code-first approach</a> to defining GraphQL schemas. If you would like to use a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">schema-first approach</a> instead, consider <a href="https://github.com/davidpdrsn/juniper-from-schema">juniper-from-schema</a> for generating code from a schema file.</p>
|
|
||||||
<h2><a class="header" href="#installation" id="installation">Installation</a></h2>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = { git = "https://github.com/graphql-rust/juniper" }
|
|
||||||
</code></pre>
|
|
||||||
<h2><a class="header" href="#schema-example" id="schema-example">Schema example</a></h2>
|
|
||||||
<p>Exposing simple enums and structs as GraphQL is just a matter of adding a custom
|
|
||||||
derive attribute to them. Juniper includes support for basic Rust types that
|
|
||||||
naturally map to GraphQL features, such as <code>Option<T></code>, <code>Vec<T></code>, <code>Box<T></code>,
|
|
||||||
<code>String</code>, <code>f64</code>, and <code>i32</code>, references, and slices.</p>
|
|
||||||
<p>For more advanced mappings, Juniper provides multiple macros to map your Rust
|
|
||||||
types to a GraphQL schema. The most important one is the
|
|
||||||
<a href="https://docs.rs/juniper/latest/juniper/macro.graphql_object.html">graphql_object</a> procedural macro that is used for declaring an object with
|
|
||||||
resolvers, which you will use for the <code>Query</code> and <code>Mutation</code> roots.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::fmt::Display;
|
|
||||||
</span>use juniper::{
|
|
||||||
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
|
|
||||||
GraphQLInputObject, GraphQLObject, ScalarValue,
|
|
||||||
};
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">struct DatabasePool;
|
|
||||||
</span><span class="boring">impl DatabasePool {
|
|
||||||
</span><span class="boring"> fn get_connection(&self) -> FieldResult<DatabasePool> { Ok(DatabasePool) }
|
|
||||||
</span><span class="boring"> fn find_human(&self, _id: &str) -> FieldResult<Human> { Err("")? }
|
|
||||||
</span><span class="boring"> fn insert_human(&self, _human: &NewHuman) -> FieldResult<Human> { Err("")? }
|
|
||||||
</span><span class="boring">}
|
|
||||||
</span>
|
|
||||||
#[derive(GraphQLEnum)]
|
|
||||||
enum Episode {
|
|
||||||
NewHope,
|
|
||||||
Empire,
|
|
||||||
Jedi,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(description = "A humanoid creature in the Star Wars universe")]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
name: String,
|
|
||||||
appears_in: Vec<Episode>,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// There is also a custom derive for mapping GraphQL input objects.
|
|
||||||
|
|
||||||
#[derive(GraphQLInputObject)]
|
|
||||||
#[graphql(description = "A humanoid creature in the Star Wars universe")]
|
|
||||||
struct NewHuman {
|
|
||||||
name: String,
|
|
||||||
appears_in: Vec<Episode>,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now, we create our root Query and Mutation types with resolvers by using the
|
|
||||||
// object macro.
|
|
||||||
// Objects can have contexts that allow accessing shared state like a database
|
|
||||||
// pool.
|
|
||||||
|
|
||||||
struct Context {
|
|
||||||
// Use your real database pool here.
|
|
||||||
pool: DatabasePool,
|
|
||||||
}
|
|
||||||
|
|
||||||
// To make our context usable by Juniper, we have to implement a marker trait.
|
|
||||||
impl juniper::Context for Context {}
|
|
||||||
|
|
||||||
struct Query;
|
|
||||||
|
|
||||||
#[graphql_object(
|
|
||||||
// Here we specify the context type for the object.
|
|
||||||
// We need to do this in every type that
|
|
||||||
// needs access to the context.
|
|
||||||
context = Context,
|
|
||||||
)]
|
|
||||||
impl Query {
|
|
||||||
fn apiVersion() -> &str {
|
|
||||||
"1.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arguments to resolvers can either be simple types or input objects.
|
|
||||||
// To gain access to the context, we specify a argument
|
|
||||||
// that is a reference to the Context type.
|
|
||||||
// Juniper automatically injects the correct context here.
|
|
||||||
fn human(context: &Context, id: String) -> FieldResult<Human> {
|
|
||||||
// Get a db connection.
|
|
||||||
let connection = context.pool.get_connection()?;
|
|
||||||
// Execute a db query.
|
|
||||||
// Note the use of `?` to propagate errors.
|
|
||||||
let human = connection.find_human(&id)?;
|
|
||||||
// Return the result.
|
|
||||||
Ok(human)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now, we do the same for our Mutation type.
|
|
||||||
|
|
||||||
struct Mutation;
|
|
||||||
|
|
||||||
#[graphql_object(
|
|
||||||
context = Context,
|
|
||||||
|
|
||||||
// If we need to use `ScalarValue` parametrization explicitly somewhere
|
|
||||||
// in the object definition (like here in `FieldResult`), we should
|
|
||||||
// declare an explicit type parameter for that, and specify it.
|
|
||||||
scalar = S,
|
|
||||||
)]
|
|
||||||
impl<S: ScalarValue + Display> Mutation {
|
|
||||||
fn createHuman(context: &Context, new_human: NewHuman) -> FieldResult<Human, S> {
|
|
||||||
let db = context.pool.get_connection().map_err(|e| e.map_scalar_value())?;
|
|
||||||
let human: Human = db.insert_human(&new_human).map_err(|e| e.map_scalar_value())?;
|
|
||||||
Ok(human)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A root schema consists of a query, a mutation, and a subscription.
|
|
||||||
// Request queries can be executed against a RootNode.
|
|
||||||
type Schema = juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {
|
|
||||||
</span><span class="boring"> let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
|
|
||||||
</span><span class="boring">}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>We now have a very simple but functional schema for a GraphQL server!</p>
|
|
||||||
<p>To actually serve the schema, see the guides for our various <a href="./servers/index.html">server integrations</a>.</p>
|
|
||||||
<p>Juniper is a library that can be used in many contexts--it does not require a server and it does not have a dependency on a particular transport or serialization format. You can invoke the executor directly to get a result for a query:</p>
|
|
||||||
<h2><a class="header" href="#executor" id="executor">Executor</a></h2>
|
|
||||||
<p>You can invoke <code>juniper::execute</code> directly to run a GraphQL query:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">// Only needed due to 2018 edition because the macro is not accessible.
|
|
||||||
</span><span class="boring">#[macro_use] extern crate juniper;
|
|
||||||
</span>use juniper::{
|
|
||||||
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
|
|
||||||
GraphQLEnum, Variables,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(GraphQLEnum, Clone, Copy)]
|
|
||||||
enum Episode {
|
|
||||||
NewHope,
|
|
||||||
Empire,
|
|
||||||
Jedi,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Arbitrary context data.
|
|
||||||
struct Ctx(Episode);
|
|
||||||
|
|
||||||
impl juniper::Context for Ctx {}
|
|
||||||
|
|
||||||
struct Query;
|
|
||||||
|
|
||||||
#[graphql_object(context = Ctx)]
|
|
||||||
impl Query {
|
|
||||||
fn favoriteEpisode(context: &Ctx) -> FieldResult<Episode> {
|
|
||||||
Ok(context.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// A root schema consists of a query, a mutation, and a subscription.
|
|
||||||
// Request queries can be executed against a RootNode.
|
|
||||||
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// Create a context object.
|
|
||||||
let ctx = Ctx(Episode::NewHope);
|
|
||||||
|
|
||||||
// Run the executor.
|
|
||||||
let (res, _errors) = juniper::execute_sync(
|
|
||||||
"query { favoriteEpisode }",
|
|
||||||
None,
|
|
||||||
&Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
|
|
||||||
&Variables::new(),
|
|
||||||
&ctx,
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
// Ensure the value matches.
|
|
||||||
assert_eq!(
|
|
||||||
res,
|
|
||||||
graphql_value!({
|
|
||||||
"favoriteEpisode": "NEW_HOPE",
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
</code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="types/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="types/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,477 +0,0 @@
|
||||||
"use strict";
|
|
||||||
window.search = window.search || {};
|
|
||||||
(function search(search) {
|
|
||||||
// Search functionality
|
|
||||||
//
|
|
||||||
// You can use !hasFocus() to prevent keyhandling in your key
|
|
||||||
// event handlers while the user is typing their search.
|
|
||||||
|
|
||||||
if (!Mark || !elasticlunr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//IE 11 Compatibility from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
|
|
||||||
if (!String.prototype.startsWith) {
|
|
||||||
String.prototype.startsWith = function(search, pos) {
|
|
||||||
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
var search_wrap = document.getElementById('search-wrapper'),
|
|
||||||
searchbar = document.getElementById('searchbar'),
|
|
||||||
searchbar_outer = document.getElementById('searchbar-outer'),
|
|
||||||
searchresults = document.getElementById('searchresults'),
|
|
||||||
searchresults_outer = document.getElementById('searchresults-outer'),
|
|
||||||
searchresults_header = document.getElementById('searchresults-header'),
|
|
||||||
searchicon = document.getElementById('search-toggle'),
|
|
||||||
content = document.getElementById('content'),
|
|
||||||
|
|
||||||
searchindex = null,
|
|
||||||
doc_urls = [],
|
|
||||||
results_options = {
|
|
||||||
teaser_word_count: 30,
|
|
||||||
limit_results: 30,
|
|
||||||
},
|
|
||||||
search_options = {
|
|
||||||
bool: "AND",
|
|
||||||
expand: true,
|
|
||||||
fields: {
|
|
||||||
title: {boost: 1},
|
|
||||||
body: {boost: 1},
|
|
||||||
breadcrumbs: {boost: 0}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
mark_exclude = [],
|
|
||||||
marker = new Mark(content),
|
|
||||||
current_searchterm = "",
|
|
||||||
URL_SEARCH_PARAM = 'search',
|
|
||||||
URL_MARK_PARAM = 'highlight',
|
|
||||||
teaser_count = 0,
|
|
||||||
|
|
||||||
SEARCH_HOTKEY_KEYCODE = 83,
|
|
||||||
ESCAPE_KEYCODE = 27,
|
|
||||||
DOWN_KEYCODE = 40,
|
|
||||||
UP_KEYCODE = 38,
|
|
||||||
SELECT_KEYCODE = 13;
|
|
||||||
|
|
||||||
function hasFocus() {
|
|
||||||
return searchbar === document.activeElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeChildren(elem) {
|
|
||||||
while (elem.firstChild) {
|
|
||||||
elem.removeChild(elem.firstChild);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to parse a url into its building blocks.
|
|
||||||
function parseURL(url) {
|
|
||||||
var a = document.createElement('a');
|
|
||||||
a.href = url;
|
|
||||||
return {
|
|
||||||
source: url,
|
|
||||||
protocol: a.protocol.replace(':',''),
|
|
||||||
host: a.hostname,
|
|
||||||
port: a.port,
|
|
||||||
params: (function(){
|
|
||||||
var ret = {};
|
|
||||||
var seg = a.search.replace(/^\?/,'').split('&');
|
|
||||||
var len = seg.length, i = 0, s;
|
|
||||||
for (;i<len;i++) {
|
|
||||||
if (!seg[i]) { continue; }
|
|
||||||
s = seg[i].split('=');
|
|
||||||
ret[s[0]] = s[1];
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
})(),
|
|
||||||
file: (a.pathname.match(/\/([^/?#]+)$/i) || [,''])[1],
|
|
||||||
hash: a.hash.replace('#',''),
|
|
||||||
path: a.pathname.replace(/^([^/])/,'/$1')
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to recreate a url string from its building blocks.
|
|
||||||
function renderURL(urlobject) {
|
|
||||||
var url = urlobject.protocol + "://" + urlobject.host;
|
|
||||||
if (urlobject.port != "") {
|
|
||||||
url += ":" + urlobject.port;
|
|
||||||
}
|
|
||||||
url += urlobject.path;
|
|
||||||
var joiner = "?";
|
|
||||||
for(var prop in urlobject.params) {
|
|
||||||
if(urlobject.params.hasOwnProperty(prop)) {
|
|
||||||
url += joiner + prop + "=" + urlobject.params[prop];
|
|
||||||
joiner = "&";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (urlobject.hash != "") {
|
|
||||||
url += "#" + urlobject.hash;
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to escape html special chars for displaying the teasers
|
|
||||||
var escapeHTML = (function() {
|
|
||||||
var MAP = {
|
|
||||||
'&': '&',
|
|
||||||
'<': '<',
|
|
||||||
'>': '>',
|
|
||||||
'"': '"',
|
|
||||||
"'": '''
|
|
||||||
};
|
|
||||||
var repl = function(c) { return MAP[c]; };
|
|
||||||
return function(s) {
|
|
||||||
return s.replace(/[&<>'"]/g, repl);
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
function formatSearchMetric(count, searchterm) {
|
|
||||||
if (count == 1) {
|
|
||||||
return count + " search result for '" + searchterm + "':";
|
|
||||||
} else if (count == 0) {
|
|
||||||
return "No search results for '" + searchterm + "'.";
|
|
||||||
} else {
|
|
||||||
return count + " search results for '" + searchterm + "':";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatSearchResult(result, searchterms) {
|
|
||||||
var teaser = makeTeaser(escapeHTML(result.doc.body), searchterms);
|
|
||||||
teaser_count++;
|
|
||||||
|
|
||||||
// The ?URL_MARK_PARAM= parameter belongs inbetween the page and the #heading-anchor
|
|
||||||
var url = doc_urls[result.ref].split("#");
|
|
||||||
if (url.length == 1) { // no anchor found
|
|
||||||
url.push("");
|
|
||||||
}
|
|
||||||
|
|
||||||
return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]
|
|
||||||
+ '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'
|
|
||||||
+ '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">'
|
|
||||||
+ teaser + '</span>';
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeTeaser(body, searchterms) {
|
|
||||||
// The strategy is as follows:
|
|
||||||
// First, assign a value to each word in the document:
|
|
||||||
// Words that correspond to search terms (stemmer aware): 40
|
|
||||||
// Normal words: 2
|
|
||||||
// First word in a sentence: 8
|
|
||||||
// Then use a sliding window with a constant number of words and count the
|
|
||||||
// sum of the values of the words within the window. Then use the window that got the
|
|
||||||
// maximum sum. If there are multiple maximas, then get the last one.
|
|
||||||
// Enclose the terms in <em>.
|
|
||||||
var stemmed_searchterms = searchterms.map(function(w) {
|
|
||||||
return elasticlunr.stemmer(w.toLowerCase());
|
|
||||||
});
|
|
||||||
var searchterm_weight = 40;
|
|
||||||
var weighted = []; // contains elements of ["word", weight, index_in_document]
|
|
||||||
// split in sentences, then words
|
|
||||||
var sentences = body.toLowerCase().split('. ');
|
|
||||||
var index = 0;
|
|
||||||
var value = 0;
|
|
||||||
var searchterm_found = false;
|
|
||||||
for (var sentenceindex in sentences) {
|
|
||||||
var words = sentences[sentenceindex].split(' ');
|
|
||||||
value = 8;
|
|
||||||
for (var wordindex in words) {
|
|
||||||
var word = words[wordindex];
|
|
||||||
if (word.length > 0) {
|
|
||||||
for (var searchtermindex in stemmed_searchterms) {
|
|
||||||
if (elasticlunr.stemmer(word).startsWith(stemmed_searchterms[searchtermindex])) {
|
|
||||||
value = searchterm_weight;
|
|
||||||
searchterm_found = true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
weighted.push([word, value, index]);
|
|
||||||
value = 2;
|
|
||||||
}
|
|
||||||
index += word.length;
|
|
||||||
index += 1; // ' ' or '.' if last word in sentence
|
|
||||||
};
|
|
||||||
index += 1; // because we split at a two-char boundary '. '
|
|
||||||
};
|
|
||||||
|
|
||||||
if (weighted.length == 0) {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
var window_weight = [];
|
|
||||||
var window_size = Math.min(weighted.length, results_options.teaser_word_count);
|
|
||||||
|
|
||||||
var cur_sum = 0;
|
|
||||||
for (var wordindex = 0; wordindex < window_size; wordindex++) {
|
|
||||||
cur_sum += weighted[wordindex][1];
|
|
||||||
};
|
|
||||||
window_weight.push(cur_sum);
|
|
||||||
for (var wordindex = 0; wordindex < weighted.length - window_size; wordindex++) {
|
|
||||||
cur_sum -= weighted[wordindex][1];
|
|
||||||
cur_sum += weighted[wordindex + window_size][1];
|
|
||||||
window_weight.push(cur_sum);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (searchterm_found) {
|
|
||||||
var max_sum = 0;
|
|
||||||
var max_sum_window_index = 0;
|
|
||||||
// backwards
|
|
||||||
for (var i = window_weight.length - 1; i >= 0; i--) {
|
|
||||||
if (window_weight[i] > max_sum) {
|
|
||||||
max_sum = window_weight[i];
|
|
||||||
max_sum_window_index = i;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
max_sum_window_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add <em/> around searchterms
|
|
||||||
var teaser_split = [];
|
|
||||||
var index = weighted[max_sum_window_index][2];
|
|
||||||
for (var i = max_sum_window_index; i < max_sum_window_index+window_size; i++) {
|
|
||||||
var word = weighted[i];
|
|
||||||
if (index < word[2]) {
|
|
||||||
// missing text from index to start of `word`
|
|
||||||
teaser_split.push(body.substring(index, word[2]));
|
|
||||||
index = word[2];
|
|
||||||
}
|
|
||||||
if (word[1] == searchterm_weight) {
|
|
||||||
teaser_split.push("<em>")
|
|
||||||
}
|
|
||||||
index = word[2] + word[0].length;
|
|
||||||
teaser_split.push(body.substring(word[2], index));
|
|
||||||
if (word[1] == searchterm_weight) {
|
|
||||||
teaser_split.push("</em>")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return teaser_split.join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
function init(config) {
|
|
||||||
results_options = config.results_options;
|
|
||||||
search_options = config.search_options;
|
|
||||||
searchbar_outer = config.searchbar_outer;
|
|
||||||
doc_urls = config.doc_urls;
|
|
||||||
searchindex = elasticlunr.Index.load(config.index);
|
|
||||||
|
|
||||||
// Set up events
|
|
||||||
searchicon.addEventListener('click', function(e) { searchIconClickHandler(); }, false);
|
|
||||||
searchbar.addEventListener('keyup', function(e) { searchbarKeyUpHandler(); }, false);
|
|
||||||
document.addEventListener('keydown', function(e) { globalKeyHandler(e); }, false);
|
|
||||||
// If the user uses the browser buttons, do the same as if a reload happened
|
|
||||||
window.onpopstate = function(e) { doSearchOrMarkFromUrl(); };
|
|
||||||
// Suppress "submit" events so the page doesn't reload when the user presses Enter
|
|
||||||
document.addEventListener('submit', function(e) { e.preventDefault(); }, false);
|
|
||||||
|
|
||||||
// If reloaded, do the search or mark again, depending on the current url parameters
|
|
||||||
doSearchOrMarkFromUrl();
|
|
||||||
}
|
|
||||||
|
|
||||||
function unfocusSearchbar() {
|
|
||||||
// hacky, but just focusing a div only works once
|
|
||||||
var tmp = document.createElement('input');
|
|
||||||
tmp.setAttribute('style', 'position: absolute; opacity: 0;');
|
|
||||||
searchicon.appendChild(tmp);
|
|
||||||
tmp.focus();
|
|
||||||
tmp.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
// On reload or browser history backwards/forwards events, parse the url and do search or mark
|
|
||||||
function doSearchOrMarkFromUrl() {
|
|
||||||
// Check current URL for search request
|
|
||||||
var url = parseURL(window.location.href);
|
|
||||||
if (url.params.hasOwnProperty(URL_SEARCH_PARAM)
|
|
||||||
&& url.params[URL_SEARCH_PARAM] != "") {
|
|
||||||
showSearch(true);
|
|
||||||
searchbar.value = decodeURIComponent(
|
|
||||||
(url.params[URL_SEARCH_PARAM]+'').replace(/\+/g, '%20'));
|
|
||||||
searchbarKeyUpHandler(); // -> doSearch()
|
|
||||||
} else {
|
|
||||||
showSearch(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (url.params.hasOwnProperty(URL_MARK_PARAM)) {
|
|
||||||
var words = url.params[URL_MARK_PARAM].split(' ');
|
|
||||||
marker.mark(words, {
|
|
||||||
exclude: mark_exclude
|
|
||||||
});
|
|
||||||
|
|
||||||
var markers = document.querySelectorAll("mark");
|
|
||||||
function hide() {
|
|
||||||
for (var i = 0; i < markers.length; i++) {
|
|
||||||
markers[i].classList.add("fade-out");
|
|
||||||
window.setTimeout(function(e) { marker.unmark(); }, 300);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (var i = 0; i < markers.length; i++) {
|
|
||||||
markers[i].addEventListener('click', hide);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Eventhandler for keyevents on `document`
|
|
||||||
function globalKeyHandler(e) {
|
|
||||||
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text') { return; }
|
|
||||||
|
|
||||||
if (e.keyCode === ESCAPE_KEYCODE) {
|
|
||||||
e.preventDefault();
|
|
||||||
searchbar.classList.remove("active");
|
|
||||||
setSearchUrlParameters("",
|
|
||||||
(searchbar.value.trim() !== "") ? "push" : "replace");
|
|
||||||
if (hasFocus()) {
|
|
||||||
unfocusSearchbar();
|
|
||||||
}
|
|
||||||
showSearch(false);
|
|
||||||
marker.unmark();
|
|
||||||
} else if (!hasFocus() && e.keyCode === SEARCH_HOTKEY_KEYCODE) {
|
|
||||||
e.preventDefault();
|
|
||||||
showSearch(true);
|
|
||||||
window.scrollTo(0, 0);
|
|
||||||
searchbar.select();
|
|
||||||
} else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
|
|
||||||
e.preventDefault();
|
|
||||||
unfocusSearchbar();
|
|
||||||
searchresults.firstElementChild.classList.add("focus");
|
|
||||||
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
|
|
||||||
|| e.keyCode === UP_KEYCODE
|
|
||||||
|| e.keyCode === SELECT_KEYCODE)) {
|
|
||||||
// not `:focus` because browser does annoying scrolling
|
|
||||||
var focused = searchresults.querySelector("li.focus");
|
|
||||||
if (!focused) return;
|
|
||||||
e.preventDefault();
|
|
||||||
if (e.keyCode === DOWN_KEYCODE) {
|
|
||||||
var next = focused.nextElementSibling;
|
|
||||||
if (next) {
|
|
||||||
focused.classList.remove("focus");
|
|
||||||
next.classList.add("focus");
|
|
||||||
}
|
|
||||||
} else if (e.keyCode === UP_KEYCODE) {
|
|
||||||
focused.classList.remove("focus");
|
|
||||||
var prev = focused.previousElementSibling;
|
|
||||||
if (prev) {
|
|
||||||
prev.classList.add("focus");
|
|
||||||
} else {
|
|
||||||
searchbar.select();
|
|
||||||
}
|
|
||||||
} else { // SELECT_KEYCODE
|
|
||||||
window.location.assign(focused.querySelector('a'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showSearch(yes) {
|
|
||||||
if (yes) {
|
|
||||||
search_wrap.classList.remove('hidden');
|
|
||||||
searchicon.setAttribute('aria-expanded', 'true');
|
|
||||||
} else {
|
|
||||||
search_wrap.classList.add('hidden');
|
|
||||||
searchicon.setAttribute('aria-expanded', 'false');
|
|
||||||
var results = searchresults.children;
|
|
||||||
for (var i = 0; i < results.length; i++) {
|
|
||||||
results[i].classList.remove("focus");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showResults(yes) {
|
|
||||||
if (yes) {
|
|
||||||
searchresults_outer.classList.remove('hidden');
|
|
||||||
} else {
|
|
||||||
searchresults_outer.classList.add('hidden');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Eventhandler for search icon
|
|
||||||
function searchIconClickHandler() {
|
|
||||||
if (search_wrap.classList.contains('hidden')) {
|
|
||||||
showSearch(true);
|
|
||||||
window.scrollTo(0, 0);
|
|
||||||
searchbar.select();
|
|
||||||
} else {
|
|
||||||
showSearch(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Eventhandler for keyevents while the searchbar is focused
|
|
||||||
function searchbarKeyUpHandler() {
|
|
||||||
var searchterm = searchbar.value.trim();
|
|
||||||
if (searchterm != "") {
|
|
||||||
searchbar.classList.add("active");
|
|
||||||
doSearch(searchterm);
|
|
||||||
} else {
|
|
||||||
searchbar.classList.remove("active");
|
|
||||||
showResults(false);
|
|
||||||
removeChildren(searchresults);
|
|
||||||
}
|
|
||||||
|
|
||||||
setSearchUrlParameters(searchterm, "push_if_new_search_else_replace");
|
|
||||||
|
|
||||||
// Remove marks
|
|
||||||
marker.unmark();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update current url with ?URL_SEARCH_PARAM= parameter, remove ?URL_MARK_PARAM and #heading-anchor .
|
|
||||||
// `action` can be one of "push", "replace", "push_if_new_search_else_replace"
|
|
||||||
// and replaces or pushes a new browser history item.
|
|
||||||
// "push_if_new_search_else_replace" pushes if there is no `?URL_SEARCH_PARAM=abc` yet.
|
|
||||||
function setSearchUrlParameters(searchterm, action) {
|
|
||||||
var url = parseURL(window.location.href);
|
|
||||||
var first_search = ! url.params.hasOwnProperty(URL_SEARCH_PARAM);
|
|
||||||
if (searchterm != "" || action == "push_if_new_search_else_replace") {
|
|
||||||
url.params[URL_SEARCH_PARAM] = searchterm;
|
|
||||||
delete url.params[URL_MARK_PARAM];
|
|
||||||
url.hash = "";
|
|
||||||
} else {
|
|
||||||
delete url.params[URL_SEARCH_PARAM];
|
|
||||||
}
|
|
||||||
// A new search will also add a new history item, so the user can go back
|
|
||||||
// to the page prior to searching. A updated search term will only replace
|
|
||||||
// the url.
|
|
||||||
if (action == "push" || (action == "push_if_new_search_else_replace" && first_search) ) {
|
|
||||||
history.pushState({}, document.title, renderURL(url));
|
|
||||||
} else if (action == "replace" || (action == "push_if_new_search_else_replace" && !first_search) ) {
|
|
||||||
history.replaceState({}, document.title, renderURL(url));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function doSearch(searchterm) {
|
|
||||||
|
|
||||||
// Don't search the same twice
|
|
||||||
if (current_searchterm == searchterm) { return; }
|
|
||||||
else { current_searchterm = searchterm; }
|
|
||||||
|
|
||||||
if (searchindex == null) { return; }
|
|
||||||
|
|
||||||
// Do the actual search
|
|
||||||
var results = searchindex.search(searchterm, search_options);
|
|
||||||
var resultcount = Math.min(results.length, results_options.limit_results);
|
|
||||||
|
|
||||||
// Display search metrics
|
|
||||||
searchresults_header.innerText = formatSearchMetric(resultcount, searchterm);
|
|
||||||
|
|
||||||
// Clear and insert results
|
|
||||||
var searchterms = searchterm.split(' ');
|
|
||||||
removeChildren(searchresults);
|
|
||||||
for(var i = 0; i < resultcount ; i++){
|
|
||||||
var resultElem = document.createElement('li');
|
|
||||||
resultElem.innerHTML = formatSearchResult(results[i], searchterms);
|
|
||||||
searchresults.appendChild(resultElem);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display results
|
|
||||||
showResults(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch(path_to_root + 'searchindex.json')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(json => init(json))
|
|
||||||
.catch(error => { // Try to load searchindex.js if fetch failed
|
|
||||||
var script = document.createElement('script');
|
|
||||||
script.src = path_to_root + 'searchindex.js';
|
|
||||||
script.onload = () => init(window.search);
|
|
||||||
document.head.appendChild(script);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Exported functions
|
|
||||||
search.hasFocus = hasFocus;
|
|
||||||
})(window.search);
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,3 +0,0 @@
|
||||||
.markdown-section pre {
|
|
||||||
line-height: 1.3;
|
|
||||||
}
|
|
|
@ -1,104 +0,0 @@
|
||||||
/* Tomorrow Night Theme */
|
|
||||||
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
|
|
||||||
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
|
|
||||||
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
|
|
||||||
|
|
||||||
/* Tomorrow Comment */
|
|
||||||
.hljs-comment {
|
|
||||||
color: #969896;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Red */
|
|
||||||
.hljs-variable,
|
|
||||||
.hljs-attribute,
|
|
||||||
.hljs-tag,
|
|
||||||
.hljs-regexp,
|
|
||||||
.ruby .hljs-constant,
|
|
||||||
.xml .hljs-tag .hljs-title,
|
|
||||||
.xml .hljs-pi,
|
|
||||||
.xml .hljs-doctype,
|
|
||||||
.html .hljs-doctype,
|
|
||||||
.css .hljs-id,
|
|
||||||
.css .hljs-class,
|
|
||||||
.css .hljs-pseudo {
|
|
||||||
color: #cc6666;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Orange */
|
|
||||||
.hljs-number,
|
|
||||||
.hljs-preprocessor,
|
|
||||||
.hljs-pragma,
|
|
||||||
.hljs-built_in,
|
|
||||||
.hljs-literal,
|
|
||||||
.hljs-params,
|
|
||||||
.hljs-constant {
|
|
||||||
color: #de935f;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Yellow */
|
|
||||||
.ruby .hljs-class .hljs-title,
|
|
||||||
.css .hljs-rule .hljs-attribute {
|
|
||||||
color: #f0c674;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Green */
|
|
||||||
.hljs-string,
|
|
||||||
.hljs-value,
|
|
||||||
.hljs-inheritance,
|
|
||||||
.hljs-header,
|
|
||||||
.hljs-name,
|
|
||||||
.ruby .hljs-symbol,
|
|
||||||
.xml .hljs-cdata {
|
|
||||||
color: #b5bd68;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Aqua */
|
|
||||||
.hljs-title,
|
|
||||||
.css .hljs-hexcolor {
|
|
||||||
color: #8abeb7;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Blue */
|
|
||||||
.hljs-function,
|
|
||||||
.python .hljs-decorator,
|
|
||||||
.python .hljs-title,
|
|
||||||
.ruby .hljs-function .hljs-title,
|
|
||||||
.ruby .hljs-title .hljs-keyword,
|
|
||||||
.perl .hljs-sub,
|
|
||||||
.javascript .hljs-title,
|
|
||||||
.coffeescript .hljs-title {
|
|
||||||
color: #81a2be;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tomorrow Purple */
|
|
||||||
.hljs-keyword,
|
|
||||||
.javascript .hljs-function {
|
|
||||||
color: #b294bb;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs {
|
|
||||||
display: block;
|
|
||||||
overflow-x: auto;
|
|
||||||
background: #1d1f21;
|
|
||||||
color: #c5c8c6;
|
|
||||||
padding: 0.5em;
|
|
||||||
-webkit-text-size-adjust: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.coffeescript .javascript,
|
|
||||||
.javascript .xml,
|
|
||||||
.tex .hljs-formula,
|
|
||||||
.xml .javascript,
|
|
||||||
.xml .vbscript,
|
|
||||||
.xml .css,
|
|
||||||
.xml .hljs-cdata {
|
|
||||||
opacity: 0.5;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-addition {
|
|
||||||
color: #718c00;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hljs-deletion {
|
|
||||||
color: #c82829;
|
|
||||||
}
|
|
|
@ -1,295 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Enums - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html" class="active"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#enums" id="enums">Enums</a></h1>
|
|
||||||
<p>Enums in GraphQL are string constants grouped together to represent a set of
|
|
||||||
possible values. Simple Rust enums can be converted to GraphQL enums by using a
|
|
||||||
custom derive attribute:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLEnum)]
|
|
||||||
enum Episode {
|
|
||||||
NewHope,
|
|
||||||
Empire,
|
|
||||||
Jedi,
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Juniper converts all enum variants to uppercase, so the corresponding string
|
|
||||||
values for these variants are <code>NEWHOPE</code>, <code>EMPIRE</code>, and <code>JEDI</code>, respectively. If
|
|
||||||
you want to override this, you can use the <code>graphql</code> attribute, similar to how
|
|
||||||
it works when <a href="objects/defining_objects.html">defining objects</a>:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLEnum)]
|
|
||||||
enum Episode {
|
|
||||||
#[graphql(name="NEW_HOPE")]
|
|
||||||
NewHope,
|
|
||||||
Empire,
|
|
||||||
Jedi,
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#documentation-and-deprecation" id="documentation-and-deprecation">Documentation and deprecation</a></h2>
|
|
||||||
<p>Just like when defining objects, the type itself can be renamed and documented,
|
|
||||||
while individual enum variants can be renamed, documented, and deprecated:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLEnum)]
|
|
||||||
#[graphql(name="Episode", description="An episode of Star Wars")]
|
|
||||||
enum StarWarsEpisode {
|
|
||||||
#[graphql(deprecated="We don't really talk about this one")]
|
|
||||||
ThePhantomMenace,
|
|
||||||
|
|
||||||
#[graphql(name="NEW_HOPE")]
|
|
||||||
NewHope,
|
|
||||||
|
|
||||||
#[graphql(description="Arguably the best one in the trilogy")]
|
|
||||||
Empire,
|
|
||||||
Jedi,
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#supported-macro-attributes-derive" id="supported-macro-attributes-derive">Supported Macro Attributes (Derive)</a></h2>
|
|
||||||
<table><thead><tr><th>Name of Attribute</th><th align="center">Container Support</th><th align="center">Field Support</th></tr></thead><tbody>
|
|
||||||
<tr><td>context</td><td align="center">✔</td><td align="center">?</td></tr>
|
|
||||||
<tr><td>deprecated</td><td align="center">✔</td><td align="center">✔</td></tr>
|
|
||||||
<tr><td>description</td><td align="center">✔</td><td align="center">✔</td></tr>
|
|
||||||
<tr><td>interfaces</td><td align="center">?</td><td align="center">✘</td></tr>
|
|
||||||
<tr><td>name</td><td align="center">✔</td><td align="center">✔</td></tr>
|
|
||||||
<tr><td>noasync</td><td align="center">✔</td><td align="center">?</td></tr>
|
|
||||||
<tr><td>scalar</td><td align="center">✘</td><td align="center">?</td></tr>
|
|
||||||
<tr><td>skip</td><td align="center">?</td><td align="center">✘</td></tr>
|
|
||||||
<tr><td>✔: supported</td><td align="center">✘: not supported</td><td align="center">?: not available</td></tr>
|
|
||||||
</tbody></table>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/other-index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/interfaces.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/other-index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/interfaces.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,258 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Type System - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html" class="active">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#type-system" id="type-system">Type System</a></h1>
|
|
||||||
<p>Most of the work in working with juniper consists of mapping the
|
|
||||||
GraphQL type system to the Rust types your application uses.</p>
|
|
||||||
<p>Juniper provides some convenient abstractions that try to make this process
|
|
||||||
as painless as possible.</p>
|
|
||||||
<p>Find out more in the individual chapters below.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="objects/defining_objects.html">Defining objects</a>
|
|
||||||
<ul>
|
|
||||||
<li><a href="objects/complex_fields.html">Complex fields</a></li>
|
|
||||||
<li><a href="objects/using_contexts.html">Using contexts</a></li>
|
|
||||||
<li><a href="objects/error_handling.html">Error handling</a></li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
<li><a href="other-index.html">Other types</a>
|
|
||||||
<ul>
|
|
||||||
<li><a href="enums.html">Enums</a></li>
|
|
||||||
<li><a href="interfaces.html">Interfaces</a></li>
|
|
||||||
<li><a href="input_objects.html">Input objects</a></li>
|
|
||||||
<li><a href="scalars.html">Scalars</a></li>
|
|
||||||
<li><a href="unions.html">Unions</a></li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../quickstart.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/objects/defining_objects.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../quickstart.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/objects/defining_objects.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,289 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Input objects - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html" class="active"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#input-objects" id="input-objects">Input objects</a></h1>
|
|
||||||
<p>Input objects are complex data structures that can be used as arguments to
|
|
||||||
GraphQL fields. In Juniper, you can define input objects using a custom derive
|
|
||||||
attribute, similar to simple objects and enums:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLInputObject)]
|
|
||||||
struct Coordinate {
|
|
||||||
latitude: f64,
|
|
||||||
longitude: f64
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Root;
|
|
||||||
<span class="boring">#[derive(juniper::GraphQLObject)] struct User { name: String }
|
|
||||||
</span>
|
|
||||||
#[juniper::graphql_object]
|
|
||||||
impl Root {
|
|
||||||
fn users_at_location(coordinate: Coordinate, radius: f64) -> Vec<User> {
|
|
||||||
// Send coordinate to database
|
|
||||||
// ...
|
|
||||||
<span class="boring">unimplemented!()
|
|
||||||
</span> }
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#documentation-and-renaming" id="documentation-and-renaming">Documentation and renaming</a></h2>
|
|
||||||
<p>Just like the <a href="objects/defining_objects.html">other</a> <a href="enums.html">derives</a>, you can rename
|
|
||||||
and add documentation to both the type and the fields:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLInputObject)]
|
|
||||||
#[graphql(name="Coordinate", description="A position on the globe")]
|
|
||||||
struct WorldCoordinate {
|
|
||||||
#[graphql(name="lat", description="The latitude")]
|
|
||||||
latitude: f64,
|
|
||||||
|
|
||||||
#[graphql(name="long", description="The longitude")]
|
|
||||||
longitude: f64
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Root;
|
|
||||||
<span class="boring">#[derive(juniper::GraphQLObject)] struct User { name: String }
|
|
||||||
</span>
|
|
||||||
#[juniper::graphql_object]
|
|
||||||
impl Root {
|
|
||||||
fn users_at_location(coordinate: WorldCoordinate, radius: f64) -> Vec<User> {
|
|
||||||
// Send coordinate to database
|
|
||||||
// ...
|
|
||||||
<span class="boring">unimplemented!()
|
|
||||||
</span> }
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/interfaces.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/scalars.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/interfaces.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/scalars.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,629 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Interfaces - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html" class="active"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#interfaces" id="interfaces">Interfaces</a></h1>
|
|
||||||
<p><a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interfaces</a> map well to interfaces known from common object-oriented languages such as Java or C#, but Rust, unfortunately, has no concept that maps perfectly to them. The nearest analogue of <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interfaces</a> are Rust traits, and the main difference is that in GraphQL an <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interface type</a> serves both as an <em>abstraction</em> and a <em>boxed value (downcastable to concrete implementers)</em>, while in Rust, a trait is an <em>abstraction only</em> and <em>to represent such a boxed value a separate type is required</em>, like enum or trait object, because Rust trait doesn't represent a type itself, and so can have no values. This difference imposes some unintuitive and non-obvious corner cases when we try to express <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interfaces</a> in Rust, but on the other hand gives you full control over which type is backing your interface, and how it's resolved.</p>
|
|
||||||
<p>For implementing <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interfaces</a> Juniper provides the <code>#[graphql_interface]</code> macro.</p>
|
|
||||||
<h2><a class="header" href="#traits" id="traits">Traits</a></h2>
|
|
||||||
<p>Defining a trait is mandatory for defining a <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a>, because this is the <em>obvious</em> way we describe an <em>abstraction</em> in Rust. All <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interface</a> fields are defined as computed ones via trait methods.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::graphql_interface;
|
|
||||||
|
|
||||||
#[graphql_interface]
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>However, to return values of such <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interface</a>, we should provide its implementers and the Rust type representing a <em>boxed value of this trait</em>. The last one can be represented in two flavors: enum and <a href="https://doc.rust-lang.org/reference/types/trait-object.html">trait object</a>.</p>
|
|
||||||
<h3><a class="header" href="#enum-values-default" id="enum-values-default">Enum values (default)</a></h3>
|
|
||||||
<p>By default, Juniper generates an enum representing the values of the defined <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a>, and names it straightforwardly, <code>{Interface}Value</code>.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
#[graphql_interface(for = [Human, Droid])] // enumerating all implementers is mandatory
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue)] // notice enum name, NOT trait name
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface] // implementing requires macro attribute too, (°o°)!
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Droid {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {
|
|
||||||
</span>let human = Human { id: "human-32".to_owned() };
|
|
||||||
// Values type for interface has `From` implementations for all its implementers,
|
|
||||||
// so we don't need to bother with enum variant names.
|
|
||||||
let character: CharacterValue = human.into();
|
|
||||||
assert_eq!(character.id(), "human-32");
|
|
||||||
<span class="boring">}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Also, enum name can be specified explicitly, if desired.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
#[graphql_interface(enum = CharaterInterface, for = Human)]
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharaterInterface)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#trait-object-values" id="trait-object-values">Trait object values</a></h3>
|
|
||||||
<p>If, for some reason, we would like to use <a href="https://doc.rust-lang.org/reference/types/trait-object.html">trait objects</a> for representing <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interface</a> values incorporating dynamic dispatch, then it should be specified explicitly in the trait definition.</p>
|
|
||||||
<p>Downcasting <a href="https://doc.rust-lang.org/reference/types/trait-object.html">trait objects</a> in Rust is not that trivial, that's why macro transforms the trait definition slightly, imposing some additional type parameters under-the-hood.</p>
|
|
||||||
<blockquote>
|
|
||||||
<p><strong>NOTICE</strong>:<br />
|
|
||||||
A <strong>trait has to be <a href="https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety">object safe</a></strong>, because schema resolvers will need to return a <a href="https://doc.rust-lang.org/reference/types/trait-object.html">trait object</a> to specify a <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> behind it.</p>
|
|
||||||
</blockquote>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">extern crate tokio;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
// `dyn` argument accepts the name of type alias for the required trait object,
|
|
||||||
// and macro generates this alias automatically.
|
|
||||||
#[graphql_interface(dyn = DynCharacter, for = Human)]
|
|
||||||
trait Character {
|
|
||||||
async fn id(&self) -> &str; // async fields are supported natively
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = DynCharacter<__S>)] // macro adds `ScalarValue` type parameter to trait,
|
|
||||||
struct Human { // so it may be specified explicitly when required
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface(dyn)] // implementing requires to know about dynamic dispatch too
|
|
||||||
impl Character for Human {
|
|
||||||
async fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = DynCharacter<__S>)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Droid {
|
|
||||||
async fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">#[tokio::main]
|
|
||||||
</span><span class="boring">async fn main() {
|
|
||||||
</span>let human = Human { id: "human-32".to_owned() };
|
|
||||||
let character: Box<DynCharacter> = Box::new(human);
|
|
||||||
assert_eq!(character.id().await, "human-32");
|
|
||||||
<span class="boring">}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#ignoring-trait-methods" id="ignoring-trait-methods">Ignoring trait methods</a></h3>
|
|
||||||
<p>We may want to omit some trait methods to be assumed as <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> fields and ignore them.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
#[graphql_interface(for = Human)]
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
|
|
||||||
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
|
|
||||||
fn ignored(&self) -> u32 { 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#fields-arguments-and-interface-customization" id="fields-arguments-and-interface-customization">Fields, arguments and interface customization</a></h3>
|
|
||||||
<p>Similarly to <a href="https://spec.graphql.org/June2018/#sec-Objects">GraphQL objects</a> Juniper allows to fully customize <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interface</a> fields and their arguments.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(deprecated)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::graphql_interface;
|
|
||||||
|
|
||||||
// Renames the interface in GraphQL schema.
|
|
||||||
#[graphql_interface(name = "MyCharacter")]
|
|
||||||
// Describes the interface in GraphQL schema.
|
|
||||||
#[graphql_interface(description = "My own character.")]
|
|
||||||
// Usual Rust docs are supported too as GraphQL interface description,
|
|
||||||
// but `description` attribute argument takes precedence over them, if specified.
|
|
||||||
/// This doc is absent in GraphQL schema.
|
|
||||||
trait Character {
|
|
||||||
// Renames the field in GraphQL schema.
|
|
||||||
#[graphql(name = "myId")]
|
|
||||||
// Deprecates the field in GraphQL schema.
|
|
||||||
// Usual Rust `#[deprecated]` attribute is supported too as field deprecation,
|
|
||||||
// but `deprecated` attribute argument takes precedence over it, if specified.
|
|
||||||
#[graphql(deprecated = "Do not use it.")]
|
|
||||||
// Describes the field in GraphQL schema.
|
|
||||||
#[graphql(description = "ID of my own character.")]
|
|
||||||
// Usual Rust docs are supported too as field description,
|
|
||||||
// but `description` attribute argument takes precedence over them, if specified.
|
|
||||||
/// This description is absent in GraphQL schema.
|
|
||||||
fn id(
|
|
||||||
&self,
|
|
||||||
// Renames the argument in GraphQL schema.
|
|
||||||
#[graphql(name = "myNum")]
|
|
||||||
// Describes the argument in GraphQL schema.
|
|
||||||
#[graphql(description = "ID number of my own character.")]
|
|
||||||
// Specifies the default value for the argument.
|
|
||||||
// The concrete value may be omitted, and the `Default::default` one
|
|
||||||
// will be used in such case.
|
|
||||||
#[graphql(default = 5)]
|
|
||||||
num: i32,
|
|
||||||
) -> &str;
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#custom-context" id="custom-context">Custom context</a></h3>
|
|
||||||
<p>If a <a href="https://docs.rs/juniper/0.14.2/juniper/trait.Context.html"><code>Context</code></a> is required in a trait method to resolve a <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> field, specify it as an argument.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
humans: HashMap<String, Human>,
|
|
||||||
}
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
#[graphql_interface(for = Human)] // look, ma, context type is inferred! \(^o^)/
|
|
||||||
trait Character { // while still can be specified via `Context = ...` attribute argument
|
|
||||||
// If a field argument is named `context` or `ctx`, it's automatically assumed
|
|
||||||
// as a context argument.
|
|
||||||
fn id(&self, context: &Database) -> Option<&str>;
|
|
||||||
|
|
||||||
// Otherwise, you may mark it explicitly as a context argument.
|
|
||||||
fn name(&self, #[graphql(context)] db: &Database) -> Option<&str>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue, Context = Database)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self, db: &Database) -> Option<&str> {
|
|
||||||
if db.humans.contains_key(&self.id) {
|
|
||||||
Some(&self.id)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name(&self, db: &Database) -> Option<&str> {
|
|
||||||
if db.humans.contains_key(&self.id) {
|
|
||||||
Some(&self.name)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#using-executor-and-explicit-generic-scalar" id="using-executor-and-explicit-generic-scalar">Using executor and explicit generic scalar</a></h3>
|
|
||||||
<p>If an <a href="https://docs.rs/juniper/latest/juniper/struct.Executor.html"><code>Executor</code></a> is required in a trait method to resolve a <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> field, specify it as an argument.</p>
|
|
||||||
<p>This requires to explicitly parametrize over <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a>, as <a href="https://docs.rs/juniper/latest/juniper/struct.Executor.html"><code>Executor</code></a> does so. </p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_interface, Executor, GraphQLObject, LookAheadMethods as _, ScalarValue};
|
|
||||||
|
|
||||||
#[graphql_interface(for = Human, Scalar = S)] // notice specifying `ScalarValue` as existing type parameter
|
|
||||||
trait Character<S: ScalarValue> {
|
|
||||||
// If a field argument is named `executor`, it's automatically assumed
|
|
||||||
// as an executor argument.
|
|
||||||
async fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str
|
|
||||||
where
|
|
||||||
S: Send + Sync; // required by `#[async_trait]` transformation ¯\_(ツ)_/¯
|
|
||||||
|
|
||||||
// Otherwise, you may mark it explicitly as an executor argument.
|
|
||||||
async fn name<'b>(
|
|
||||||
&'b self,
|
|
||||||
#[graphql(executor)] another: &Executor<'_, '_, (), S>,
|
|
||||||
) -> &'b str
|
|
||||||
where
|
|
||||||
S: Send + Sync;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue<__S>)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface(scalar = S)]
|
|
||||||
impl<S: ScalarValue> Character<S> for Human {
|
|
||||||
async fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str
|
|
||||||
where
|
|
||||||
S: Send + Sync,
|
|
||||||
{
|
|
||||||
executor.look_ahead().field_name()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn name<'b>(&'b self, _: &Executor<'_, '_, (), S>) -> &'b str
|
|
||||||
where
|
|
||||||
S: Send + Sync,
|
|
||||||
{
|
|
||||||
&self.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#downcasting" id="downcasting">Downcasting</a></h3>
|
|
||||||
<p>By default, the <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> value is downcast to one of its implementer types via matching the enum variant or downcasting the trait object (if <code>dyn</code> macro argument is used).</p>
|
|
||||||
<p>However, if some custom logic is needed to downcast a <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> implementer, you may specify either an external function or a trait method to do so.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span>use juniper::{graphql_interface, GraphQLObject};
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
droids: HashMap<String, Droid>,
|
|
||||||
}
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
#[graphql_interface(for = [Human, Droid], context = Database)]
|
|
||||||
#[graphql_interface(on Droid = get_droid)] // enables downcasting `Droid` via `get_droid()` function
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
|
|
||||||
#[graphql(downcast)] // makes method a downcast to `Human`, not a field
|
|
||||||
// NOTICE: The method signature may optionally contain `&Database` context argument.
|
|
||||||
fn as_human(&self) -> Option<&Human> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue, Context = Database)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn as_human(&self) -> Option<&Self> {
|
|
||||||
Some(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue, Context = Database)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface]
|
|
||||||
impl Character for Droid {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// External downcast function doesn't have to be a method of a type.
|
|
||||||
// It's only a matter of the function signature to match the requirements.
|
|
||||||
fn get_droid<'db>(ch: &CharacterValue, db: &'db Database) -> Option<&'db Droid> {
|
|
||||||
db.droids.get(ch.id())
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>The attribute syntax <code>#[graphql_interface(on ImplementerType = resolver_fn)]</code> follows the <a href="https://spec.graphql.org/June2018/#example-5cc55">GraphQL syntax for downcasting interface implementer</a>.</p>
|
|
||||||
<h2><a class="header" href="#scalarvalue-considerations" id="scalarvalue-considerations"><code>ScalarValue</code> considerations</a></h2>
|
|
||||||
<p>By default, <code>#[graphql_interface]</code> macro generates code, which is generic over a <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type. This may introduce a problem when at least one of <a href="https://spec.graphql.org/June2018/#sec-Interfaces">GraphQL interface</a> implementers is restricted to a concrete <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type in its implementation. To resolve such problem, a concrete <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type should be specified.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_interface, DefaultScalarValue, GraphQLObject};
|
|
||||||
|
|
||||||
#[graphql_interface(for = [Human, Droid])]
|
|
||||||
#[graphql_interface(scalar = DefaultScalarValue)] // removing this line will fail compilation
|
|
||||||
trait Character {
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface(scalar = DefaultScalarValue)]
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
#[graphql_interface(scalar = DefaultScalarValue)]
|
|
||||||
impl Character for Droid {
|
|
||||||
fn id(&self) -> &str {
|
|
||||||
&self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/enums.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/input_objects.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/enums.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/input_objects.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,392 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Complex fields - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/objects/complex_fields.html" class="active"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#complex-fields" id="complex-fields">Complex fields</a></h1>
|
|
||||||
<p>If you've got a struct that can't be mapped directly to GraphQL, that contains
|
|
||||||
computed fields or circular structures, you have to use a more powerful tool:
|
|
||||||
the <code>#[graphql_object]</code> procedural macro. This macro lets you define GraphQL object
|
|
||||||
fields in a Rust <code>impl</code> block for a type. Note that only GraphQL fields
|
|
||||||
can be specified in this <code>impl</code> block. If you want to define normal methods on the struct,
|
|
||||||
you have to do so in a separate, normal <code>impl</code> block. Continuing with the
|
|
||||||
example from the last chapter, this is how you would define <code>Person</code> using the
|
|
||||||
macro:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(dead_code)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::graphql_object;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Person {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
self.name.as_str()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn age(&self) -> i32 {
|
|
||||||
self.age
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note that this syntax generates an implementation of the GraphQLType trait,
|
|
||||||
// the base impl of your struct can still be written like usual:
|
|
||||||
impl Person {
|
|
||||||
pub fn hidden_from_graphql(&self) {
|
|
||||||
// [...]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() { }
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>While this is a bit more verbose, it lets you write any kind of function in the
|
|
||||||
field resolver. With this syntax, fields can also take arguments:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::{graphql_object, GraphQLObject};
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct House {
|
|
||||||
inhabitants: Vec<Person>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl House {
|
|
||||||
// Creates the field inhabitantWithName(name), returning a nullable person
|
|
||||||
fn inhabitant_with_name(&self, name: String) -> Option<&Person> {
|
|
||||||
self.inhabitants.iter().find(|p| p.name == name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>To access global data such as database connections or authentication
|
|
||||||
information, a <em>context</em> is used. To learn more about this, see the next
|
|
||||||
chapter: <a href="using_contexts.html">Using contexts</a>.</p>
|
|
||||||
<h2><a class="header" href="#description-renaming-and-deprecation" id="description-renaming-and-deprecation">Description, renaming, and deprecation</a></h2>
|
|
||||||
<p>Like with the derive attribute, field names will be converted from <code>snake_case</code>
|
|
||||||
to <code>camelCase</code>. If you need to override the conversion, you can simply rename
|
|
||||||
the field. Also, the type name can be changed with an alias:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::graphql_object;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>struct Person;
|
|
||||||
|
|
||||||
/// Doc comments are used as descriptions for GraphQL.
|
|
||||||
#[graphql_object(
|
|
||||||
// With this attribute you can change the public GraphQL name of the type.
|
|
||||||
name = "PersonObject",
|
|
||||||
|
|
||||||
// You can also specify a description here, which will overwrite
|
|
||||||
// a doc comment description.
|
|
||||||
description = "...",
|
|
||||||
)]
|
|
||||||
impl Person {
|
|
||||||
/// A doc comment on the field will also be used for GraphQL.
|
|
||||||
#[graphql(
|
|
||||||
// Or provide a description here.
|
|
||||||
description = "...",
|
|
||||||
)]
|
|
||||||
fn doc_comment(&self) -> &str {
|
|
||||||
""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fields can also be renamed if required.
|
|
||||||
#[graphql(name = "myCustomFieldName")]
|
|
||||||
fn renamed_field() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecations also work as you'd expect.
|
|
||||||
// Both the standard Rust syntax and a custom attribute is accepted.
|
|
||||||
#[deprecated(note = "...")]
|
|
||||||
fn deprecated_standard() -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql(deprecated = "...")]
|
|
||||||
fn deprecated_graphql() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() { }
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#customizing-arguments" id="customizing-arguments">Customizing arguments</a></h2>
|
|
||||||
<p>Method field arguments can also be customized.</p>
|
|
||||||
<p>They can have custom descriptions and default values.</p>
|
|
||||||
<p><strong>Note</strong>: The syntax for this is currently a little awkward.
|
|
||||||
This will become better once the <a href="https://github.com/rust-lang/rust/issues/60406">Rust RFC 2565</a> is implemented.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::graphql_object;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>struct Person {}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Person {
|
|
||||||
#[graphql(
|
|
||||||
arguments(
|
|
||||||
arg1(
|
|
||||||
// Set a default value which will be injected if not present.
|
|
||||||
// The default can be any valid Rust expression, including a function call, etc.
|
|
||||||
default = true,
|
|
||||||
// Set a description.
|
|
||||||
description = "The first argument..."
|
|
||||||
),
|
|
||||||
arg2(
|
|
||||||
default = 0,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)]
|
|
||||||
fn field1(&self, arg1: bool, arg2: i32) -> String {
|
|
||||||
format!("{} {}", arg1, arg2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() { }
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#more-features" id="more-features">More features</a></h2>
|
|
||||||
<p>GraphQL fields expose more features than Rust's standard method syntax gives us:</p>
|
|
||||||
<ul>
|
|
||||||
<li>Per-field description and deprecation messages</li>
|
|
||||||
<li>Per-argument default values</li>
|
|
||||||
<li>Per-argument descriptions</li>
|
|
||||||
</ul>
|
|
||||||
<p>These, and more features, are described more thoroughly in <a href="https://docs.rs/juniper/latest/juniper/macro.object.html">the reference
|
|
||||||
documentation</a>.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/defining_objects.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/using_contexts.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/defining_objects.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/using_contexts.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,401 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Defining objects - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../../types/objects/defining_objects.html" class="active"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#defining-objects" id="defining-objects">Defining objects</a></h1>
|
|
||||||
<p>While any type in Rust can be exposed as a GraphQL object, the most common one
|
|
||||||
is a struct.</p>
|
|
||||||
<p>There are two ways to create a GraphQL object in Juniper. If you've got a simple
|
|
||||||
struct you want to expose, the easiest way is to use the custom derive
|
|
||||||
attribute. The other way is described in the <a href="complex_fields.html">Complex fields</a>
|
|
||||||
chapter.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>This will create a GraphQL object type called <code>Person</code>, with two fields: <code>name</code>
|
|
||||||
of type <code>String!</code>, and <code>age</code> of type <code>Int!</code>. Because of Rust's type system,
|
|
||||||
everything is exported as non-null by default. If you need a nullable field, you
|
|
||||||
can use <code>Option<T></code>.</p>
|
|
||||||
<p>We should take advantage of the
|
|
||||||
fact that GraphQL is self-documenting and add descriptions to the type and
|
|
||||||
fields. Juniper will automatically use associated doc comments as GraphQL
|
|
||||||
descriptions:</p>
|
|
||||||
<p>!FILENAME GraphQL descriptions via Rust doc comments</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
/// Information about a person
|
|
||||||
struct Person {
|
|
||||||
/// The person's full name, including both first and last names
|
|
||||||
name: String,
|
|
||||||
/// The person's age in years, rounded down
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Objects and fields without doc comments can instead set a <code>description</code>
|
|
||||||
via the <code>graphql</code> attribute. The following example is equivalent to the above:</p>
|
|
||||||
<p>!FILENAME GraphQL descriptions via attribute</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
#[graphql(description = "Information about a person")]
|
|
||||||
struct Person {
|
|
||||||
#[graphql(description = "The person's full name, including both first and last names")]
|
|
||||||
name: String,
|
|
||||||
#[graphql(description = "The person's age in years, rounded down")]
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Descriptions set via the <code>graphql</code> attribute take precedence over Rust
|
|
||||||
doc comments. This enables internal Rust documentation and external GraphQL
|
|
||||||
documentation to differ:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
#[graphql(description = "This description shows up in GraphQL")]
|
|
||||||
/// This description shows up in RustDoc
|
|
||||||
struct Person {
|
|
||||||
#[graphql(description = "This description shows up in GraphQL")]
|
|
||||||
/// This description shows up in RustDoc
|
|
||||||
name: String,
|
|
||||||
/// This description shows up in both RustDoc and GraphQL
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#relationships" id="relationships">Relationships</a></h2>
|
|
||||||
<p>You can only use the custom derive attribute under these circumstances:</p>
|
|
||||||
<ul>
|
|
||||||
<li>The annotated type is a <code>struct</code>,</li>
|
|
||||||
<li>Every struct field is either
|
|
||||||
<ul>
|
|
||||||
<li>A primitive type (<code>i32</code>, <code>f64</code>, <code>bool</code>, <code>String</code>, <code>juniper::ID</code>), or</li>
|
|
||||||
<li>A valid custom GraphQL type, e.g. another struct marked with this attribute,
|
|
||||||
or</li>
|
|
||||||
<li>A container/reference containing any of the above, e.g. <code>Vec<T></code>, <code>Box<T></code>,
|
|
||||||
<code>Option<T></code></li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
<p>Let's see what that means for building relationships between objects:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct House {
|
|
||||||
address: Option<String>, // Converted into String (nullable)
|
|
||||||
inhabitants: Vec<Person>, // Converted into [Person!]!
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Because <code>Person</code> is a valid GraphQL type, you can have a <code>Vec<Person></code> in a
|
|
||||||
struct and it'll be automatically converted into a list of non-nullable <code>Person</code>
|
|
||||||
objects.</p>
|
|
||||||
<h2><a class="header" href="#renaming-fields" id="renaming-fields">Renaming fields</a></h2>
|
|
||||||
<p>By default, struct fields are converted from Rust's standard <code>snake_case</code> naming
|
|
||||||
convention into GraphQL's <code>camelCase</code> convention:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
first_name: String, // Would be exposed as firstName in the GraphQL schema
|
|
||||||
last_name: String, // Exposed as lastName
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>You can override the name by using the <code>graphql</code> attribute on individual struct
|
|
||||||
fields:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
#[graphql(name = "websiteURL")]
|
|
||||||
website_url: Option<String>, // Now exposed as websiteURL in the schema
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#deprecating-fields" id="deprecating-fields">Deprecating fields</a></h2>
|
|
||||||
<p>To deprecate a field, you specify a deprecation reason using the <code>graphql</code>
|
|
||||||
attribute:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
#[graphql(deprecated = "Please use the name field instead")]
|
|
||||||
first_name: String,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>The <code>name</code>, <code>description</code>, and <code>deprecation</code> arguments can of course be
|
|
||||||
combined. Some restrictions from the GraphQL spec still applies though; you can
|
|
||||||
only deprecate object fields and enum values.</p>
|
|
||||||
<h2><a class="header" href="#skipping-fields" id="skipping-fields">Skipping fields</a></h2>
|
|
||||||
<p>By default all fields in a <code>GraphQLObject</code> are included in the generated GraphQL type. To prevent including a specific field, annotate the field with <code>#[graphql(skip)]</code>:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::GraphQLObject;
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: i32,
|
|
||||||
#[graphql(skip)]
|
|
||||||
<span class="boring"> #[allow(dead_code)]
|
|
||||||
</span> password_hash: String, // This cannot be queried or modified from GraphQL
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/complex_fields.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/complex_fields.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,642 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Error handling - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../../types/objects/error_handling.html" class="active"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#error-handling" id="error-handling">Error handling</a></h1>
|
|
||||||
<p>Error handling in GraphQL can be done in multiple ways. In the
|
|
||||||
following two different error handling models are discussed: field
|
|
||||||
results and GraphQL schema backed errors. Each approach has its
|
|
||||||
advantages. Choosing the right error handling method depends on the
|
|
||||||
requirements of the application--investigating both approaches is
|
|
||||||
beneficial.</p>
|
|
||||||
<h2><a class="header" href="#field-results" id="field-results">Field Results</a></h2>
|
|
||||||
<p>Rust
|
|
||||||
<a href="https://doc.rust-lang.org/book/second-edition/ch09-00-error-handling.html">provides</a>
|
|
||||||
two ways of dealing with errors: <code>Result<T, E></code> for recoverable errors and
|
|
||||||
<code>panic!</code> for unrecoverable errors. Juniper does not do anything about panicking;
|
|
||||||
it will bubble up to the surrounding framework and hopefully be dealt with
|
|
||||||
there.</p>
|
|
||||||
<p>For recoverable errors, Juniper works well with the built-in <code>Result</code> type, you
|
|
||||||
can use the <code>?</code> operator or the <code>try!</code> macro and things will generally just work
|
|
||||||
as you expect them to:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use std::{
|
|
||||||
str,
|
|
||||||
path::PathBuf,
|
|
||||||
fs::{File},
|
|
||||||
io::{Read},
|
|
||||||
};
|
|
||||||
use juniper::{graphql_object, FieldResult};
|
|
||||||
|
|
||||||
struct Example {
|
|
||||||
filename: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Example {
|
|
||||||
fn contents() -> FieldResult<String> {
|
|
||||||
let mut file = File::open(&self.filename)?;
|
|
||||||
let mut contents = String::new();
|
|
||||||
file.read_to_string(&mut contents)?;
|
|
||||||
Ok(contents)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn foo() -> FieldResult<Option<String>> {
|
|
||||||
// Some invalid bytes.
|
|
||||||
let invalid = vec![128, 223];
|
|
||||||
|
|
||||||
match str::from_utf8(&invalid) {
|
|
||||||
Ok(s) => Ok(Some(s.to_string())),
|
|
||||||
Err(e) => Err(e)?,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p><code>FieldResult<T></code> is an alias for <code>Result<T, FieldError></code>, which is the error
|
|
||||||
type all fields must return. By using the <code>?</code> operator or <code>try!</code> macro, any type
|
|
||||||
that implements the <code>Display</code> trait - which are most of the error types out
|
|
||||||
there - those errors are automatically converted into <code>FieldError</code>.</p>
|
|
||||||
<h2><a class="header" href="#error-payloads-null-and-partial-errors" id="error-payloads-null-and-partial-errors">Error payloads, <code>null</code>, and partial errors</a></h2>
|
|
||||||
<p>Juniper's error behavior conforms to the <a href="https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability">GraphQL specification</a>.</p>
|
|
||||||
<p>When a field returns an error, the field's result is replaced by <code>null</code>, an
|
|
||||||
additional <code>errors</code> object is created at the top level of the response, and the
|
|
||||||
execution is resumed. For example, with the previous example and the following
|
|
||||||
query:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
example {
|
|
||||||
contents
|
|
||||||
foo
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>If <code>str::from_utf8</code> resulted in a <code>std::str::Utf8Error</code>, the following would be
|
|
||||||
returned:</p>
|
|
||||||
<p>!FILENAME Response for nullable field with error</p>
|
|
||||||
<pre><code class="language-js">{
|
|
||||||
"data": {
|
|
||||||
"example": {
|
|
||||||
contents: "<Contents of the file>",
|
|
||||||
foo: null,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"errors": [
|
|
||||||
"message": "invalid utf-8 sequence of 2 bytes from index 0",
|
|
||||||
"locations": [{ "line": 2, "column": 4 }])
|
|
||||||
]
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>If an error is returned from a non-null field, such as the
|
|
||||||
example above, the <code>null</code> value is propagated up to the first nullable parent
|
|
||||||
field, or the root <code>data</code> object if there are no nullable fields.</p>
|
|
||||||
<p>For example, with the following query:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
example {
|
|
||||||
contents
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>If <code>File::open()</code> above resulted in <code>std::io::ErrorKind::PermissionDenied</code>, the
|
|
||||||
following would be returned:</p>
|
|
||||||
<p>!FILENAME Response for non-null field with error and no nullable parent</p>
|
|
||||||
<pre><code class="language-js">{
|
|
||||||
"errors": [
|
|
||||||
"message": "Permission denied (os error 13)",
|
|
||||||
"locations": [{ "line": 2, "column": 4 }])
|
|
||||||
]
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<h3><a class="header" href="#structured-errors" id="structured-errors">Structured errors</a></h3>
|
|
||||||
<p>Sometimes it is desirable to return additional structured error information
|
|
||||||
to clients. This can be accomplished by implementing <a href="https://docs.rs/juniper/latest/juniper/trait.IntoFieldError.html"><code>IntoFieldError</code></a>:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#[macro_use] extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::{graphql_object, FieldError, IntoFieldError, ScalarValue};
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>enum CustomError {
|
|
||||||
WhateverNotSet,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: ScalarValue> IntoFieldError<S> for CustomError {
|
|
||||||
fn into_field_error(self) -> FieldError<S> {
|
|
||||||
match self {
|
|
||||||
CustomError::WhateverNotSet => FieldError::new(
|
|
||||||
"Whatever does not exist",
|
|
||||||
graphql_value!({
|
|
||||||
"type": "NO_WHATEVER"
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Example {
|
|
||||||
whatever: Option<bool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Example {
|
|
||||||
fn whatever() -> Result<bool, CustomError> {
|
|
||||||
if let Some(value) = self.whatever {
|
|
||||||
return Ok(value);
|
|
||||||
}
|
|
||||||
Err(CustomError::WhateverNotSet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>The specified structured error information is included in the <a href="https://facebook.github.io/graphql/June2018/#sec-Errors"><code>extensions</code></a> key:</p>
|
|
||||||
<pre><code class="language-json">{
|
|
||||||
"errors": [{
|
|
||||||
"message": "Whatever does not exist",
|
|
||||||
"locations": [{"line": 2, "column": 4}],
|
|
||||||
"extensions": {
|
|
||||||
"type": "NO_WHATEVER"
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<h2><a class="header" href="#errors-backed-by-graphqls-schema" id="errors-backed-by-graphqls-schema">Errors Backed by GraphQL's Schema</a></h2>
|
|
||||||
<p>Rust's model of errors can be adapted for GraphQL. Rust's panic is
|
|
||||||
similar to a <code>FieldError</code>--the whole query is aborted and nothing can
|
|
||||||
be extracted (except for error related information).</p>
|
|
||||||
<p>Not all errors require this strict handling. Recoverable or partial errors can be put
|
|
||||||
into the GraphQL schema so the client can intelligently handle them.</p>
|
|
||||||
<p>To implement this approach, all errors must be partitioned into two error classes:</p>
|
|
||||||
<ul>
|
|
||||||
<li>Critical errors that cannot be fixed by the user (e.g. a database error).</li>
|
|
||||||
<li>Recoverable errors that can be fixed by the user (e.g. invalid input data).</li>
|
|
||||||
</ul>
|
|
||||||
<p>Critical errors are returned from resolvers as <code>FieldErrors</code> (from the previous section). Non-critical errors are part of the GraphQL schema and can be handled gracefully by clients. Similar to Rust, GraphQL allows similar error models with unions (see Unions).</p>
|
|
||||||
<h3><a class="header" href="#example-input-validation-simple" id="example-input-validation-simple">Example Input Validation (simple)</a></h3>
|
|
||||||
<p>In this example, basic input validation is implemented with GraphQL
|
|
||||||
types. Strings are used to identify the problematic field name. Errors
|
|
||||||
for a particular field are also returned as a string. In this example
|
|
||||||
the string contains a server-side localized error message. However, it is also
|
|
||||||
possible to return a unique string identifier and have the client present a localized string to the user.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
pub struct Item {
|
|
||||||
name: String,
|
|
||||||
quantity: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
pub struct ValidationError {
|
|
||||||
field: String,
|
|
||||||
message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
pub struct ValidationErrors {
|
|
||||||
errors: Vec<ValidationError>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
pub enum GraphQLResult {
|
|
||||||
Ok(Item),
|
|
||||||
Err(ValidationErrors),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Mutation;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Mutation {
|
|
||||||
fn addItem(&self, name: String, quantity: i32) -> GraphQLResult {
|
|
||||||
let mut errors = Vec::new();
|
|
||||||
|
|
||||||
if !(10 <= name.len() && name.len() <= 100) {
|
|
||||||
errors.push(ValidationError {
|
|
||||||
field: "name".to_string(),
|
|
||||||
message: "between 10 and 100".to_string()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if !(1 <= quantity && quantity <= 10) {
|
|
||||||
errors.push(ValidationError {
|
|
||||||
field: "quantity".to_string(),
|
|
||||||
message: "between 1 and 10".to_string()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if errors.is_empty() {
|
|
||||||
GraphQLResult::Ok(Item { name, quantity })
|
|
||||||
} else {
|
|
||||||
GraphQLResult::Err(ValidationErrors { errors })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>Each function may have a different return type and depending on the input
|
|
||||||
parameters a new result type is required. For example, adding a user
|
|
||||||
requires a new result type which contains the variant <code>Ok(User)</code>
|
|
||||||
instead of <code>Ok(Item)</code>.</p>
|
|
||||||
<p>The client can send a mutation request and handle the
|
|
||||||
resulting errors as shown in the following example:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
mutation {
|
|
||||||
addItem(name: "", quantity: 0) {
|
|
||||||
... on Item {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
... on ValidationErrors {
|
|
||||||
errors {
|
|
||||||
field
|
|
||||||
message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>A useful side effect of this approach is to have partially successful
|
|
||||||
queries or mutations. If one resolver fails, the results of the
|
|
||||||
successful resolvers are not discarded.</p>
|
|
||||||
<h3><a class="header" href="#example-input-validation-complex" id="example-input-validation-complex">Example Input Validation (complex)</a></h3>
|
|
||||||
<p>Instead of using strings to propagate errors, it is possible to use
|
|
||||||
GraphQL's type system to describe the errors more precisely.</p>
|
|
||||||
<p>For each fallible input variable a field in a GraphQL object is created. The
|
|
||||||
field is set if the validation for that particular field fails. You will likely want some kind of code generation to reduce repetition as the number of types required is significantly larger than
|
|
||||||
before. Each resolver function has a custom <code>ValidationResult</code> which
|
|
||||||
contains only fields provided by the function.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>#[derive(GraphQLObject)]
|
|
||||||
pub struct Item {
|
|
||||||
name: String,
|
|
||||||
quantity: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
pub struct ValidationError {
|
|
||||||
name: Option<String>,
|
|
||||||
quantity: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
pub enum GraphQLResult {
|
|
||||||
Ok(Item),
|
|
||||||
Err(ValidationError),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Mutation;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Mutation {
|
|
||||||
fn addItem(&self, name: String, quantity: i32) -> GraphQLResult {
|
|
||||||
let mut error = ValidationError {
|
|
||||||
name: None,
|
|
||||||
quantity: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if !(10 <= name.len() && name.len() <= 100) {
|
|
||||||
error.name = Some("between 10 and 100".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
if !(1 <= quantity && quantity <= 10) {
|
|
||||||
error.quantity = Some("between 1 and 10".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
if error.name.is_none() && error.quantity.is_none() {
|
|
||||||
GraphQLResult::Ok(Item { name, quantity })
|
|
||||||
} else {
|
|
||||||
GraphQLResult::Err(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
mutation {
|
|
||||||
addItem {
|
|
||||||
... on Item {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
... on ValidationErrorsItem {
|
|
||||||
name
|
|
||||||
quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>Expected errors are handled directly inside the query. Additionally, all
|
|
||||||
non-critical errors are known in advance by both the server and the client.</p>
|
|
||||||
<h3><a class="header" href="#example-input-validation-complex-with-critical-error" id="example-input-validation-complex-with-critical-error">Example Input Validation (complex with critical error)</a></h3>
|
|
||||||
<p>Our examples so far have only included non-critical errors. Providing
|
|
||||||
errors inside the GraphQL schema still allows you to return unexpected critical
|
|
||||||
errors when they occur.</p>
|
|
||||||
<p>In the following example, a theoretical database could fail
|
|
||||||
and would generate errors. Since it is not common for the database to
|
|
||||||
fail, the corresponding error is returned as a critical error:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>use juniper::{graphql_object, graphql_value, FieldError, GraphQLObject, GraphQLUnion, ScalarValue};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
pub struct Item {
|
|
||||||
name: String,
|
|
||||||
quantity: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
pub struct ValidationErrorItem {
|
|
||||||
name: Option<String>,
|
|
||||||
quantity: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
pub enum GraphQLResult {
|
|
||||||
Ok(Item),
|
|
||||||
Err(ValidationErrorItem),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum ApiError {
|
|
||||||
Database,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: ScalarValue> juniper::IntoFieldError<S> for ApiError {
|
|
||||||
fn into_field_error(self) -> FieldError<S> {
|
|
||||||
match self {
|
|
||||||
ApiError::Database => FieldError::new(
|
|
||||||
"Internal database error",
|
|
||||||
graphql_value!({
|
|
||||||
"type": "DATABASE"
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Mutation;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Mutation {
|
|
||||||
fn addItem(&self, name: String, quantity: i32) -> Result<GraphQLResult, ApiError> {
|
|
||||||
let mut error = ValidationErrorItem {
|
|
||||||
name: None,
|
|
||||||
quantity: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if !(10 <= name.len() && name.len() <= 100) {
|
|
||||||
error.name = Some("between 10 and 100".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
if !(1 <= quantity && quantity <= 10) {
|
|
||||||
error.quantity = Some("between 1 and 10".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
if error.name.is_none() && error.quantity.is_none() {
|
|
||||||
Ok(GraphQLResult::Ok(Item { name, quantity }))
|
|
||||||
} else {
|
|
||||||
Ok(GraphQLResult::Err(error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#additional-material" id="additional-material">Additional Material</a></h2>
|
|
||||||
<p>The <a href="https://shopify.dev/docs/admin-api/graphql/reference">Shopify API</a>
|
|
||||||
implements a similar approach. Their API is a good reference to
|
|
||||||
explore this approach in a real world application.</p>
|
|
||||||
<h1><a class="header" href="#comparison" id="comparison">Comparison</a></h1>
|
|
||||||
<p>The first approach discussed above--where every error is a critical error defined by <code>FieldResult</code> --is easier to implement. However, the client does not know what errors may occur and must instead infer what happened from the error string. This is brittle and could change over time due to either the client or server changing. Therefore, extensive integration testing between the client and server is required to maintain the implicit contract between the two.</p>
|
|
||||||
<p>Encoding non-critical errors in the GraphQL schema makes the contract between the client and the server explicit. This allows the client to understand and handle these errors correctly and the server to know when changes are potentially breaking clients. However, encoding this error information into the GraphQL schema requires additional code and up-front definition of non-critical errors.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/using_contexts.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/other-index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/using_contexts.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/other-index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,313 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Using contexts - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../../types/objects/using_contexts.html" class="active"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#using-contexts" id="using-contexts">Using contexts</a></h1>
|
|
||||||
<p>The context type is a feature in Juniper that lets field resolvers access global
|
|
||||||
data, most commonly database connections or authentication information. The
|
|
||||||
context is usually created from a <em>context factory</em>. How this is defined is
|
|
||||||
specific to the framework integration you're using, so check out the
|
|
||||||
documentation for either the <a href="../../servers/iron.html">Iron</a> or <a href="../../servers/rocket.html">Rocket</a>
|
|
||||||
integration.</p>
|
|
||||||
<p>In this chapter, we'll show you how to define a context type and use it in field
|
|
||||||
resolvers. Let's say that we have a simple user database in a <code>HashMap</code>:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(dead_code)]
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>struct Database {
|
|
||||||
users: HashMap<i32, User>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct User {
|
|
||||||
id: i32,
|
|
||||||
name: String,
|
|
||||||
friend_ids: Vec<i32>,
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() { }
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>We would like a <code>friends</code> field on <code>User</code> that returns a list of <code>User</code> objects.
|
|
||||||
In order to write such a field though, the database must be queried.</p>
|
|
||||||
<p>To solve this, we mark the <code>Database</code> as a valid context type and assign it to
|
|
||||||
the user object. </p>
|
|
||||||
<p>To gain access to the context, we need to specify an argument with the same
|
|
||||||
type as the specified <code>Context</code> for the type:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span><span class="boring">use juniper::graphql_object;
|
|
||||||
</span><span class="boring">
|
|
||||||
</span>// This struct represents our context.
|
|
||||||
struct Database {
|
|
||||||
users: HashMap<i32, User>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark the Database as a valid context type for Juniper
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
struct User {
|
|
||||||
id: i32,
|
|
||||||
name: String,
|
|
||||||
friend_ids: Vec<i32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign Database as the context type for User
|
|
||||||
#[graphql_object(context = Database)]
|
|
||||||
impl User {
|
|
||||||
// 3. Inject the context by specifying an argument
|
|
||||||
// with the context type.
|
|
||||||
// Note:
|
|
||||||
// - the type must be a reference
|
|
||||||
// - the name of the argument SHOULD be context
|
|
||||||
fn friends(&self, context: &Database) -> Vec<&User> {
|
|
||||||
|
|
||||||
// 5. Use the database to lookup users
|
|
||||||
self.friend_ids.iter()
|
|
||||||
.map(|id| context.users.get(id).expect("Could not find user with ID"))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
self.name.as_str()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn id(&self) -> i32 {
|
|
||||||
self.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() { }
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>You only get an immutable reference to the context, so if you want to affect
|
|
||||||
change to the execution, you'll need to use <a href="https://doc.rust-lang.org/book/first-edition/mutability.html#interior-vs-exterior-mutability">interior
|
|
||||||
mutability</a>
|
|
||||||
using e.g. <code>RwLock</code> or <code>RefCell</code>.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/complex_fields.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/error_handling.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../../types/objects/complex_fields.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../../types/objects/error_handling.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,244 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Other types - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html" class="active"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#other-types" id="other-types">Other Types</a></h1>
|
|
||||||
<p>The GraphQL type system provides several types in additon to objects.</p>
|
|
||||||
<p>Find out more about each type below:</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="enums.html">Enums</a></li>
|
|
||||||
<li><a href="interfaces.html">Interfaces</a></li>
|
|
||||||
<li><a href="input_objects.html">Input objects</a></li>
|
|
||||||
<li><a href="scalars.html">Scalars</a></li>
|
|
||||||
<li><a href="unions.html">Unions</a></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/objects/error_handling.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/enums.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/objects/error_handling.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/enums.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,354 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Scalars - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html" class="active"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#scalars" id="scalars">Scalars</a></h1>
|
|
||||||
<p>Scalars are the primitive types at the leaves of a GraphQL query: numbers,
|
|
||||||
strings, and booleans. You can create custom scalars to other primitive values,
|
|
||||||
but this often requires coordination with the client library intended to consume
|
|
||||||
the API you're building.</p>
|
|
||||||
<p>Since any value going over the wire is eventually transformed into JSON, you're
|
|
||||||
also limited in the data types you can use. </p>
|
|
||||||
<p>There are two ways to define custom scalars. </p>
|
|
||||||
<ul>
|
|
||||||
<li>For simple scalars that just wrap a primitive type, you can use the newtype pattern with
|
|
||||||
a custom derive. </li>
|
|
||||||
<li>For more advanced use cases with custom validation, you can use
|
|
||||||
the <code>graphql_scalar</code> proc macro.</li>
|
|
||||||
</ul>
|
|
||||||
<h2><a class="header" href="#built-in-scalars" id="built-in-scalars">Built-in scalars</a></h2>
|
|
||||||
<p>Juniper has built-in support for:</p>
|
|
||||||
<ul>
|
|
||||||
<li><code>i32</code> as <code>Int</code></li>
|
|
||||||
<li><code>f64</code> as <code>Float</code></li>
|
|
||||||
<li><code>String</code> and <code>&str</code> as <code>String</code></li>
|
|
||||||
<li><code>bool</code> as <code>Boolean</code></li>
|
|
||||||
<li><code>juniper::ID</code> as <code>ID</code>. This type is defined <a href="http://facebook.github.io/graphql/#sec-ID">in the
|
|
||||||
spec</a> as a type that is serialized
|
|
||||||
as a string but can be parsed from both a string and an integer.</li>
|
|
||||||
</ul>
|
|
||||||
<p>Note that there is no built-in support for <code>i64</code>/<code>u64</code>, as the GraphQL spec <a href="https://spec.graphql.org/June2018/#sec-Int">doesn't define any built-in scalars for <code>i64</code>/<code>u64</code> by default</a>. You may wish to leverage a <a href="#custom-scalars">custom GraphQL scalar</a> in your schema to support them.</p>
|
|
||||||
<p><strong>Third party types</strong>:</p>
|
|
||||||
<p>Juniper has built-in support for a few additional types from common third party
|
|
||||||
crates. They are enabled via features that are on by default.</p>
|
|
||||||
<ul>
|
|
||||||
<li>uuid::Uuid</li>
|
|
||||||
<li>chrono::DateTime</li>
|
|
||||||
<li>url::Url</li>
|
|
||||||
<li>bson::oid::ObjectId</li>
|
|
||||||
</ul>
|
|
||||||
<h2><a class="header" href="#newtype-pattern" id="newtype-pattern">newtype pattern</a></h2>
|
|
||||||
<p>Often, you might need a custom scalar that just wraps an existing type. </p>
|
|
||||||
<p>This can be done with the newtype pattern and a custom derive, similar to how
|
|
||||||
serde supports this pattern with <code>#[serde(transparent)]</code>.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>#[derive(juniper::GraphQLScalarValue)]
|
|
||||||
pub struct UserId(i32);
|
|
||||||
|
|
||||||
#[derive(juniper::GraphQLObject)]
|
|
||||||
struct User {
|
|
||||||
id: UserId,
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>That's it, you can now user <code>UserId</code> in your schema.</p>
|
|
||||||
<p>The macro also allows for more customization:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>/// You can use a doc comment to specify a description.
|
|
||||||
#[derive(juniper::GraphQLScalarValue)]
|
|
||||||
#[graphql(
|
|
||||||
transparent,
|
|
||||||
// Overwrite the GraphQL type name.
|
|
||||||
name = "MyUserId",
|
|
||||||
// Specify a custom description.
|
|
||||||
// A description in the attribute will overwrite a doc comment.
|
|
||||||
description = "My user id description",
|
|
||||||
)]
|
|
||||||
pub struct UserId(i32);
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#custom-scalars" id="custom-scalars">Custom scalars</a></h2>
|
|
||||||
<p>For more complex situations where you also need custom parsing or validation,
|
|
||||||
you can use the <code>graphql_scalar</code> proc macro.</p>
|
|
||||||
<p>Typically, you represent your custom scalars as strings.</p>
|
|
||||||
<p>The example below implements a custom scalar for a custom <code>Date</code> type.</p>
|
|
||||||
<p>Note: juniper already has built-in support for the <code>chrono::DateTime</code> type
|
|
||||||
via <code>chrono</code> feature, which is enabled by default and should be used for this
|
|
||||||
purpose.</p>
|
|
||||||
<p>The example below is used just for illustration.</p>
|
|
||||||
<p><strong>Note</strong>: the example assumes that the <code>Date</code> type implements
|
|
||||||
<code>std::fmt::Display</code> and <code>std::str::FromStr</code>.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">mod date {
|
|
||||||
</span><span class="boring"> pub struct Date;
|
|
||||||
</span><span class="boring"> impl std::str::FromStr for Date{
|
|
||||||
</span><span class="boring"> type Err = String; fn from_str(_value: &str) -> Result<Self, Self::Err> { unimplemented!() }
|
|
||||||
</span><span class="boring"> }
|
|
||||||
</span><span class="boring"> // And we define how to represent date as a string.
|
|
||||||
</span><span class="boring"> impl std::fmt::Display for Date {
|
|
||||||
</span><span class="boring"> fn fmt(&self, _f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
</span><span class="boring"> unimplemented!()
|
|
||||||
</span><span class="boring"> }
|
|
||||||
</span><span class="boring"> }
|
|
||||||
</span><span class="boring">}
|
|
||||||
</span>
|
|
||||||
use juniper::{Value, ParseScalarResult, ParseScalarValue};
|
|
||||||
use date::Date;
|
|
||||||
|
|
||||||
#[juniper::graphql_scalar(description = "Date")]
|
|
||||||
impl<S> GraphQLScalar for Date
|
|
||||||
where
|
|
||||||
S: ScalarValue
|
|
||||||
{
|
|
||||||
// Define how to convert your custom scalar into a primitive type.
|
|
||||||
fn resolve(&self) -> Value {
|
|
||||||
Value::scalar(self.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define how to parse a primitive type into your custom scalar.
|
|
||||||
fn from_input_value(v: &InputValue) -> Option<Date> {
|
|
||||||
v.as_scalar_value()
|
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.and_then(|s| s.parse().ok())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define how to parse a string value.
|
|
||||||
fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> {
|
|
||||||
<String as ParseScalarValue<S>>::from_str(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
<span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/input_objects.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/unions.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/input_objects.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/unions.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,652 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js light">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Unions - Juniper - GraphQL Server for Rust</title>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="../favicon.svg">
|
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
|
|
||||||
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var path_to_root = "../";
|
|
||||||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
html.classList.remove('no-js')
|
|
||||||
html.classList.remove('light')
|
|
||||||
html.classList.add(theme);
|
|
||||||
html.classList.add('js');
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<div class="sidebar-scrollbox">
|
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html" class="active"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar-hover-placeholder"></div>
|
|
||||||
<div id="menu-bar" class="menu-bar sticky bordered">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<h1><a class="header" href="#unions" id="unions">Unions</a></h1>
|
|
||||||
<p>From the server's point of view, <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL unions</a> are somewhat similar to <a href="https://spec.graphql.org/June2018/#sec-Interfaces">interfaces</a> - the main difference is that they don't contain fields on their own.</p>
|
|
||||||
<p>The most obvious and straightforward way to represent a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> in Rust is enum. However, we also can do so either with trait or a regular struct. That's why, for implementing <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL unions</a> Juniper provides:</p>
|
|
||||||
<ul>
|
|
||||||
<li><code>#[derive(GraphQLUnion)]</code> macro for enums and structs.</li>
|
|
||||||
<li><code>#[graphql_union]</code> for traits.</li>
|
|
||||||
</ul>
|
|
||||||
<h2><a class="header" href="#enums" id="enums">Enums</a></h2>
|
|
||||||
<p>Most of the time, we just need a trivial and straightforward Rust enum to represent a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a>.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">extern crate derive_more;
|
|
||||||
</span>use derive_more::From;
|
|
||||||
use juniper::{GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(From, GraphQLUnion)]
|
|
||||||
enum Character {
|
|
||||||
Human(Human),
|
|
||||||
Droid(Droid),
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#ignoring-enum-variants" id="ignoring-enum-variants">Ignoring enum variants</a></h3>
|
|
||||||
<p>In some rare situations we may want to omit exposing an enum variant in the GraphQL schema.</p>
|
|
||||||
<p>As an example, let's consider the situation where we need to bind some type parameter <code>T</code> for doing interesting type-level stuff in our resolvers. To achieve this we need to have <code>PhantomData<T></code>, but we don't want it exposed in the GraphQL schema.</p>
|
|
||||||
<blockquote>
|
|
||||||
<p><strong>WARNING</strong>:<br />
|
|
||||||
It's the <em>library user's responsibility</em> to ensure that ignored enum variant is <em>never</em> returned from resolvers, otherwise resolving the GraphQL query will <strong>panic at runtime</strong>.</p>
|
|
||||||
</blockquote>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">extern crate derive_more;
|
|
||||||
</span><span class="boring">use std::marker::PhantomData;
|
|
||||||
</span>use derive_more::From;
|
|
||||||
use juniper::{GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(From, GraphQLUnion)]
|
|
||||||
enum Character<S> {
|
|
||||||
Human(Human),
|
|
||||||
Droid(Droid),
|
|
||||||
#[from(ignore)]
|
|
||||||
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
|
|
||||||
_State(PhantomData<S>),
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#external-resolver-functions" id="external-resolver-functions">External resolver functions</a></h3>
|
|
||||||
<p>If some custom logic is needed to resolve a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variant, you may specify an external function to do so:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(dead_code)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CustomContext {
|
|
||||||
droid: Droid,
|
|
||||||
}
|
|
||||||
impl juniper::Context for CustomContext {}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
enum Character {
|
|
||||||
Human(Human),
|
|
||||||
#[graphql(with = Character::droid_from_context)]
|
|
||||||
Droid(Droid),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character {
|
|
||||||
// NOTICE: The function signature must contain `&self` and `&Context`,
|
|
||||||
// and return `Option<&VariantType>`.
|
|
||||||
fn droid_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Droid> {
|
|
||||||
Some(&ctx.droid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<p>With an external resolver function we can even declare a new <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variant where the Rust type is absent in the initial enum definition. The attribute syntax <code>#[graphql(on VariantType = resolver_fn)]</code> follows the <a href="https://spec.graphql.org/June2018/#example-f8163">GraphQL syntax for dispatching union variants</a>.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(dead_code)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
struct Ewok {
|
|
||||||
id: String,
|
|
||||||
is_funny: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CustomContext {
|
|
||||||
ewok: Ewok,
|
|
||||||
}
|
|
||||||
impl juniper::Context for CustomContext {}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
#[graphql(Context = CustomContext)]
|
|
||||||
#[graphql(on Ewok = Character::ewok_from_context)]
|
|
||||||
enum Character {
|
|
||||||
Human(Human),
|
|
||||||
Droid(Droid),
|
|
||||||
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
|
|
||||||
Ewok,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character {
|
|
||||||
fn ewok_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Ewok> {
|
|
||||||
if let Self::Ewok = self {
|
|
||||||
Some(&ctx.ewok)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#structs" id="structs">Structs</a></h2>
|
|
||||||
<p>Using Rust structs as <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL unions</a> is very similar to using enums, with the nuance that specifying an external resolver function is the only way to declare a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variant.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span>use juniper::{GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
humans: HashMap<String, Human>,
|
|
||||||
droids: HashMap<String, Droid>,
|
|
||||||
}
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
#[graphql(
|
|
||||||
Context = Database,
|
|
||||||
on Human = Character::get_human,
|
|
||||||
on Droid = Character::get_droid,
|
|
||||||
)]
|
|
||||||
struct Character {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character {
|
|
||||||
fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human>{
|
|
||||||
ctx.humans.get(&self.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid>{
|
|
||||||
ctx.droids.get(&self.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#traits" id="traits">Traits</a></h2>
|
|
||||||
<p>To use a Rust trait definition as a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> you need to use the <code>#[graphql_union]</code> macro. <a href="https://doc.rust-lang.org/stable/reference/procedural-macros.html#derive-macros">Rust doesn't allow derive macros on traits</a>, so using <code>#[derive(GraphQLUnion)]</code> on traits doesn't work.</p>
|
|
||||||
<blockquote>
|
|
||||||
<p><strong>NOTICE</strong>:<br />
|
|
||||||
A <strong>trait has to be <a href="https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety">object safe</a></strong>, because schema resolvers will need to return a <a href="https://doc.rust-lang.org/stable/reference/types/trait-object.html">trait object</a> to specify a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> behind it.</p>
|
|
||||||
</blockquote>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_union, GraphQLObject};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_union]
|
|
||||||
trait Character {
|
|
||||||
// NOTICE: The method signature must contain `&self` and return `Option<&VariantType>`.
|
|
||||||
fn as_human(&self) -> Option<&Human> { None }
|
|
||||||
fn as_droid(&self) -> Option<&Droid> { None }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Human {
|
|
||||||
fn as_human(&self) -> Option<&Human> { Some(&self) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Droid {
|
|
||||||
fn as_droid(&self) -> Option<&Droid> { Some(&self) }
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#custom-context" id="custom-context">Custom context</a></h3>
|
|
||||||
<p>If a <a href="https://docs.rs/juniper/0.14.2/juniper/trait.Context.html"><code>Context</code></a> is required in a trait method to resolve a <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variant, specify it as an argument.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span>use juniper::{graphql_union, GraphQLObject};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
humans: HashMap<String, Human>,
|
|
||||||
droids: HashMap<String, Droid>,
|
|
||||||
}
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
#[graphql_union(context = Database)]
|
|
||||||
trait Character {
|
|
||||||
// NOTICE: The method signature may optionally contain `&Context`.
|
|
||||||
fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> { None }
|
|
||||||
fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> { None }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Human {
|
|
||||||
fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> {
|
|
||||||
ctx.humans.get(&self.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Droid {
|
|
||||||
fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> {
|
|
||||||
ctx.droids.get(&self.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#ignoring-trait-methods" id="ignoring-trait-methods">Ignoring trait methods</a></h3>
|
|
||||||
<p>As with enums, we may want to omit some trait methods to be assumed as <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variants and ignore them.</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{graphql_union, GraphQLObject};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_union]
|
|
||||||
trait Character {
|
|
||||||
fn as_human(&self) -> Option<&Human> { None }
|
|
||||||
fn as_droid(&self) -> Option<&Droid> { None }
|
|
||||||
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Human {
|
|
||||||
fn as_human(&self) -> Option<&Human> { Some(&self) }
|
|
||||||
fn id(&self) -> &str { self.id.as_str() }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Droid {
|
|
||||||
fn as_droid(&self) -> Option<&Droid> { Some(&self) }
|
|
||||||
fn id(&self) -> &str { self.id.as_str() }
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h3><a class="header" href="#external-resolver-functions-1" id="external-resolver-functions-1">External resolver functions</a></h3>
|
|
||||||
<p>Similarly to enums and structs, it's not mandatory to use trait methods as <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variant resolvers. Instead, custom functions may be specified:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
|
||||||
</span><span class="boring">use std::collections::HashMap;
|
|
||||||
</span>use juniper::{graphql_union, GraphQLObject};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Context = Database)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Database {
|
|
||||||
humans: HashMap<String, Human>,
|
|
||||||
droids: HashMap<String, Droid>,
|
|
||||||
}
|
|
||||||
impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
#[graphql_union(context = Database)]
|
|
||||||
#[graphql_union(
|
|
||||||
on Human = DynCharacter::get_human,
|
|
||||||
on Droid = get_droid,
|
|
||||||
)]
|
|
||||||
trait Character {
|
|
||||||
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
|
|
||||||
fn id(&self) -> &str;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Human {
|
|
||||||
fn id(&self) -> &str { self.id.as_str() }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Character for Droid {
|
|
||||||
fn id(&self) -> &str { self.id.as_str() }
|
|
||||||
}
|
|
||||||
|
|
||||||
// The trait object is always `Send` and `Sync`.
|
|
||||||
type DynCharacter<'a> = dyn Character + Send + Sync + 'a;
|
|
||||||
|
|
||||||
impl<'a> DynCharacter<'a> {
|
|
||||||
fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> {
|
|
||||||
ctx.humans.get(self.id())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// External resolver function doesn't have to be a method of a type.
|
|
||||||
// It's only a matter of the function signature to match the requirements.
|
|
||||||
fn get_droid<'db>(ch: &DynCharacter<'_>, ctx: &'db Database) -> Option<&'db Droid> {
|
|
||||||
ctx.droids.get(ch.id())
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
<h2><a class="header" href="#scalarvalue-considerations" id="scalarvalue-considerations"><code>ScalarValue</code> considerations</a></h2>
|
|
||||||
<p>By default, <code>#[derive(GraphQLUnion)]</code> and <code>#[graphql_union]</code> macros generate code, which is generic over a <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type. This may introduce a problem when at least one of <a href="https://spec.graphql.org/June2018/#sec-Unions">GraphQL union</a> variants is restricted to a concrete <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type in its implementation. To resolve such problem, a concrete <a href="https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html"><code>ScalarValue</code></a> type should be specified:</p>
|
|
||||||
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(dead_code)]
|
|
||||||
</span><span class="boring">extern crate juniper;
|
|
||||||
</span>use juniper::{DefaultScalarValue, GraphQLObject, GraphQLUnion};
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
#[graphql(Scalar = DefaultScalarValue)]
|
|
||||||
struct Human {
|
|
||||||
id: String,
|
|
||||||
home_planet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Droid {
|
|
||||||
id: String,
|
|
||||||
primary_function: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(GraphQLUnion)]
|
|
||||||
#[graphql(Scalar = DefaultScalarValue)] // removing this line will fail compilation
|
|
||||||
enum Character {
|
|
||||||
Human(Human),
|
|
||||||
Droid(Droid),
|
|
||||||
}
|
|
||||||
<span class="boring">
|
|
||||||
</span><span class="boring">fn main() {}
|
|
||||||
</span></code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/scalars.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/scalars.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
window.playground_copyable = true;
|
|
||||||
</script>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
16
index.html
16
index.html
|
@ -1,16 +0,0 @@
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<p>
|
|
||||||
Documentation for the <a href="https://github.com/graphql-rust/juniper">Juniper</a> GraphQl library for Rust.
|
|
||||||
</p>
|
|
||||||
<ul>
|
|
||||||
<li>
|
|
||||||
<a href="./current/">Current Version</a>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a href="./master/">Master</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
|
@ -4,40 +4,28 @@
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title></title>
|
<title></title>
|
||||||
|
|
||||||
|
|
||||||
<base href="/">
|
<base href="/">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Custom HTML head -->
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
|
||||||
<link rel="icon" href="favicon.svg">
|
<link rel="icon" href="favicon.svg">
|
||||||
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="favicon.png">
|
<link rel="shortcut icon" href="favicon.png">
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/variables.css">
|
<link rel="stylesheet" href="css/variables.css">
|
||||||
<link rel="stylesheet" href="css/general.css">
|
<link rel="stylesheet" href="css/general.css">
|
||||||
<link rel="stylesheet" href="css/chrome.css">
|
<link rel="stylesheet" href="css/chrome.css">
|
||||||
|
|
||||||
<link rel="stylesheet" href="css/print.css" media="print">
|
<link rel="stylesheet" href="css/print.css" media="print">
|
||||||
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||||||
|
|
||||||
<link rel="stylesheet" href="fonts/fonts.css">
|
<link rel="stylesheet" href="fonts/fonts.css">
|
||||||
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="highlight.css">
|
<link rel="stylesheet" href="highlight.css">
|
||||||
<link rel="stylesheet" href="tomorrow-night.css">
|
<link rel="stylesheet" href="tomorrow-night.css">
|
||||||
|
@ -45,8 +33,6 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
|
@ -97,8 +83,7 @@
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<div class="sidebar-scrollbox">
|
<div class="sidebar-scrollbox">
|
||||||
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol>
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
</div>
|
|
||||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
@ -122,29 +107,24 @@
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
|
||||||
<div class="right-buttons">
|
<div class="right-buttons">
|
||||||
|
|
||||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -153,7 +133,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -165,7 +144,7 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<h1><a class="header" href="#document-not-found-404" id="document-not-found-404">Document not found (404)</a></h1>
|
<h1 id="document-not-found-404"><a class="header" href="#document-not-found-404">Document not found (404)</a></h1>
|
||||||
<p>This URL is invalid, sorry. Please use the navigation bar or search to continue.</p>
|
<p>This URL is invalid, sorry. Please use the navigation bar or search to continue.</p>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
@ -174,8 +153,6 @@
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
|
@ -183,8 +160,6 @@
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -192,23 +167,15 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.playground_copyable = true;
|
window.playground_copyable = true;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -216,7 +183,5 @@
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
4
master/FontAwesome/css/font-awesome.css
vendored
4
master/FontAwesome/css/font-awesome.css
vendored
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load diff
Before Width: | Height: | Size: 348 KiB After Width: | Height: | Size: 434 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
||||||
master
|
|
|
@ -1,365 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Dataloaders - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html" class="active"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#avoiding-the-n1-problem-with-dataloaders" id="avoiding-the-n1-problem-with-dataloaders"><h1>Avoiding the N+1 Problem With Dataloaders</h1></a>
|
|
||||||
<p>A common issue with graphql servers is how the resolvers query their datasource.
|
|
||||||
This issue results in a large number of unneccessary database queries or http requests.
|
|
||||||
Say you were wanting to list a bunch of cults people were in</p>
|
|
||||||
<pre><code class="language-graphql">query {
|
|
||||||
persons {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
cult {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>What would be executed by a SQL database would be:</p>
|
|
||||||
<pre><code class="language-sql">SELECT id, name, cult_id FROM persons;
|
|
||||||
SELECT id, name FROM cults WHERE id = 1;
|
|
||||||
SELECT id, name FROM cults WHERE id = 1;
|
|
||||||
SELECT id, name FROM cults WHERE id = 1;
|
|
||||||
SELECT id, name FROM cults WHERE id = 1;
|
|
||||||
SELECT id, name FROM cults WHERE id = 2;
|
|
||||||
SELECT id, name FROM cults WHERE id = 2;
|
|
||||||
SELECT id, name FROM cults WHERE id = 2;
|
|
||||||
# ...
|
|
||||||
</code></pre>
|
|
||||||
<p>Once the list of users has been returned, a separate query is run to find the cult of each user.
|
|
||||||
You can see how this could quickly become a problem.</p>
|
|
||||||
<p>A common solution to this is to introduce a <strong>dataloader</strong>.
|
|
||||||
This can be done with Juniper using the crate <a href="https://github.com/cksac/dataloader-rs">cksac/dataloader-rs</a>, which has two types of dataloaders; cached and non-cached.</p>
|
|
||||||
<a class="header" href="#cached-loader" id="cached-loader"><h4>Cached Loader</h4></a>
|
|
||||||
<p>DataLoader provides a memoization cache, after .load() is called once with a given key, the resulting value is cached to eliminate redundant loads.</p>
|
|
||||||
<p>DataLoader caching does not replace Redis, Memcache, or any other shared application-level cache. DataLoader is first and foremost a data loading mechanism, and its cache only serves the purpose of not repeatedly loading the same data in the context of a single request to your Application. <a href="https://github.com/graphql/dataloader#caching">(read more)</a></p>
|
|
||||||
<a class="header" href="#what-does-it-look-like" id="what-does-it-look-like"><h3>What does it look like?</h3></a>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
actix-identity = "0.4.0-beta.2"
|
|
||||||
actix-rt = "1.0"
|
|
||||||
actix-web = {version = "2.0", features = []}
|
|
||||||
juniper = { git = "https://github.com/graphql-rust/juniper" }
|
|
||||||
futures = "0.3"
|
|
||||||
postgres = "0.15.2"
|
|
||||||
dataloader = "0.12.0"
|
|
||||||
async-trait = "0.1.30"
|
|
||||||
</code></pre>
|
|
||||||
<pre><code class="language-rust ignore">// use dataloader::cached::Loader;
|
|
||||||
use dataloader::non_cached::Loader;
|
|
||||||
use dataloader::BatchFn;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use postgres::{Connection, TlsMode};
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
pub fn get_db_conn() -> Connection {
|
|
||||||
let pg_connection_string = env::var("DATABASE_URI").expect("need a db uri");
|
|
||||||
println!("Connecting to {}", pg_connection_string);
|
|
||||||
let conn = Connection::connect(&pg_connection_string[..], TlsMode::None).unwrap();
|
|
||||||
println!("Connection is fine");
|
|
||||||
conn
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Cult {
|
|
||||||
pub id: i32,
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_cult_by_ids(hashmap: &mut HashMap<i32, Cult>, ids: Vec<i32>) {
|
|
||||||
let conn = get_db_conn();
|
|
||||||
for row in &conn
|
|
||||||
.query("SELECT id, name FROM cults WHERE id = ANY($1)", &[&ids])
|
|
||||||
.unwrap()
|
|
||||||
{
|
|
||||||
let cult = Cult {
|
|
||||||
id: row.get(0),
|
|
||||||
name: row.get(1),
|
|
||||||
};
|
|
||||||
hashmap.insert(cult.id, cult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CultBatcher;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl BatchFn<i32, Cult> for CultBatcher {
|
|
||||||
|
|
||||||
// A hashmap is used, as we need to return an array which maps each original key to a Cult.
|
|
||||||
async fn load(&self, keys: &[i32]) -> HashMap<i32, Cult> {
|
|
||||||
println!("load cult batch {:?}", keys);
|
|
||||||
let mut cult_hashmap = HashMap::new();
|
|
||||||
get_cult_by_ids(&mut cult_hashmap, keys.to_vec());
|
|
||||||
cult_hashmap
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type CultLoader = Loader<i32, Cult, CultBatcher>;
|
|
||||||
|
|
||||||
// To create a new loader
|
|
||||||
pub fn get_loader() -> CultLoader {
|
|
||||||
Loader::new(CultBatcher)
|
|
||||||
// Usually a DataLoader will coalesce all individual loads which occur
|
|
||||||
// within a single frame of execution before calling your batch function with all requested keys.
|
|
||||||
// However sometimes this behavior is not desirable or optimal.
|
|
||||||
// Perhaps you expect requests to be spread out over a few subsequent ticks
|
|
||||||
// See: https://github.com/cksac/dataloader-rs/issues/12
|
|
||||||
// More info: https://github.com/graphql/dataloader#batch-scheduling
|
|
||||||
// A larger yield count will allow more requests to append to batch but will wait longer before actual load.
|
|
||||||
.with_yield_count(100)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[juniper::graphql_object(Context = Context)]
|
|
||||||
impl Cult {
|
|
||||||
// your resolvers
|
|
||||||
|
|
||||||
// To call the dataloader
|
|
||||||
pub async fn cult_by_id(ctx: &Context, id: i32) -> Cult {
|
|
||||||
ctx.cult_loader.load(id).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
</code></pre>
|
|
||||||
<a class="header" href="#how-do-i-call-them" id="how-do-i-call-them"><h3>How do I call them?</h3></a>
|
|
||||||
<p>Once created, a dataloader has the async functions <code>.load()</code> and <code>.load_many()</code>.
|
|
||||||
In the above example <code>cult_loader.load(id: i32).await</code> returns <code>Cult</code>. If we had used <code>cult_loader.load_many(Vec<i32>).await</code> it would have returned <code>Vec<Cult></code>.</p>
|
|
||||||
<a class="header" href="#where-do-i-create-my-dataloaders" id="where-do-i-create-my-dataloaders"><h3>Where do I create my dataloaders?</h3></a>
|
|
||||||
<p><strong>Dataloaders</strong> should be created per-request to avoid risk of bugs where one user is able to load cached/batched data from another user/ outside of its authenticated scope.
|
|
||||||
Creating dataloaders within individual resolvers will prevent batching from occurring and will nullify the benefits of the dataloader.</p>
|
|
||||||
<p>For example:</p>
|
|
||||||
<p><em>When you declare your context</em></p>
|
|
||||||
<pre><code class="language-rust ignore">use juniper;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Context {
|
|
||||||
pub cult_loader: CultLoader,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl juniper::Context for Context {}
|
|
||||||
|
|
||||||
impl Context {
|
|
||||||
pub fn new(cult_loader: CultLoader) -> Self {
|
|
||||||
Self {
|
|
||||||
cult_loader
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p><em>Your handler for GraphQL (Note: instantiating context here keeps it per-request)</em></p>
|
|
||||||
<pre><code class="language-rust ignore">pub async fn graphql(
|
|
||||||
st: web::Data<Arc<Schema>>,
|
|
||||||
data: web::Json<GraphQLRequest>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
|
|
||||||
// Context setup
|
|
||||||
let cult_loader = get_loader();
|
|
||||||
let ctx = Context::new(cult_loader);
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
let res = data.execute(&st, &ctx).await;
|
|
||||||
let json = serde_json::to_string(&res).map_err(error::ErrorInternalServerError)?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
|
||||||
.content_type("application/json")
|
|
||||||
.body(json))
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<a class="header" href="#further-example" id="further-example"><h3>Further Example:</h3></a>
|
|
||||||
<p>For a full example using Dataloaders and Context check out <a href="https://github.com/jayy-lmao/rust-graphql-docker">jayy-lmao/rust-graphql-docker</a>.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/multiple_ops_per_request.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/subscriptions.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/multiple_ops_per_request.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/subscriptions.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,289 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Implicit and explicit null - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html" class="active"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#implicit-and-explicit-null" id="implicit-and-explicit-null"><h1>Implicit and explicit null</h1></a>
|
|
||||||
<p>There are two ways that a client can submit a null argument or field in a query.</p>
|
|
||||||
<p>They can use a null literal:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
field(arg: null)
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>Or they can simply omit the argument:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
field
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>The former is an explicit null and the latter is an implicit null.</p>
|
|
||||||
<p>There are some situations where it's useful to know which one the user provided.</p>
|
|
||||||
<p>For example, let's say your business logic has a function that allows users to
|
|
||||||
perform a "patch" operation on themselves. Let's say your users can optionally
|
|
||||||
have favorite and least favorite numbers, and the input for that might look
|
|
||||||
like this:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust">/// Updates user attributes. Fields that are `None` are left as-is.
|
|
||||||
pub struct UserPatch {
|
|
||||||
/// If `Some`, updates the user's favorite number.
|
|
||||||
pub favorite_number: Option<Option<i32>>,
|
|
||||||
|
|
||||||
/// If `Some`, updates the user's least favorite number.
|
|
||||||
pub least_favorite_number: Option<Option<i32>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
# fn main() {}
|
|
||||||
</code></pre></pre>
|
|
||||||
<p>To set a user's favorite number to 7, you would set <code>favorite_number</code> to
|
|
||||||
<code>Some(Some(7))</code>. In GraphQL, that might look like this:</p>
|
|
||||||
<pre><code class="language-graphql">mutation { patchUser(patch: { favoriteNumber: 7 }) }
|
|
||||||
</code></pre>
|
|
||||||
<p>To unset the user's favorite number, you would set <code>favorite_number</code> to
|
|
||||||
<code>Some(None)</code>. In GraphQL, that might look like this:</p>
|
|
||||||
<pre><code class="language-graphql">mutation { patchUser(patch: { favoriteNumber: null }) }
|
|
||||||
</code></pre>
|
|
||||||
<p>If you want to leave the user's favorite number alone, you would set it to
|
|
||||||
<code>None</code>. In GraphQL, that might look like this:</p>
|
|
||||||
<pre><code class="language-graphql">mutation { patchUser(patch: {}) }
|
|
||||||
</code></pre>
|
|
||||||
<p>The last two cases rely on being able to distinguish between explicit and implicit null.</p>
|
|
||||||
<p>In Juniper, this can be done using the <code>Nullable</code> type:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
|
||||||
use juniper::{FieldResult, Nullable};
|
|
||||||
|
|
||||||
#[derive(juniper::GraphQLInputObject)]
|
|
||||||
struct UserPatchInput {
|
|
||||||
pub favorite_number: Nullable<i32>,
|
|
||||||
pub least_favorite_number: Nullable<i32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<UserPatch> for UserPatchInput {
|
|
||||||
fn into(self) -> UserPatch {
|
|
||||||
UserPatch {
|
|
||||||
// The `explicit` function transforms the `Nullable` into an
|
|
||||||
// `Option<Option<T>>` as expected by the business logic layer.
|
|
||||||
favorite_number: self.favorite_number.explicit(),
|
|
||||||
least_favorite_number: self.least_favorite_number.explicit(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# pub struct UserPatch {
|
|
||||||
# pub favorite_number: Option<Option<i32>>,
|
|
||||||
# pub least_favorite_number: Option<Option<i32>>,
|
|
||||||
# }
|
|
||||||
|
|
||||||
# struct Session;
|
|
||||||
# impl Session {
|
|
||||||
# fn patch_user(&self, _patch: UserPatch) -> FieldResult<()> { Ok(()) }
|
|
||||||
# }
|
|
||||||
|
|
||||||
struct Context {
|
|
||||||
session: Session,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Mutation;
|
|
||||||
|
|
||||||
#[juniper::graphql_object(Context=Context)]
|
|
||||||
impl Mutation {
|
|
||||||
fn patch_user(ctx: &Context, patch: UserPatchInput) -> FieldResult<bool> {
|
|
||||||
ctx.session.patch_user(patch.into())?;
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# fn main() {}
|
|
||||||
</code></pre></pre>
|
|
||||||
<p>This type functions much like <code>Option</code>, but has two empty variants so you can
|
|
||||||
distinguish between implicit and explicit null.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/non_struct_objects.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/objects_and_generics.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/non_struct_objects.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/objects_and_generics.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,210 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Advanced Topics - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html" class="active"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#advanced-topics" id="advanced-topics"><h1>Advanced Topics</h1></a>
|
|
||||||
<p>The chapters below cover some more advanced scenarios.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="introspection.html">Introspection</a></li>
|
|
||||||
<li><a href="non_struct_objects.html">Non-struct objects</a></li>
|
|
||||||
<li><a href="implicit_and_explicit_null.html">Implicit and explicit null</a></li>
|
|
||||||
<li><a href="objects_and_generics.html">Objects and generics</a></li>
|
|
||||||
<li><a href="multiple_ops_per_request.html">Multiple operations per request</a></li>
|
|
||||||
<li><a href="dataloaders.html">Dataloaders</a></li>
|
|
||||||
<li><a href="subscriptions.html">Subscriptions</a></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/third-party.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/introspection.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/third-party.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/introspection.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,272 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Introspection - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html" class="active"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#introspection" id="introspection"><h1>Introspection</h1></a>
|
|
||||||
<p>GraphQL defines a special built-in top-level field called <code>__schema</code>. Querying
|
|
||||||
for this field allows one to <a href="https://graphql.org/learn/introspection/">introspect the schema</a>
|
|
||||||
at runtime to see what queries and mutations the GraphQL server supports.</p>
|
|
||||||
<p>Because introspection queries are just regular GraphQL queries, Juniper supports
|
|
||||||
them natively. For example, to get all the names of the types supported one
|
|
||||||
could execute the following query against Juniper:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
__schema {
|
|
||||||
types {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<a class="header" href="#schema-introspection-output-as-json" id="schema-introspection-output-as-json"><h2>Schema introspection output as JSON</h2></a>
|
|
||||||
<p>Many client libraries and tools in the GraphQL ecosystem require a complete
|
|
||||||
representation of the server schema. Often this representation is in JSON and
|
|
||||||
referred to as <code>schema.json</code>. A complete representation of the schema can be
|
|
||||||
produced by issuing a specially crafted introspection query.</p>
|
|
||||||
<p>Juniper provides a convenience function to introspect the entire schema. The
|
|
||||||
result can then be converted to JSON for use with tools and libraries such as
|
|
||||||
<a href="https://github.com/graphql-rust/graphql-client">graphql-client</a>:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
|
||||||
# extern crate juniper;
|
|
||||||
# extern crate serde_json;
|
|
||||||
use juniper::{
|
|
||||||
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
|
|
||||||
GraphQLObject, IntrospectionFormat,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Define our schema.
|
|
||||||
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct Example {
|
|
||||||
id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Context;
|
|
||||||
impl juniper::Context for Context {}
|
|
||||||
|
|
||||||
struct Query;
|
|
||||||
|
|
||||||
#[graphql_object(context = Context)]
|
|
||||||
impl Query {
|
|
||||||
fn example(id: String) -> FieldResult<Example> {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Schema = juniper::RootNode<
|
|
||||||
'static,
|
|
||||||
Query,
|
|
||||||
EmptyMutation<Context>,
|
|
||||||
EmptySubscription<Context>
|
|
||||||
>;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// Create a context object.
|
|
||||||
let ctx = Context{};
|
|
||||||
|
|
||||||
// Run the built-in introspection query.
|
|
||||||
let (res, _errors) = juniper::introspect(
|
|
||||||
&Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
|
|
||||||
&ctx,
|
|
||||||
IntrospectionFormat::default(),
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
// Convert introspection result to json.
|
|
||||||
let json_result = serde_json::to_string_pretty(&res);
|
|
||||||
assert!(json_result.is_ok());
|
|
||||||
}
|
|
||||||
</code></pre></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/non_struct_objects.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/non_struct_objects.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,253 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Multiple operations per request - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html" class="active"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#multiple-operations-per-request" id="multiple-operations-per-request"><h1>Multiple operations per request</h1></a>
|
|
||||||
<p>The GraphQL standard generally assumes there will be one server request for each client operation you want to perform (such as a query or mutation). This is conceptually simple but has the potential to be inefficent.</p>
|
|
||||||
<p>Some client libraries such as <a href="https://www.apollographql.com/docs/link/links/batch-http.html">apollo-link-batch-http</a> have added the ability to batch operations in a single HTTP request to save network round-trips and potentially increase performance. There are some <a href="https://blog.apollographql.com/batching-client-graphql-queries-a685f5bcd41b">tradeoffs</a> that should be considered before batching requests.</p>
|
|
||||||
<p>Juniper's server integration crates support multiple operations in a single HTTP request using JSON arrays. This makes them compatible with client libraries that support batch operations without any special configuration.</p>
|
|
||||||
<p>Server integration crates maintained by others are <strong>not required</strong> to support batch requests. Batch requests aren't part of the official GraphQL specification.</p>
|
|
||||||
<p>Assuming an integration supports batch requests, for the following GraphQL query:</p>
|
|
||||||
<pre><code class="language-graphql">{
|
|
||||||
hero {
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>The json data to POST to the server for an individual request would be:</p>
|
|
||||||
<pre><code class="language-json">{
|
|
||||||
"query": "{hero{name}}"
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>And the response would be of the form:</p>
|
|
||||||
<pre><code class="language-json">{
|
|
||||||
"data": {
|
|
||||||
"hero": {
|
|
||||||
"name": "R2-D2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<p>If you wanted to run the same query twice in a single HTTP request, the batched json data to POST to the server would be:</p>
|
|
||||||
<pre><code class="language-json">[
|
|
||||||
{
|
|
||||||
"query": "{hero{name}}"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"query": "{hero{name}}"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
</code></pre>
|
|
||||||
<p>And the response would be of the form:</p>
|
|
||||||
<pre><code class="language-json">[
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"hero": {
|
|
||||||
"name": "R2-D2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"hero": {
|
|
||||||
"name": "R2-D2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
</code></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/objects_and_generics.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/dataloaders.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/objects_and_generics.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/dataloaders.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,251 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Non-struct objects - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html" class="active"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#non-struct-objects" id="non-struct-objects"><h1>Non-struct objects</h1></a>
|
|
||||||
<p>Up until now, we've only looked at mapping structs to GraphQL objects. However,
|
|
||||||
any Rust type can be mapped into a GraphQL object. In this chapter, we'll look
|
|
||||||
at enums, but traits will work too - they don't <em>have</em> to be mapped into GraphQL
|
|
||||||
interfaces.</p>
|
|
||||||
<p>Using <code>Result</code>-like enums can be a useful way of reporting e.g. validation
|
|
||||||
errors from a mutation:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
|
||||||
# use juniper::{graphql_object, GraphQLObject};
|
|
||||||
# #[derive(juniper::GraphQLObject)] struct User { name: String }
|
|
||||||
#
|
|
||||||
#[derive(GraphQLObject)]
|
|
||||||
struct ValidationError {
|
|
||||||
field: String,
|
|
||||||
message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
# #[allow(dead_code)]
|
|
||||||
enum SignUpResult {
|
|
||||||
Ok(User),
|
|
||||||
Error(Vec<ValidationError>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl SignUpResult {
|
|
||||||
fn user(&self) -> Option<&User> {
|
|
||||||
match *self {
|
|
||||||
SignUpResult::Ok(ref user) => Some(user),
|
|
||||||
SignUpResult::Error(_) => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error(&self) -> Option<&Vec<ValidationError>> {
|
|
||||||
match *self {
|
|
||||||
SignUpResult::Ok(_) => None,
|
|
||||||
SignUpResult::Error(ref errors) => Some(errors)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#
|
|
||||||
# fn main() {}
|
|
||||||
</code></pre></pre>
|
|
||||||
<p>Here, we use an enum to decide whether a user's input data was valid or not, and
|
|
||||||
it could be used as the result of e.g. a sign up mutation.</p>
|
|
||||||
<p>While this is an example of how you could use something other than a struct to
|
|
||||||
represent a GraphQL object, it's also an example on how you could implement
|
|
||||||
error handling for "expected" errors - errors like validation errors. There are
|
|
||||||
no hard rules on how to represent errors in GraphQL, but there are
|
|
||||||
<a href="https://github.com/facebook/graphql/issues/117#issuecomment-170180628">some</a>
|
|
||||||
<a href="https://github.com/graphql/graphql-js/issues/560#issuecomment-259508214">comments</a>
|
|
||||||
from one of the authors of GraphQL on how they intended "hard" field errors to
|
|
||||||
be used, and how to model expected errors.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/introspection.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/implicit_and_explicit_null.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/introspection.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/implicit_and_explicit_null.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,258 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Objects and generics - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html" class="active"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#objects-and-generics" id="objects-and-generics"><h1>Objects and generics</h1></a>
|
|
||||||
<p>Yet another point where GraphQL and Rust differs is in how generics work. In
|
|
||||||
Rust, almost any type could be generic - that is, take type parameters. In
|
|
||||||
GraphQL, there are only two generic types: lists and non-nullables.</p>
|
|
||||||
<p>This poses a restriction on what you can expose in GraphQL from Rust: no generic
|
|
||||||
structs can be exposed - all type parameters must be bound. For example, you can
|
|
||||||
not make e.g. <code>Result<T, E></code> into a GraphQL type, but you <em>can</em> make e.g.
|
|
||||||
<code>Result<User, String></code> into a GraphQL type.</p>
|
|
||||||
<p>Let's make a slightly more compact but generic implementation of <a href="non_struct_objects.html">the last
|
|
||||||
chapter</a>:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
|
||||||
# #[derive(juniper::GraphQLObject)] struct User { name: String }
|
|
||||||
# #[derive(juniper::GraphQLObject)] struct ForumPost { title: String }
|
|
||||||
|
|
||||||
#[derive(juniper::GraphQLObject)]
|
|
||||||
struct ValidationError {
|
|
||||||
field: String,
|
|
||||||
message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
# #[allow(dead_code)]
|
|
||||||
struct MutationResult<T>(Result<T, Vec<ValidationError>>);
|
|
||||||
|
|
||||||
#[juniper::graphql_object(
|
|
||||||
name = "UserResult",
|
|
||||||
)]
|
|
||||||
impl MutationResult<User> {
|
|
||||||
fn user(&self) -> Option<&User> {
|
|
||||||
self.0.as_ref().ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error(&self) -> Option<&Vec<ValidationError>> {
|
|
||||||
self.0.as_ref().err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[juniper::graphql_object(
|
|
||||||
name = "ForumPostResult",
|
|
||||||
)]
|
|
||||||
impl MutationResult<ForumPost> {
|
|
||||||
fn forum_post(&self) -> Option<&ForumPost> {
|
|
||||||
self.0.as_ref().ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error(&self) -> Option<&Vec<ValidationError>> {
|
|
||||||
self.0.as_ref().err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# fn main() {}
|
|
||||||
</code></pre></pre>
|
|
||||||
<p>Here, we've made a wrapper around <code>Result</code> and exposed some concrete
|
|
||||||
instantiations of <code>Result<T, E></code> as distinct GraphQL objects. The reason we
|
|
||||||
needed the wrapper is of Rust's rules for when you can derive a trait - in this
|
|
||||||
case, both <code>Result</code> and Juniper's internal GraphQL trait are from third-party
|
|
||||||
sources.</p>
|
|
||||||
<p>Because we're using generics, we also need to specify a name for our
|
|
||||||
instantiated types. Even if Juniper <em>could</em> figure out the name,
|
|
||||||
<code>MutationResult<User></code> wouldn't be a valid GraphQL type name.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/implicit_and_explicit_null.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/multiple_ops_per_request.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/implicit_and_explicit_null.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/multiple_ops_per_request.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,345 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Subscriptions - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html" class="active"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#subscriptions" id="subscriptions"><h1>Subscriptions</h1></a>
|
|
||||||
<a class="header" href="#how-to-achieve-realtime-data-with-graphql-subscriptions" id="how-to-achieve-realtime-data-with-graphql-subscriptions"><h3>How to achieve realtime data with GraphQL subscriptions</h3></a>
|
|
||||||
<p>GraphQL subscriptions are a way to push data from the server to clients requesting real-time messages
|
|
||||||
from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client,
|
|
||||||
but instead of immediately returning a single answer a result is sent every time a particular event happens on the
|
|
||||||
server.</p>
|
|
||||||
<p>In order to execute subscriptions you need a coordinator (that spawns connections)
|
|
||||||
and a GraphQL object that can be resolved into a stream--elements of which will then
|
|
||||||
be returned to the end user. The <a href="https://github.com/graphql-rust/juniper/tree/master/juniper_subscriptions"><code>juniper_subscriptions</code></a> crate
|
|
||||||
provides a default connection implementation. Currently subscriptions are only supported on the <code>master</code> branch. Add the following to your <code>Cargo.toml</code>:</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
|
|
||||||
juniper_subscriptions = { git = "https://github.com/graphql-rust/juniper", branch = "master" }
|
|
||||||
</code></pre>
|
|
||||||
<a class="header" href="#schema-definition" id="schema-definition"><h3>Schema Definition</h3></a>
|
|
||||||
<p>The <code>Subscription</code> is just a GraphQL object, similar to the query root and mutations object that you defined for the
|
|
||||||
operations in your [Schema][Schema]. For subscriptions all fields/operations should be async and should return a <a href="https://docs.rs/futures/0.3.4/futures/stream/trait.Stream.html">Stream</a>.</p>
|
|
||||||
<p>This example shows a subscription operation that returns two events, the strings <code>Hello</code> and <code>World!</code>
|
|
||||||
sequentially:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate futures;
|
|
||||||
# extern crate juniper;
|
|
||||||
# extern crate juniper_subscriptions;
|
|
||||||
# extern crate tokio;
|
|
||||||
# use juniper::{graphql_object, graphql_subscription, FieldError};
|
|
||||||
# use futures::Stream;
|
|
||||||
# use std::pin::Pin;
|
|
||||||
#
|
|
||||||
# #[derive(Clone)]
|
|
||||||
# pub struct Database;
|
|
||||||
# impl juniper::Context for Database {}
|
|
||||||
|
|
||||||
# pub struct Query;
|
|
||||||
# #[graphql_object(context = Database)]
|
|
||||||
# impl Query {
|
|
||||||
# fn hello_world() -> &str {
|
|
||||||
# "Hello World!"
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
pub struct Subscription;
|
|
||||||
|
|
||||||
type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
|
|
||||||
|
|
||||||
#[graphql_subscription(context = Database)]
|
|
||||||
impl Subscription {
|
|
||||||
async fn hello_world() -> StringStream {
|
|
||||||
let stream = futures::stream::iter(vec![
|
|
||||||
Ok(String::from("Hello")),
|
|
||||||
Ok(String::from("World!"))
|
|
||||||
]);
|
|
||||||
Box::pin(stream)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#
|
|
||||||
# fn main () {}
|
|
||||||
</code></pre></pre>
|
|
||||||
<a class="header" href="#coordinator" id="coordinator"><h3>Coordinator</h3></a>
|
|
||||||
<p>Subscriptions require a bit more resources than regular queries and provide a great vector for DOS attacks. This can can bring down a server easily if not handled correctly. The [<code>SubscriptionCoordinator</code>][SubscriptionCoordinator] trait provides coordination logic to enable functionality like DOS attack mitigation and resource limits.</p>
|
|
||||||
<p>The [<code>SubscriptionCoordinator</code>][SubscriptionCoordinator] contains the schema and can keep track of opened connections, handle subscription
|
|
||||||
start and end, and maintain a global subscription id for each subscription. Each time a connection is established,<br />
|
|
||||||
the [<code>SubscriptionCoordinator</code>][SubscriptionCoordinator] spawns a [<code>SubscriptionConnection</code>][SubscriptionConnection]. The [<code>SubscriptionConnection</code>][SubscriptionConnection] handles a single connection, providing resolver logic for a client stream as well as reconnection
|
|
||||||
and shutdown logic.</p>
|
|
||||||
<p>While you can implement [<code>SubscriptionCoordinator</code>][SubscriptionCoordinator] yourself, Juniper contains a simple and generic implementation called [<code>Coordinator</code>][Coordinator]. The <code>subscribe</code>
|
|
||||||
operation returns a [<code>Future</code>][Future] with an <code>Item</code> value of a <code>Result<Connection, GraphQLError></code>,
|
|
||||||
where [<code>Connection</code>][Connection] is a <code>Stream</code> of values returned by the operation and [<code>GraphQLError</code>][GraphQLError] is the error when the subscription fails.</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(dead_code)]
|
|
||||||
# extern crate futures;
|
|
||||||
# extern crate juniper;
|
|
||||||
# extern crate juniper_subscriptions;
|
|
||||||
# extern crate serde_json;
|
|
||||||
# extern crate tokio;
|
|
||||||
# use juniper::{
|
|
||||||
# http::GraphQLRequest,
|
|
||||||
# graphql_object, graphql_subscription,
|
|
||||||
# DefaultScalarValue, EmptyMutation, FieldError,
|
|
||||||
# RootNode, SubscriptionCoordinator,
|
|
||||||
# };
|
|
||||||
# use juniper_subscriptions::Coordinator;
|
|
||||||
# use futures::{Stream, StreamExt};
|
|
||||||
# use std::pin::Pin;
|
|
||||||
#
|
|
||||||
# #[derive(Clone)]
|
|
||||||
# pub struct Database;
|
|
||||||
#
|
|
||||||
# impl juniper::Context for Database {}
|
|
||||||
#
|
|
||||||
# impl Database {
|
|
||||||
# fn new() -> Self {
|
|
||||||
# Self {}
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# pub struct Query;
|
|
||||||
#
|
|
||||||
# #[graphql_object(context = Database)]
|
|
||||||
# impl Query {
|
|
||||||
# fn hello_world() -> &str {
|
|
||||||
# "Hello World!"
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# pub struct Subscription;
|
|
||||||
#
|
|
||||||
# type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
|
|
||||||
#
|
|
||||||
# #[graphql_subscription(context = Database)]
|
|
||||||
# impl Subscription {
|
|
||||||
# async fn hello_world() -> StringStream {
|
|
||||||
# let stream =
|
|
||||||
# futures::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
|
|
||||||
# Box::pin(stream)
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
|
|
||||||
|
|
||||||
fn schema() -> Schema {
|
|
||||||
Schema::new(Query {}, EmptyMutation::new(), Subscription {})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run_subscription() {
|
|
||||||
let schema = schema();
|
|
||||||
let coordinator = Coordinator::new(schema);
|
|
||||||
let req: GraphQLRequest<DefaultScalarValue> = serde_json::from_str(
|
|
||||||
r#"{
|
|
||||||
"query": "subscription { helloWorld }"
|
|
||||||
}"#,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let ctx = Database::new();
|
|
||||||
let mut conn = coordinator.subscribe(&req, &ctx).await.unwrap();
|
|
||||||
while let Some(result) = conn.next().await {
|
|
||||||
println!("{}", serde_json::to_string(&result).unwrap());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#
|
|
||||||
# fn main() { }
|
|
||||||
</code></pre></pre>
|
|
||||||
<a class="header" href="#web-integration-and-examples" id="web-integration-and-examples"><h3>Web Integration and Examples</h3></a>
|
|
||||||
<p>Currently there is an example of subscriptions with [warp][warp], but it still in an alpha state.
|
|
||||||
GraphQL over [WS][WS] is not fully supported yet and is non-standard.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://github.com/graphql-rust/juniper/tree/master/examples/warp_subscriptions">Warp Subscription Example</a></li>
|
|
||||||
<li><a href="https://github.com/graphql-rust/juniper/tree/master/examples/basic_subscriptions">Small Example</a></li>
|
|
||||||
</ul>
|
|
||||||
<!-- TODO: Fix these links when the documentation for the `juniper_subscriptions` are defined in the docs. --->
|
|
||||||
[Coordinator]: https://docs.rs/juniper_subscriptions/0.15.0/struct.Coordinator.html
|
|
||||||
[SubscriptionCoordinator]: https://docs.rs/juniper_subscriptions/0.15.0/trait.SubscriptionCoordinator.html
|
|
||||||
[Connection]: https://docs.rs/juniper_subscriptions/0.15.0/struct.Connection.html
|
|
||||||
[SubscriptionConnection]: https://docs.rs/juniper_subscriptions/0.15.0/trait.SubscriptionConnection.html
|
|
||||||
<!--- --->
|
|
||||||
[Future]: https://docs.rs/futures/0.3.4/futures/future/trait.Future.html
|
|
||||||
[warp]: https://github.com/graphql-rust/juniper/tree/master/juniper_warp
|
|
||||||
[WS]: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
|
|
||||||
[GraphQLError]: https://docs.rs/juniper/0.14.2/juniper/enum.GraphQLError.html
|
|
||||||
[Schema]: ../schema/schemas_and_mutations.md
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../advanced/dataloaders.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../advanced/dataloaders.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -12,8 +12,7 @@ Original by Dempfi (https://github.com/dempfi/ayu)
|
||||||
}
|
}
|
||||||
|
|
||||||
.hljs-comment,
|
.hljs-comment,
|
||||||
.hljs-quote,
|
.hljs-quote {
|
||||||
.hljs-meta {
|
|
||||||
color: #5c6773;
|
color: #5c6773;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
@ -30,6 +29,7 @@ Original by Dempfi (https://github.com/dempfi/ayu)
|
||||||
}
|
}
|
||||||
|
|
||||||
.hljs-number,
|
.hljs-number,
|
||||||
|
.hljs-meta,
|
||||||
.hljs-builtin-name,
|
.hljs-builtin-name,
|
||||||
.hljs-literal,
|
.hljs-literal,
|
||||||
.hljs-type,
|
.hljs-type,
|
||||||
|
@ -69,3 +69,11 @@ Original by Dempfi (https://github.com/dempfi/ayu)
|
||||||
.hljs-strong {
|
.hljs-strong {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hljs-addition {
|
||||||
|
color: #91b362;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-deletion {
|
||||||
|
color: #d96c75;
|
||||||
|
}
|
||||||
|
|
308
master/book.js
308
master/book.js
|
@ -4,8 +4,8 @@
|
||||||
window.onunload = function () { };
|
window.onunload = function () { };
|
||||||
|
|
||||||
// Global variable, shared between modules
|
// Global variable, shared between modules
|
||||||
function playpen_text(playpen) {
|
function playground_text(playground) {
|
||||||
let code_block = playpen.querySelector("code");
|
let code_block = playground.querySelector("code");
|
||||||
|
|
||||||
if (window.ace && code_block.classList.contains("editable")) {
|
if (window.ace && code_block.classList.contains("editable")) {
|
||||||
let editor = window.ace.edit(code_block);
|
let editor = window.ace.edit(code_block);
|
||||||
|
@ -16,9 +16,6 @@ function playpen_text(playpen) {
|
||||||
}
|
}
|
||||||
|
|
||||||
(function codeSnippets() {
|
(function codeSnippets() {
|
||||||
// Hide Rust code lines prepended with a specific character
|
|
||||||
var hiding_character = "#";
|
|
||||||
|
|
||||||
function fetch_with_timeout(url, options, timeout = 6000) {
|
function fetch_with_timeout(url, options, timeout = 6000) {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
fetch(url, options),
|
fetch(url, options),
|
||||||
|
@ -26,8 +23,8 @@ function playpen_text(playpen) {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var playpens = Array.from(document.querySelectorAll(".playpen"));
|
var playgrounds = Array.from(document.querySelectorAll(".playground"));
|
||||||
if (playpens.length > 0) {
|
if (playgrounds.length > 0) {
|
||||||
fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
|
fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': "application/json",
|
'Content-Type': "application/json",
|
||||||
|
@ -39,21 +36,30 @@ function playpen_text(playpen) {
|
||||||
.then(response => {
|
.then(response => {
|
||||||
// get list of crates available in the rust playground
|
// get list of crates available in the rust playground
|
||||||
let playground_crates = response.crates.map(item => item["id"]);
|
let playground_crates = response.crates.map(item => item["id"]);
|
||||||
playpens.forEach(block => handle_crate_list_update(block, playground_crates));
|
playgrounds.forEach(block => handle_crate_list_update(block, playground_crates));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_crate_list_update(playpen_block, playground_crates) {
|
function handle_crate_list_update(playground_block, playground_crates) {
|
||||||
// update the play buttons after receiving the response
|
// update the play buttons after receiving the response
|
||||||
update_play_button(playpen_block, playground_crates);
|
update_play_button(playground_block, playground_crates);
|
||||||
|
|
||||||
// and install on change listener to dynamically update ACE editors
|
// and install on change listener to dynamically update ACE editors
|
||||||
if (window.ace) {
|
if (window.ace) {
|
||||||
let code_block = playpen_block.querySelector("code");
|
let code_block = playground_block.querySelector("code");
|
||||||
if (code_block.classList.contains("editable")) {
|
if (code_block.classList.contains("editable")) {
|
||||||
let editor = window.ace.edit(code_block);
|
let editor = window.ace.edit(code_block);
|
||||||
editor.addEventListener("change", function (e) {
|
editor.addEventListener("change", function (e) {
|
||||||
update_play_button(playpen_block, playground_crates);
|
update_play_button(playground_block, playground_crates);
|
||||||
|
});
|
||||||
|
// add Ctrl-Enter command to execute rust code
|
||||||
|
editor.commands.addCommand({
|
||||||
|
name: "run",
|
||||||
|
bindKey: {
|
||||||
|
win: "Ctrl-Enter",
|
||||||
|
mac: "Ctrl-Enter"
|
||||||
|
},
|
||||||
|
exec: _editor => run_rust_code(playground_block)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,7 +77,7 @@ function playpen_text(playpen) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get list of `extern crate`'s from snippet
|
// get list of `extern crate`'s from snippet
|
||||||
var txt = playpen_text(pre_block);
|
var txt = playground_text(pre_block);
|
||||||
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
|
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
|
||||||
var snippet_crates = [];
|
var snippet_crates = [];
|
||||||
var item;
|
var item;
|
||||||
|
@ -100,12 +106,19 @@ function playpen_text(playpen) {
|
||||||
code_block.append(result_block);
|
code_block.append(result_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = playpen_text(code_block);
|
let text = playground_text(code_block);
|
||||||
|
let classes = code_block.querySelector('code').classList;
|
||||||
|
let edition = "2015";
|
||||||
|
if(classes.contains("edition2018")) {
|
||||||
|
edition = "2018";
|
||||||
|
} else if(classes.contains("edition2021")) {
|
||||||
|
edition = "2021";
|
||||||
|
}
|
||||||
var params = {
|
var params = {
|
||||||
version: "stable",
|
version: "stable",
|
||||||
optimize: "0",
|
optimize: "0",
|
||||||
code: text
|
code: text,
|
||||||
|
edition: edition
|
||||||
};
|
};
|
||||||
|
|
||||||
if (text.indexOf("#![feature") !== -1) {
|
if (text.indexOf("#![feature") !== -1) {
|
||||||
|
@ -123,7 +136,15 @@ function playpen_text(playpen) {
|
||||||
body: JSON.stringify(params)
|
body: JSON.stringify(params)
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(response => result_block.innerText = response.result)
|
.then(response => {
|
||||||
|
if (response.result.trim() === '') {
|
||||||
|
result_block.innerText = "No output";
|
||||||
|
result_block.classList.add("result-no-output");
|
||||||
|
} else {
|
||||||
|
result_block.innerText = response.result;
|
||||||
|
result_block.classList.remove("result-no-output");
|
||||||
|
}
|
||||||
|
})
|
||||||
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
|
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,103 +154,68 @@ function playpen_text(playpen) {
|
||||||
languages: [], // Languages used for auto-detection
|
languages: [], // Languages used for auto-detection
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let code_nodes = Array
|
||||||
|
.from(document.querySelectorAll('code'))
|
||||||
|
// Don't highlight `inline code` blocks in headers.
|
||||||
|
.filter(function (node) {return !node.parentElement.classList.contains("header"); });
|
||||||
|
|
||||||
if (window.ace) {
|
if (window.ace) {
|
||||||
// language-rust class needs to be removed for editable
|
// language-rust class needs to be removed for editable
|
||||||
// blocks or highlightjs will capture events
|
// blocks or highlightjs will capture events
|
||||||
Array
|
code_nodes
|
||||||
.from(document.querySelectorAll('code.editable'))
|
.filter(function (node) {return node.classList.contains("editable"); })
|
||||||
.forEach(function (block) { block.classList.remove('language-rust'); });
|
.forEach(function (block) { block.classList.remove('language-rust'); });
|
||||||
|
|
||||||
Array
|
Array
|
||||||
.from(document.querySelectorAll('code:not(.editable)'))
|
code_nodes
|
||||||
|
.filter(function (node) {return !node.classList.contains("editable"); })
|
||||||
.forEach(function (block) { hljs.highlightBlock(block); });
|
.forEach(function (block) { hljs.highlightBlock(block); });
|
||||||
} else {
|
} else {
|
||||||
Array
|
code_nodes.forEach(function (block) { hljs.highlightBlock(block); });
|
||||||
.from(document.querySelectorAll('code'))
|
|
||||||
.forEach(function (block) { hljs.highlightBlock(block); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adding the hljs class gives code blocks the color css
|
// Adding the hljs class gives code blocks the color css
|
||||||
// even if highlighting doesn't apply
|
// even if highlighting doesn't apply
|
||||||
Array
|
code_nodes.forEach(function (block) { block.classList.add('hljs'); });
|
||||||
.from(document.querySelectorAll('code'))
|
|
||||||
.forEach(function (block) { block.classList.add('hljs'); });
|
|
||||||
|
|
||||||
Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
|
Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
|
||||||
|
|
||||||
var code_block = block;
|
var lines = Array.from(block.querySelectorAll('.boring'));
|
||||||
var pre_block = block.parentNode;
|
|
||||||
// hide lines
|
|
||||||
var lines = code_block.innerHTML.split("\n");
|
|
||||||
var first_non_hidden_line = false;
|
|
||||||
var lines_hidden = false;
|
|
||||||
var trimmed_line = "";
|
|
||||||
|
|
||||||
for (var n = 0; n < lines.length; n++) {
|
|
||||||
trimmed_line = lines[n].trim();
|
|
||||||
if (trimmed_line[0] == hiding_character && trimmed_line[1] != hiding_character) {
|
|
||||||
if (first_non_hidden_line) {
|
|
||||||
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)# ?/, "$1") + "</span>";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)# ?/, "$1") + "\n" + "</span>";
|
|
||||||
}
|
|
||||||
lines_hidden = true;
|
|
||||||
}
|
|
||||||
else if (first_non_hidden_line) {
|
|
||||||
lines[n] = "\n" + lines[n];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
first_non_hidden_line = true;
|
|
||||||
}
|
|
||||||
if (trimmed_line[0] == hiding_character && trimmed_line[1] == hiding_character) {
|
|
||||||
lines[n] = lines[n].replace("##", "#")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
code_block.innerHTML = lines.join("");
|
|
||||||
|
|
||||||
// If no lines were hidden, return
|
// If no lines were hidden, return
|
||||||
if (!lines_hidden) { return; }
|
if (!lines.length) { return; }
|
||||||
|
block.classList.add("hide-boring");
|
||||||
|
|
||||||
var buttons = document.createElement('div');
|
var buttons = document.createElement('div');
|
||||||
buttons.className = 'buttons';
|
buttons.className = 'buttons';
|
||||||
buttons.innerHTML = "<button class=\"fa fa-expand\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
|
buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
|
||||||
|
|
||||||
// add expand button
|
// add expand button
|
||||||
|
var pre_block = block.parentNode;
|
||||||
pre_block.insertBefore(buttons, pre_block.firstChild);
|
pre_block.insertBefore(buttons, pre_block.firstChild);
|
||||||
|
|
||||||
pre_block.querySelector('.buttons').addEventListener('click', function (e) {
|
pre_block.querySelector('.buttons').addEventListener('click', function (e) {
|
||||||
if (e.target.classList.contains('fa-expand')) {
|
if (e.target.classList.contains('fa-eye')) {
|
||||||
var lines = pre_block.querySelectorAll('span.hidden');
|
e.target.classList.remove('fa-eye');
|
||||||
|
e.target.classList.add('fa-eye-slash');
|
||||||
e.target.classList.remove('fa-expand');
|
|
||||||
e.target.classList.add('fa-compress');
|
|
||||||
e.target.title = 'Hide lines';
|
e.target.title = 'Hide lines';
|
||||||
e.target.setAttribute('aria-label', e.target.title);
|
e.target.setAttribute('aria-label', e.target.title);
|
||||||
|
|
||||||
Array.from(lines).forEach(function (line) {
|
block.classList.remove('hide-boring');
|
||||||
line.classList.remove('hidden');
|
} else if (e.target.classList.contains('fa-eye-slash')) {
|
||||||
line.classList.add('unhidden');
|
e.target.classList.remove('fa-eye-slash');
|
||||||
});
|
e.target.classList.add('fa-eye');
|
||||||
} else if (e.target.classList.contains('fa-compress')) {
|
|
||||||
var lines = pre_block.querySelectorAll('span.unhidden');
|
|
||||||
|
|
||||||
e.target.classList.remove('fa-compress');
|
|
||||||
e.target.classList.add('fa-expand');
|
|
||||||
e.target.title = 'Show hidden lines';
|
e.target.title = 'Show hidden lines';
|
||||||
e.target.setAttribute('aria-label', e.target.title);
|
e.target.setAttribute('aria-label', e.target.title);
|
||||||
|
|
||||||
Array.from(lines).forEach(function (line) {
|
block.classList.add('hide-boring');
|
||||||
line.classList.remove('unhidden');
|
|
||||||
line.classList.add('hidden');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (window.playground_copyable) {
|
||||||
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
|
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
|
||||||
var pre_block = block.parentNode;
|
var pre_block = block.parentNode;
|
||||||
if (!pre_block.classList.contains('playpen')) {
|
if (!pre_block.classList.contains('playground')) {
|
||||||
var buttons = pre_block.querySelector(".buttons");
|
var buttons = pre_block.querySelector(".buttons");
|
||||||
if (!buttons) {
|
if (!buttons) {
|
||||||
buttons = document.createElement('div');
|
buttons = document.createElement('div');
|
||||||
|
@ -246,9 +232,10 @@ function playpen_text(playpen) {
|
||||||
buttons.insertBefore(clipButton, buttons.firstChild);
|
buttons.insertBefore(clipButton, buttons.firstChild);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Process playpen code blocks
|
// Process playground code blocks
|
||||||
Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) {
|
Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) {
|
||||||
// Add play button
|
// Add play button
|
||||||
var buttons = pre_block.querySelector(".buttons");
|
var buttons = pre_block.querySelector(".buttons");
|
||||||
if (!buttons) {
|
if (!buttons) {
|
||||||
|
@ -263,18 +250,20 @@ function playpen_text(playpen) {
|
||||||
runCodeButton.title = 'Run this code';
|
runCodeButton.title = 'Run this code';
|
||||||
runCodeButton.setAttribute('aria-label', runCodeButton.title);
|
runCodeButton.setAttribute('aria-label', runCodeButton.title);
|
||||||
|
|
||||||
|
buttons.insertBefore(runCodeButton, buttons.firstChild);
|
||||||
|
runCodeButton.addEventListener('click', function (e) {
|
||||||
|
run_rust_code(pre_block);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (window.playground_copyable) {
|
||||||
var copyCodeClipboardButton = document.createElement('button');
|
var copyCodeClipboardButton = document.createElement('button');
|
||||||
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
|
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
|
||||||
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
|
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
|
||||||
copyCodeClipboardButton.title = 'Copy to clipboard';
|
copyCodeClipboardButton.title = 'Copy to clipboard';
|
||||||
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
|
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
|
||||||
|
|
||||||
buttons.insertBefore(runCodeButton, buttons.firstChild);
|
|
||||||
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
|
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
|
||||||
|
}
|
||||||
runCodeButton.addEventListener('click', function (e) {
|
|
||||||
run_rust_code(pre_block);
|
|
||||||
});
|
|
||||||
|
|
||||||
let code_block = pre_block.querySelector("code");
|
let code_block = pre_block.querySelector("code");
|
||||||
if (window.ace && code_block.classList.contains("editable")) {
|
if (window.ace && code_block.classList.contains("editable")) {
|
||||||
|
@ -308,7 +297,7 @@ function playpen_text(playpen) {
|
||||||
function showThemes() {
|
function showThemes() {
|
||||||
themePopup.style.display = 'block';
|
themePopup.style.display = 'block';
|
||||||
themeToggleButton.setAttribute('aria-expanded', true);
|
themeToggleButton.setAttribute('aria-expanded', true);
|
||||||
themePopup.querySelector("button#" + document.body.className).focus();
|
themePopup.querySelector("button#" + get_theme()).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
function hideThemes() {
|
function hideThemes() {
|
||||||
|
@ -317,7 +306,17 @@ function playpen_text(playpen) {
|
||||||
themeToggleButton.focus();
|
themeToggleButton.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
function set_theme(theme) {
|
function get_theme() {
|
||||||
|
var theme;
|
||||||
|
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
|
||||||
|
if (theme === null || theme === undefined) {
|
||||||
|
return default_theme;
|
||||||
|
} else {
|
||||||
|
return theme;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_theme(theme, store = true) {
|
||||||
let ace_theme;
|
let ace_theme;
|
||||||
|
|
||||||
if (theme == 'coal' || theme == 'navy') {
|
if (theme == 'coal' || theme == 'navy') {
|
||||||
|
@ -330,13 +329,11 @@ function playpen_text(playpen) {
|
||||||
stylesheets.ayuHighlight.disabled = false;
|
stylesheets.ayuHighlight.disabled = false;
|
||||||
stylesheets.tomorrowNight.disabled = true;
|
stylesheets.tomorrowNight.disabled = true;
|
||||||
stylesheets.highlight.disabled = true;
|
stylesheets.highlight.disabled = true;
|
||||||
|
|
||||||
ace_theme = "ace/theme/tomorrow_night";
|
ace_theme = "ace/theme/tomorrow_night";
|
||||||
} else {
|
} else {
|
||||||
stylesheets.ayuHighlight.disabled = true;
|
stylesheets.ayuHighlight.disabled = true;
|
||||||
stylesheets.tomorrowNight.disabled = true;
|
stylesheets.tomorrowNight.disabled = true;
|
||||||
stylesheets.highlight.disabled = false;
|
stylesheets.highlight.disabled = false;
|
||||||
|
|
||||||
ace_theme = "ace/theme/dawn";
|
ace_theme = "ace/theme/dawn";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,23 +347,20 @@ function playpen_text(playpen) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var previousTheme;
|
var previousTheme = get_theme();
|
||||||
try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { }
|
|
||||||
if (previousTheme === null || previousTheme === undefined) { previousTheme = 'light'; }
|
|
||||||
|
|
||||||
|
if (store) {
|
||||||
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
|
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
|
||||||
|
}
|
||||||
|
|
||||||
document.body.className = theme;
|
|
||||||
html.classList.remove(previousTheme);
|
html.classList.remove(previousTheme);
|
||||||
html.classList.add(theme);
|
html.classList.add(theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set theme
|
// Set theme
|
||||||
var theme;
|
var theme = get_theme();
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
|
|
||||||
set_theme(theme);
|
set_theme(theme, false);
|
||||||
|
|
||||||
themeToggleButton.addEventListener('click', function () {
|
themeToggleButton.addEventListener('click', function () {
|
||||||
if (themePopup.style.display === 'block') {
|
if (themePopup.style.display === 'block') {
|
||||||
|
@ -388,7 +382,7 @@ function playpen_text(playpen) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang-nursery/mdBook/issues/628
|
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
|
||||||
document.addEventListener('click', function(e) {
|
document.addEventListener('click', function(e) {
|
||||||
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
|
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
|
||||||
hideThemes();
|
hideThemes();
|
||||||
|
@ -435,6 +429,7 @@ function playpen_text(playpen) {
|
||||||
var sidebar = document.getElementById("sidebar");
|
var sidebar = document.getElementById("sidebar");
|
||||||
var sidebarLinks = document.querySelectorAll('#sidebar a');
|
var sidebarLinks = document.querySelectorAll('#sidebar a');
|
||||||
var sidebarToggleButton = document.getElementById("sidebar-toggle");
|
var sidebarToggleButton = document.getElementById("sidebar-toggle");
|
||||||
|
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
|
||||||
var firstContact = null;
|
var firstContact = null;
|
||||||
|
|
||||||
function showSidebar() {
|
function showSidebar() {
|
||||||
|
@ -448,6 +443,17 @@ function playpen_text(playpen) {
|
||||||
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
|
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle');
|
||||||
|
|
||||||
|
function toggleSection(ev) {
|
||||||
|
ev.currentTarget.parentElement.classList.toggle('expanded');
|
||||||
|
}
|
||||||
|
|
||||||
|
Array.from(sidebarAnchorToggles).forEach(function (el) {
|
||||||
|
el.addEventListener('click', toggleSection);
|
||||||
|
});
|
||||||
|
|
||||||
function hideSidebar() {
|
function hideSidebar() {
|
||||||
html.classList.remove('sidebar-visible')
|
html.classList.remove('sidebar-visible')
|
||||||
html.classList.add('sidebar-hidden');
|
html.classList.add('sidebar-hidden');
|
||||||
|
@ -462,6 +468,11 @@ function playpen_text(playpen) {
|
||||||
// Toggle sidebar
|
// Toggle sidebar
|
||||||
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
|
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
|
||||||
if (html.classList.contains("sidebar-hidden")) {
|
if (html.classList.contains("sidebar-hidden")) {
|
||||||
|
var current_width = parseInt(
|
||||||
|
document.documentElement.style.getPropertyValue('--sidebar-width'), 10);
|
||||||
|
if (current_width < 150) {
|
||||||
|
document.documentElement.style.setProperty('--sidebar-width', '150px');
|
||||||
|
}
|
||||||
showSidebar();
|
showSidebar();
|
||||||
} else if (html.classList.contains("sidebar-visible")) {
|
} else if (html.classList.contains("sidebar-visible")) {
|
||||||
hideSidebar();
|
hideSidebar();
|
||||||
|
@ -474,6 +485,32 @@ function playpen_text(playpen) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
sidebarResizeHandle.addEventListener('mousedown', initResize, false);
|
||||||
|
|
||||||
|
function initResize(e) {
|
||||||
|
window.addEventListener('mousemove', resize, false);
|
||||||
|
window.addEventListener('mouseup', stopResize, false);
|
||||||
|
html.classList.add('sidebar-resizing');
|
||||||
|
}
|
||||||
|
function resize(e) {
|
||||||
|
var pos = (e.clientX - sidebar.offsetLeft);
|
||||||
|
if (pos < 20) {
|
||||||
|
hideSidebar();
|
||||||
|
} else {
|
||||||
|
if (html.classList.contains("sidebar-hidden")) {
|
||||||
|
showSidebar();
|
||||||
|
}
|
||||||
|
pos = Math.min(pos, window.innerWidth - 100);
|
||||||
|
document.documentElement.style.setProperty('--sidebar-width', pos + 'px');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//on mouseup remove windows functions mousemove & mouseup
|
||||||
|
function stopResize(e) {
|
||||||
|
html.classList.remove('sidebar-resizing');
|
||||||
|
window.removeEventListener('mousemove', resize, false);
|
||||||
|
window.removeEventListener('mouseup', stopResize, false);
|
||||||
|
}
|
||||||
|
|
||||||
document.addEventListener('touchstart', function (e) {
|
document.addEventListener('touchstart', function (e) {
|
||||||
firstContact = {
|
firstContact = {
|
||||||
x: e.touches[0].clientX,
|
x: e.touches[0].clientX,
|
||||||
|
@ -500,9 +537,10 @@ function playpen_text(playpen) {
|
||||||
}, { passive: true });
|
}, { passive: true });
|
||||||
|
|
||||||
// Scroll sidebar to current active section
|
// Scroll sidebar to current active section
|
||||||
var activeSection = sidebar.querySelector(".active");
|
var activeSection = document.getElementById("sidebar").querySelector(".active");
|
||||||
if (activeSection) {
|
if (activeSection) {
|
||||||
sidebar.scrollTop = activeSection.offsetTop;
|
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
|
||||||
|
activeSection.scrollIntoView({ block: 'center' });
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
@ -543,11 +581,11 @@ function playpen_text(playpen) {
|
||||||
elem.className = 'fa fa-copy tooltipped';
|
elem.className = 'fa fa-copy tooltipped';
|
||||||
}
|
}
|
||||||
|
|
||||||
var clipboardSnippets = new Clipboard('.clip-button', {
|
var clipboardSnippets = new ClipboardJS('.clip-button', {
|
||||||
text: function (trigger) {
|
text: function (trigger) {
|
||||||
hideTooltip(trigger);
|
hideTooltip(trigger);
|
||||||
let playpen = trigger.closest("pre");
|
let playground = trigger.closest("pre");
|
||||||
return playpen_text(playpen);
|
return playground_text(playground);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -575,26 +613,60 @@ function playpen_text(playpen) {
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
(function autoHideMenu() {
|
(function controllMenu() {
|
||||||
var menu = document.getElementById('menu-bar');
|
var menu = document.getElementById('menu-bar');
|
||||||
|
|
||||||
var previousScrollTop = document.scrollingElement.scrollTop;
|
(function controllPosition() {
|
||||||
|
var scrollTop = document.scrollingElement.scrollTop;
|
||||||
|
var prevScrollTop = scrollTop;
|
||||||
|
var minMenuY = -menu.clientHeight - 50;
|
||||||
|
// When the script loads, the page can be at any scroll (e.g. if you reforesh it).
|
||||||
|
menu.style.top = scrollTop + 'px';
|
||||||
|
// Same as parseInt(menu.style.top.slice(0, -2), but faster
|
||||||
|
var topCache = menu.style.top.slice(0, -2);
|
||||||
|
menu.classList.remove('sticky');
|
||||||
|
var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster
|
||||||
document.addEventListener('scroll', function () {
|
document.addEventListener('scroll', function () {
|
||||||
if (menu.classList.contains('folded') && document.scrollingElement.scrollTop < previousScrollTop) {
|
scrollTop = Math.max(document.scrollingElement.scrollTop, 0);
|
||||||
menu.classList.remove('folded');
|
// `null` means that it doesn't need to be updated
|
||||||
} else if (!menu.classList.contains('folded') && document.scrollingElement.scrollTop > previousScrollTop) {
|
var nextSticky = null;
|
||||||
menu.classList.add('folded');
|
var nextTop = null;
|
||||||
|
var scrollDown = scrollTop > prevScrollTop;
|
||||||
|
var menuPosAbsoluteY = topCache - scrollTop;
|
||||||
|
if (scrollDown) {
|
||||||
|
nextSticky = false;
|
||||||
|
if (menuPosAbsoluteY > 0) {
|
||||||
|
nextTop = prevScrollTop;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (!menu.classList.contains('bordered') && document.scrollingElement.scrollTop > 0) {
|
if (menuPosAbsoluteY > 0) {
|
||||||
|
nextSticky = true;
|
||||||
|
} else if (menuPosAbsoluteY < minMenuY) {
|
||||||
|
nextTop = prevScrollTop + minMenuY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nextSticky === true && stickyCache === false) {
|
||||||
|
menu.classList.add('sticky');
|
||||||
|
stickyCache = true;
|
||||||
|
} else if (nextSticky === false && stickyCache === true) {
|
||||||
|
menu.classList.remove('sticky');
|
||||||
|
stickyCache = false;
|
||||||
|
}
|
||||||
|
if (nextTop !== null) {
|
||||||
|
menu.style.top = nextTop + 'px';
|
||||||
|
topCache = nextTop;
|
||||||
|
}
|
||||||
|
prevScrollTop = scrollTop;
|
||||||
|
}, { passive: true });
|
||||||
|
})();
|
||||||
|
(function controllBorder() {
|
||||||
|
menu.classList.remove('bordered');
|
||||||
|
document.addEventListener('scroll', function () {
|
||||||
|
if (menu.offsetTop === 0) {
|
||||||
|
menu.classList.remove('bordered');
|
||||||
|
} else {
|
||||||
menu.classList.add('bordered');
|
menu.classList.add('bordered');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (menu.classList.contains('bordered') && document.scrollingElement.scrollTop === 0) {
|
|
||||||
menu.classList.remove('bordered');
|
|
||||||
}
|
|
||||||
|
|
||||||
previousScrollTop = document.scrollingElement.scrollTop;
|
|
||||||
}, { passive: true });
|
}, { passive: true });
|
||||||
|
})();
|
||||||
})();
|
})();
|
||||||
|
|
4
master/clipboard.min.js
vendored
4
master/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -8,7 +8,9 @@
|
||||||
::-webkit-scrollbar-thumb {
|
::-webkit-scrollbar-thumb {
|
||||||
background: var(--scrollbar);
|
background: var(--scrollbar);
|
||||||
}
|
}
|
||||||
|
html {
|
||||||
|
scrollbar-color: var(--scrollbar) var(--bg);
|
||||||
|
}
|
||||||
#searchresults a,
|
#searchresults a,
|
||||||
.content a:link,
|
.content a:link,
|
||||||
a:visited,
|
a:visited,
|
||||||
|
@ -18,14 +20,13 @@ a > .hljs {
|
||||||
|
|
||||||
/* Menu Bar */
|
/* Menu Bar */
|
||||||
|
|
||||||
#menu-bar {
|
#menu-bar,
|
||||||
position: -webkit-sticky;
|
#menu-bar-hover-placeholder {
|
||||||
position: sticky;
|
|
||||||
top: 0;
|
|
||||||
z-index: 101;
|
z-index: 101;
|
||||||
margin: auto calc(0px - var(--page-padding));
|
margin: auto calc(0px - var(--page-padding));
|
||||||
}
|
}
|
||||||
#menu-bar > #menu-bar-sticky-container {
|
#menu-bar {
|
||||||
|
position: relative;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
background-color: var(--bg);
|
background-color: var(--bg);
|
||||||
|
@ -33,17 +34,28 @@ a > .hljs {
|
||||||
border-bottom-width: 1px;
|
border-bottom-width: 1px;
|
||||||
border-bottom-style: solid;
|
border-bottom-style: solid;
|
||||||
}
|
}
|
||||||
.js #menu-bar > #menu-bar-sticky-container {
|
#menu-bar.sticky,
|
||||||
transition: transform 0.3s;
|
.js #menu-bar-hover-placeholder:hover + #menu-bar,
|
||||||
|
.js #menu-bar:hover,
|
||||||
|
.js.sidebar-visible #menu-bar {
|
||||||
|
position: -webkit-sticky;
|
||||||
|
position: sticky;
|
||||||
|
top: 0 !important;
|
||||||
}
|
}
|
||||||
#menu-bar.bordered > #menu-bar-sticky-container {
|
#menu-bar-hover-placeholder {
|
||||||
|
position: sticky;
|
||||||
|
position: -webkit-sticky;
|
||||||
|
top: 0;
|
||||||
|
height: var(--menu-bar-height);
|
||||||
|
}
|
||||||
|
#menu-bar.bordered {
|
||||||
border-bottom-color: var(--table-border-color);
|
border-bottom-color: var(--table-border-color);
|
||||||
}
|
}
|
||||||
#menu-bar i, #menu-bar .icon-button {
|
#menu-bar i, #menu-bar .icon-button {
|
||||||
position: relative;
|
position: relative;
|
||||||
padding: 0 8px;
|
padding: 0 8px;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
line-height: 50px;
|
line-height: var(--menu-bar-height);
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: color 0.5s;
|
transition: color 0.5s;
|
||||||
}
|
}
|
||||||
|
@ -63,12 +75,11 @@ a > .hljs {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#print-button {
|
.right-buttons {
|
||||||
margin: 0 15px;
|
margin: 0 15px;
|
||||||
}
|
}
|
||||||
|
.right-buttons a {
|
||||||
html:not(.sidebar-visible) #menu-bar:not(:hover).folded > #menu-bar-sticky-container {
|
text-decoration: none;
|
||||||
transform: translateY(-60px);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.left-buttons {
|
.left-buttons {
|
||||||
|
@ -82,8 +93,8 @@ html:not(.sidebar-visible) #menu-bar:not(:hover).folded > #menu-bar-sticky-conta
|
||||||
.menu-title {
|
.menu-title {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
font-weight: 200;
|
font-weight: 200;
|
||||||
font-size: 20px;
|
font-size: 2.4rem;
|
||||||
line-height: 50px;
|
line-height: var(--menu-bar-height);
|
||||||
text-align: center;
|
text-align: center;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
@ -121,7 +132,7 @@ html:not(.sidebar-visible) #menu-bar:not(:hover).folded > #menu-bar-sticky-conta
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
|
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 50px; /* Height of menu-bar */
|
top: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
max-width: 150px;
|
max-width: 150px;
|
||||||
|
@ -132,10 +143,14 @@ html:not(.sidebar-visible) #menu-bar:not(:hover).folded > #menu-bar-sticky-conta
|
||||||
align-content: center;
|
align-content: center;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
transition: color 0.5s;
|
transition: color 0.5s, background-color 0.5s;
|
||||||
}
|
}
|
||||||
|
|
||||||
.nav-chapters:hover { text-decoration: none; }
|
.nav-chapters:hover {
|
||||||
|
text-decoration: none;
|
||||||
|
background-color: var(--theme-hover);
|
||||||
|
transition: background-color 0.15s, color 0.15s;
|
||||||
|
}
|
||||||
|
|
||||||
.nav-wrapper {
|
.nav-wrapper {
|
||||||
margin-top: 50px;
|
margin-top: 50px;
|
||||||
|
@ -173,11 +188,14 @@ html:not(.sidebar-visible) #menu-bar:not(:hover).folded > #menu-bar-sticky-conta
|
||||||
/* Inline code */
|
/* Inline code */
|
||||||
|
|
||||||
:not(pre) > .hljs {
|
:not(pre) > .hljs {
|
||||||
display: inline-block;
|
display: inline;
|
||||||
vertical-align: middle;
|
|
||||||
padding: 0.1em 0.3em;
|
padding: 0.1em 0.3em;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
:not(pre):not(a) > .hljs {
|
||||||
color: var(--inline-code-color);
|
color: var(--inline-code-color);
|
||||||
|
overflow-x: initial;
|
||||||
}
|
}
|
||||||
|
|
||||||
a:hover > .hljs {
|
a:hover > .hljs {
|
||||||
|
@ -298,8 +316,6 @@ ul#searchresults span.teaser em {
|
||||||
top: 0;
|
top: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: var(--sidebar-width);
|
width: var(--sidebar-width);
|
||||||
overflow-y: auto;
|
|
||||||
padding: 10px 10px;
|
|
||||||
font-size: 0.875em;
|
font-size: 0.875em;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
-webkit-overflow-scrolling: touch;
|
-webkit-overflow-scrolling: touch;
|
||||||
|
@ -307,12 +323,39 @@ ul#searchresults span.teaser em {
|
||||||
background-color: var(--sidebar-bg);
|
background-color: var(--sidebar-bg);
|
||||||
color: var(--sidebar-fg);
|
color: var(--sidebar-fg);
|
||||||
}
|
}
|
||||||
.js .sidebar {
|
.sidebar-resizing {
|
||||||
|
-moz-user-select: none;
|
||||||
|
-webkit-user-select: none;
|
||||||
|
-ms-user-select: none;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.js:not(.sidebar-resizing) .sidebar {
|
||||||
transition: transform 0.3s; /* Animation: slide away */
|
transition: transform 0.3s; /* Animation: slide away */
|
||||||
}
|
}
|
||||||
.sidebar code {
|
.sidebar code {
|
||||||
line-height: 2em;
|
line-height: 2em;
|
||||||
}
|
}
|
||||||
|
.sidebar .sidebar-scrollbox {
|
||||||
|
overflow-y: auto;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
padding: 10px 10px;
|
||||||
|
}
|
||||||
|
.sidebar .sidebar-resize-handle {
|
||||||
|
position: absolute;
|
||||||
|
cursor: col-resize;
|
||||||
|
width: 0;
|
||||||
|
right: 0;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
}
|
||||||
|
.js .sidebar .sidebar-resize-handle {
|
||||||
|
cursor: col-resize;
|
||||||
|
width: 5px;
|
||||||
|
}
|
||||||
.sidebar-hidden .sidebar {
|
.sidebar-hidden .sidebar {
|
||||||
transform: translateX(calc(0px - var(--sidebar-width)));
|
transform: translateX(calc(0px - var(--sidebar-width)));
|
||||||
}
|
}
|
||||||
|
@ -338,22 +381,57 @@ ul#searchresults span.teaser em {
|
||||||
padding-left: 0;
|
padding-left: 0;
|
||||||
line-height: 2.2em;
|
line-height: 2.2em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chapter ol {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
.chapter li {
|
.chapter li {
|
||||||
|
display: flex;
|
||||||
color: var(--sidebar-non-existant);
|
color: var(--sidebar-non-existant);
|
||||||
}
|
}
|
||||||
.chapter li a {
|
.chapter li a {
|
||||||
color: var(--sidebar-fg);
|
|
||||||
display: block;
|
display: block;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
|
color: var(--sidebar-fg);
|
||||||
}
|
}
|
||||||
.chapter li a:hover { text-decoration: none }
|
|
||||||
.chapter li .active,
|
.chapter li a:hover {
|
||||||
a:hover {
|
|
||||||
/* Animate color change */
|
|
||||||
color: var(--sidebar-active);
|
color: var(--sidebar-active);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chapter li a.active {
|
||||||
|
color: var(--sidebar-active);
|
||||||
|
}
|
||||||
|
|
||||||
|
.chapter li > a.toggle {
|
||||||
|
cursor: pointer;
|
||||||
|
display: block;
|
||||||
|
margin-left: auto;
|
||||||
|
padding: 0 10px;
|
||||||
|
user-select: none;
|
||||||
|
opacity: 0.68;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chapter li > a.toggle div {
|
||||||
|
transition: transform 0.5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* collapse the section */
|
||||||
|
.chapter li:not(.expanded) + li > ol {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chapter li.chapter-item {
|
||||||
|
line-height: 1.5em;
|
||||||
|
margin-top: 0.6em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chapter li.expanded > a.toggle div {
|
||||||
|
transform: rotate(90deg);
|
||||||
|
}
|
||||||
|
|
||||||
.spacer {
|
.spacer {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 3px;
|
height: 3px;
|
||||||
|
@ -379,7 +457,7 @@ a:hover {
|
||||||
.theme-popup {
|
.theme-popup {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 10px;
|
left: 10px;
|
||||||
top: 50px;
|
top: var(--menu-bar-height);
|
||||||
z-index: 1000;
|
z-index: 1000;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
font-size: 0.7em;
|
font-size: 0.7em;
|
||||||
|
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
@import 'variables.css';
|
@import 'variables.css';
|
||||||
|
|
||||||
|
:root {
|
||||||
|
/* Browser default font-size is 16px, this way 1 rem = 10px */
|
||||||
|
font-size: 62.5%;
|
||||||
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
font-family: "Open Sans", sans-serif;
|
font-family: "Open Sans", sans-serif;
|
||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
|
@ -11,19 +16,25 @@ html {
|
||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-size: 1rem;
|
font-size: 1.6rem;
|
||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
code {
|
code {
|
||||||
font-family: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace;
|
font-family: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace !important;
|
||||||
font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */
|
font-size: 0.875em; /* please adjust the ace font size accordingly in editor.js */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Don't change font size in headers. */
|
||||||
|
h1 code, h2 code, h3 code, h4 code, h5 code, h6 code {
|
||||||
|
font-size: unset;
|
||||||
|
}
|
||||||
|
|
||||||
.left { float: left; }
|
.left { float: left; }
|
||||||
.right { float: right; }
|
.right { float: right; }
|
||||||
.hidden { display: none; }
|
.boring { opacity: 0.6; }
|
||||||
.play-button.hidden { display: none; }
|
.hide-boring .boring { display: none; }
|
||||||
|
.hidden { display: none !important; }
|
||||||
|
|
||||||
h2, h3 { margin-top: 2.5em; }
|
h2, h3 { margin-top: 2.5em; }
|
||||||
h4, h5 { margin-top: 2em; }
|
h4, h5 { margin-top: 2em; }
|
||||||
|
@ -34,24 +45,35 @@ h4, h5 { margin-top: 2em; }
|
||||||
margin-top: 1em;
|
margin-top: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
a.header:target h1:before,
|
h1:target::before,
|
||||||
a.header:target h2:before,
|
h2:target::before,
|
||||||
a.header:target h3:before,
|
h3:target::before,
|
||||||
a.header:target h4:before {
|
h4:target::before,
|
||||||
|
h5:target::before,
|
||||||
|
h6:target::before {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
content: "»";
|
content: "»";
|
||||||
margin-left: -30px;
|
margin-left: -30px;
|
||||||
width: 30px;
|
width: 30px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* This is broken on Safari as of version 14, but is fixed
|
||||||
|
in Safari Technology Preview 117 which I think will be Safari 14.2.
|
||||||
|
https://bugs.webkit.org/show_bug.cgi?id=218076
|
||||||
|
*/
|
||||||
|
:target {
|
||||||
|
scroll-margin-top: calc(var(--menu-bar-height) + 0.5em);
|
||||||
|
}
|
||||||
|
|
||||||
.page {
|
.page {
|
||||||
outline: 0;
|
outline: 0;
|
||||||
padding: 0 var(--page-padding);
|
padding: 0 var(--page-padding);
|
||||||
|
margin-top: calc(0px - var(--menu-bar-height)); /* Compensate for the #menu-bar-hover-placeholder */
|
||||||
}
|
}
|
||||||
.page-wrapper {
|
.page-wrapper {
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
.js .page-wrapper {
|
.js:not(.sidebar-resizing) .page-wrapper {
|
||||||
transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
|
transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,9 +87,12 @@ a.header:target h4:before {
|
||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
max-width: var(--content-max-width);
|
max-width: var(--content-max-width);
|
||||||
}
|
}
|
||||||
|
.content p { line-height: 1.45em; }
|
||||||
|
.content ol { line-height: 1.45em; }
|
||||||
|
.content ul { line-height: 1.45em; }
|
||||||
.content a { text-decoration: none; }
|
.content a { text-decoration: none; }
|
||||||
.content a:hover { text-decoration: underline; }
|
.content a:hover { text-decoration: underline; }
|
||||||
.content img { max-width: 100%; }
|
.content img, .content video { max-width: 100%; }
|
||||||
.content .header:link,
|
.content .header:link,
|
||||||
.content .header:visited {
|
.content .header:visited {
|
||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
|
@ -92,6 +117,9 @@ table thead td {
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
border: none;
|
border: none;
|
||||||
}
|
}
|
||||||
|
table thead th {
|
||||||
|
padding: 3px 20px;
|
||||||
|
}
|
||||||
table thead tr {
|
table thead tr {
|
||||||
border: 1px var(--table-header-bg) solid;
|
border: 1px var(--table-header-bg) solid;
|
||||||
}
|
}
|
||||||
|
@ -142,3 +170,12 @@ blockquote {
|
||||||
visibility: visible;
|
visibility: visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.chapter li.part-title {
|
||||||
|
color: var(--sidebar-fg);
|
||||||
|
margin: 5px 0px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.result-no-output {
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
|
@ -5,12 +5,13 @@
|
||||||
--sidebar-width: 300px;
|
--sidebar-width: 300px;
|
||||||
--page-padding: 15px;
|
--page-padding: 15px;
|
||||||
--content-max-width: 750px;
|
--content-max-width: 750px;
|
||||||
|
--menu-bar-height: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Themes */
|
/* Themes */
|
||||||
|
|
||||||
.ayu {
|
.ayu {
|
||||||
--bg: #0f1419;
|
--bg: hsl(210, 25%, 8%);
|
||||||
--fg: #c5c5c5;
|
--fg: #c5c5c5;
|
||||||
|
|
||||||
--sidebar-bg: #14191f;
|
--sidebar-bg: #14191f;
|
||||||
|
@ -32,12 +33,12 @@
|
||||||
--theme-popup-border: #5c6773;
|
--theme-popup-border: #5c6773;
|
||||||
--theme-hover: #191f26;
|
--theme-hover: #191f26;
|
||||||
|
|
||||||
--quote-bg: #262933;
|
--quote-bg: hsl(226, 15%, 17%);
|
||||||
--quote-border: lighten(var(--quote-bg), 5%);
|
--quote-border: hsl(226, 15%, 22%);
|
||||||
|
|
||||||
--table-border-color: lighten(var(--bg), 5%);
|
--table-border-color: hsl(210, 25%, 13%);
|
||||||
--table-header-bg: lighten(var(--bg), 20%);
|
--table-header-bg: hsl(210, 25%, 28%);
|
||||||
--table-alternate-bg: lighten(var(--bg), 3%);
|
--table-alternate-bg: hsl(210, 25%, 11%);
|
||||||
|
|
||||||
--searchbar-border-color: #848484;
|
--searchbar-border-color: #848484;
|
||||||
--searchbar-bg: #424242;
|
--searchbar-bg: #424242;
|
||||||
|
@ -50,7 +51,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.coal {
|
.coal {
|
||||||
--bg: #141617;
|
--bg: hsl(200, 7%, 8%);
|
||||||
--fg: #98a3ad;
|
--fg: #98a3ad;
|
||||||
|
|
||||||
--sidebar-bg: #292c2f;
|
--sidebar-bg: #292c2f;
|
||||||
|
@ -72,12 +73,12 @@
|
||||||
--theme-popup-border: #43484d;
|
--theme-popup-border: #43484d;
|
||||||
--theme-hover: #1f2124;
|
--theme-hover: #1f2124;
|
||||||
|
|
||||||
--quote-bg: #242637;
|
--quote-bg: hsl(234, 21%, 18%);
|
||||||
--quote-border: lighten(var(--quote-bg), 5%);
|
--quote-border: hsl(234, 21%, 23%);
|
||||||
|
|
||||||
--table-border-color: lighten(var(--bg), 5%);
|
--table-border-color: hsl(200, 7%, 13%);
|
||||||
--table-header-bg: lighten(var(--bg), 20%);
|
--table-header-bg: hsl(200, 7%, 28%);
|
||||||
--table-alternate-bg: lighten(var(--bg), 3%);
|
--table-alternate-bg: hsl(200, 7%, 11%);
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
--searchbar-border-color: #aaa;
|
||||||
--searchbar-bg: #b7b7b7;
|
--searchbar-bg: #b7b7b7;
|
||||||
|
@ -90,34 +91,34 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.light {
|
.light {
|
||||||
--bg: #ffffff;
|
--bg: hsl(0, 0%, 100%);
|
||||||
--fg: #333333;
|
--fg: hsl(0, 0%, 0%);
|
||||||
|
|
||||||
--sidebar-bg: #fafafa;
|
--sidebar-bg: #fafafa;
|
||||||
--sidebar-fg: #364149;
|
--sidebar-fg: hsl(0, 0%, 0%);
|
||||||
--sidebar-non-existant: #aaaaaa;
|
--sidebar-non-existant: #aaaaaa;
|
||||||
--sidebar-active: #008cff;
|
--sidebar-active: #1f1fff;
|
||||||
--sidebar-spacer: #f4f4f4;
|
--sidebar-spacer: #f4f4f4;
|
||||||
|
|
||||||
--scrollbar: #cccccc;
|
--scrollbar: #8F8F8F;
|
||||||
|
|
||||||
--icons: #cccccc;
|
--icons: #747474;
|
||||||
--icons-hover: #333333;
|
--icons-hover: #000000;
|
||||||
|
|
||||||
--links: #4183c4;
|
--links: #20609f;
|
||||||
|
|
||||||
--inline-code-color: #6e6b5e;
|
--inline-code-color: #301900;
|
||||||
|
|
||||||
--theme-popup-bg: #fafafa;
|
--theme-popup-bg: #fafafa;
|
||||||
--theme-popup-border: #cccccc;
|
--theme-popup-border: #cccccc;
|
||||||
--theme-hover: #e6e6e6;
|
--theme-hover: #e6e6e6;
|
||||||
|
|
||||||
--quote-bg: #f2f7f9;
|
--quote-bg: hsl(197, 37%, 96%);
|
||||||
--quote-border: darken(var(--quote-bg), 5%);
|
--quote-border: hsl(197, 37%, 91%);
|
||||||
|
|
||||||
--table-border-color: darken(var(--bg), 5%);
|
--table-border-color: hsl(0, 0%, 95%);
|
||||||
--table-header-bg: darken(var(--bg), 20%);
|
--table-header-bg: hsl(0, 0%, 80%);
|
||||||
--table-alternate-bg: darken(var(--bg), 3%);
|
--table-alternate-bg: hsl(0, 0%, 97%);
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
--searchbar-border-color: #aaa;
|
||||||
--searchbar-bg: #fafafa;
|
--searchbar-bg: #fafafa;
|
||||||
|
@ -130,7 +131,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.navy {
|
.navy {
|
||||||
--bg: #161923;
|
--bg: hsl(226, 23%, 11%);
|
||||||
--fg: #bcbdd0;
|
--fg: #bcbdd0;
|
||||||
|
|
||||||
--sidebar-bg: #282d3f;
|
--sidebar-bg: #282d3f;
|
||||||
|
@ -152,12 +153,12 @@
|
||||||
--theme-popup-border: #737480;
|
--theme-popup-border: #737480;
|
||||||
--theme-hover: #282e40;
|
--theme-hover: #282e40;
|
||||||
|
|
||||||
--quote-bg: #262933;
|
--quote-bg: hsl(226, 15%, 17%);
|
||||||
--quote-border: lighten(var(--quote-bg), 5%);
|
--quote-border: hsl(226, 15%, 22%);
|
||||||
|
|
||||||
--table-border-color: lighten(var(--bg), 5%);
|
--table-border-color: hsl(226, 23%, 16%);
|
||||||
--table-header-bg: lighten(var(--bg), 20%);
|
--table-header-bg: hsl(226, 23%, 31%);
|
||||||
--table-alternate-bg: lighten(var(--bg), 3%);
|
--table-alternate-bg: hsl(226, 23%, 14%);
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
--searchbar-border-color: #aaa;
|
||||||
--searchbar-bg: #aeaec6;
|
--searchbar-bg: #aeaec6;
|
||||||
|
@ -170,7 +171,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.rust {
|
.rust {
|
||||||
--bg: #e1e1db;
|
--bg: hsl(60, 9%, 87%);
|
||||||
--fg: #262625;
|
--fg: #262625;
|
||||||
|
|
||||||
--sidebar-bg: #3b2e2a;
|
--sidebar-bg: #3b2e2a;
|
||||||
|
@ -192,12 +193,12 @@
|
||||||
--theme-popup-border: #b38f6b;
|
--theme-popup-border: #b38f6b;
|
||||||
--theme-hover: #99908a;
|
--theme-hover: #99908a;
|
||||||
|
|
||||||
--quote-bg: #c1c1bb;
|
--quote-bg: hsl(60, 5%, 75%);
|
||||||
--quote-border: darken(var(--quote-bg), 5%);
|
--quote-border: hsl(60, 5%, 70%);
|
||||||
|
|
||||||
--table-border-color: darken(var(--bg), 5%);
|
--table-border-color: hsl(60, 9%, 82%);
|
||||||
--table-header-bg: #b3a497;
|
--table-header-bg: #b3a497;
|
||||||
--table-alternate-bg: darken(var(--bg), 3%);
|
--table-alternate-bg: hsl(60, 9%, 84%);
|
||||||
|
|
||||||
--searchbar-border-color: #aaa;
|
--searchbar-border-color: #aaa;
|
||||||
--searchbar-bg: #fafafa;
|
--searchbar-bg: #fafafa;
|
||||||
|
@ -208,3 +209,45 @@
|
||||||
--searchresults-li-bg: #dec2a2;
|
--searchresults-li-bg: #dec2a2;
|
||||||
--search-mark-bg: #e69f67;
|
--search-mark-bg: #e69f67;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.light.no-js {
|
||||||
|
--bg: hsl(200, 7%, 8%);
|
||||||
|
--fg: #98a3ad;
|
||||||
|
|
||||||
|
--sidebar-bg: #292c2f;
|
||||||
|
--sidebar-fg: #a1adb8;
|
||||||
|
--sidebar-non-existant: #505254;
|
||||||
|
--sidebar-active: #3473ad;
|
||||||
|
--sidebar-spacer: #393939;
|
||||||
|
|
||||||
|
--scrollbar: var(--sidebar-fg);
|
||||||
|
|
||||||
|
--icons: #43484d;
|
||||||
|
--icons-hover: #b3c0cc;
|
||||||
|
|
||||||
|
--links: #2b79a2;
|
||||||
|
|
||||||
|
--inline-code-color: #c5c8c6;;
|
||||||
|
|
||||||
|
--theme-popup-bg: #141617;
|
||||||
|
--theme-popup-border: #43484d;
|
||||||
|
--theme-hover: #1f2124;
|
||||||
|
|
||||||
|
--quote-bg: hsl(234, 21%, 18%);
|
||||||
|
--quote-border: hsl(234, 21%, 23%);
|
||||||
|
|
||||||
|
--table-border-color: hsl(200, 7%, 13%);
|
||||||
|
--table-header-bg: hsl(200, 7%, 28%);
|
||||||
|
--table-alternate-bg: hsl(200, 7%, 11%);
|
||||||
|
|
||||||
|
--searchbar-border-color: #aaa;
|
||||||
|
--searchbar-bg: #b7b7b7;
|
||||||
|
--searchbar-fg: #000;
|
||||||
|
--searchbar-shadow-color: #aaa;
|
||||||
|
--searchresults-header-fg: #666;
|
||||||
|
--searchresults-border-color: #98a3ad;
|
||||||
|
--searchresults-li-bg: #2b2b2f;
|
||||||
|
--search-mark-bg: #355c7d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
@ -1,14 +1,18 @@
|
||||||
/* Base16 Atelier Dune Light - Theme */
|
/*
|
||||||
/* by Bram de Haan (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune) */
|
* An increased contrast highlighting scheme loosely based on the
|
||||||
/* Original Base16 color scheme by Chris Kempson (https://github.com/chriskempson/base16) */
|
* "Base16 Atelier Dune Light" theme by Bram de Haan
|
||||||
|
* (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune)
|
||||||
|
* Original Base16 color scheme by Chris Kempson
|
||||||
|
* (https://github.com/chriskempson/base16)
|
||||||
|
*/
|
||||||
|
|
||||||
/* Atelier-Dune Comment */
|
/* Comment */
|
||||||
.hljs-comment,
|
.hljs-comment,
|
||||||
.hljs-quote {
|
.hljs-quote {
|
||||||
color: #AAA;
|
color: #575757;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Atelier-Dune Red */
|
/* Red */
|
||||||
.hljs-variable,
|
.hljs-variable,
|
||||||
.hljs-template-variable,
|
.hljs-template-variable,
|
||||||
.hljs-attribute,
|
.hljs-attribute,
|
||||||
|
@ -19,10 +23,10 @@
|
||||||
.hljs-name,
|
.hljs-name,
|
||||||
.hljs-selector-id,
|
.hljs-selector-id,
|
||||||
.hljs-selector-class {
|
.hljs-selector-class {
|
||||||
color: #d73737;
|
color: #d70025;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Atelier-Dune Orange */
|
/* Orange */
|
||||||
.hljs-number,
|
.hljs-number,
|
||||||
.hljs-meta,
|
.hljs-meta,
|
||||||
.hljs-built_in,
|
.hljs-built_in,
|
||||||
|
@ -30,33 +34,33 @@
|
||||||
.hljs-literal,
|
.hljs-literal,
|
||||||
.hljs-type,
|
.hljs-type,
|
||||||
.hljs-params {
|
.hljs-params {
|
||||||
color: #b65611;
|
color: #b21e00;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Atelier-Dune Green */
|
/* Green */
|
||||||
.hljs-string,
|
.hljs-string,
|
||||||
.hljs-symbol,
|
.hljs-symbol,
|
||||||
.hljs-bullet {
|
.hljs-bullet {
|
||||||
color: #60ac39;
|
color: #008200;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Atelier-Dune Blue */
|
/* Blue */
|
||||||
.hljs-title,
|
.hljs-title,
|
||||||
.hljs-section {
|
.hljs-section {
|
||||||
color: #6684e1;
|
color: #0030f2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Atelier-Dune Purple */
|
/* Purple */
|
||||||
.hljs-keyword,
|
.hljs-keyword,
|
||||||
.hljs-selector-tag {
|
.hljs-selector-tag {
|
||||||
color: #b854d4;
|
color: #9d00ec;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hljs {
|
.hljs {
|
||||||
display: block;
|
display: block;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
background: #f1f1f1;
|
background: #f6f7f6;
|
||||||
color: #6e6b5e;
|
color: #000;
|
||||||
padding: 0.5em;
|
padding: 0.5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,3 +71,13 @@
|
||||||
.hljs-strong {
|
.hljs-strong {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hljs-addition {
|
||||||
|
color: #22863a;
|
||||||
|
background-color: #f0fff4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-deletion {
|
||||||
|
color: #b31d28;
|
||||||
|
background-color: #ffeef0;
|
||||||
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,14 +1,20 @@
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
<html lang="en" class="sidebar-visible no-js light">
|
||||||
<head>
|
<head>
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Introduction - Juniper - GraphQL Server for Rust</title>
|
<title>Introduction - Juniper - GraphQL Server for Rust</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
<link rel="icon" href="favicon.svg">
|
||||||
<link rel="shortcut icon" href="favicon.png">
|
<link rel="shortcut icon" href="favicon.png">
|
||||||
<link rel="stylesheet" href="css/variables.css">
|
<link rel="stylesheet" href="css/variables.css">
|
||||||
<link rel="stylesheet" href="css/general.css">
|
<link rel="stylesheet" href="css/general.css">
|
||||||
|
@ -17,8 +23,7 @@
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
<link rel="stylesheet" href="fonts/fonts.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="highlight.css">
|
<link rel="stylesheet" href="highlight.css">
|
||||||
|
@ -27,12 +32,13 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="light">
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
<script type="text/javascript">var path_to_root = "";</script>
|
<script type="text/javascript">
|
||||||
|
var path_to_root = "";
|
||||||
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -54,9 +60,12 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var theme;
|
var theme;
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||||
document.body.className = theme;
|
var html = document.querySelector('html');
|
||||||
document.querySelector('html').className = theme + ' js';
|
html.classList.remove('no-js')
|
||||||
|
html.classList.remove('light')
|
||||||
|
html.classList.add(theme);
|
||||||
|
html.classList.add('js');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
<!-- Hide / unhide sidebar before it is displayed -->
|
||||||
|
@ -72,15 +81,17 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<ol class="chapter"><li class="affix"><a href="index.html">Introduction</a></li><li class="affix"><a href="quickstart.html">Quickstart</a></li><li class="affix"><a href="types/index.html">Type System</a></li><li><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
<div class="sidebar-scrollbox">
|
||||||
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html" class="active">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
<div id="menu-bar-hover-placeholder"></div>
|
||||||
<div id="menu-bar-sticky-container">
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
||||||
<div class="left-buttons">
|
<div class="left-buttons">
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||||
<i class="fa fa-bars"></i>
|
<i class="fa fa-bars"></i>
|
||||||
|
@ -89,17 +100,15 @@
|
||||||
<i class="fa fa-paint-brush"></i>
|
<i class="fa fa-paint-brush"></i>
|
||||||
</button>
|
</button>
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
@ -108,14 +117,13 @@
|
||||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -124,7 +132,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -136,7 +143,7 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<a class="header" href="#juniper" id="juniper"><h1>Juniper</h1></a>
|
<h1 id="juniper"><a class="header" href="#juniper">Juniper</a></h1>
|
||||||
<p>Juniper is a <a href="http://graphql.org">GraphQL</a> server library for Rust. Build type-safe and fast API
|
<p>Juniper is a <a href="http://graphql.org">GraphQL</a> server library for Rust. Build type-safe and fast API
|
||||||
servers with minimal boilerplate and configuration.</p>
|
servers with minimal boilerplate and configuration.</p>
|
||||||
<p><a href="http://graphql.org">GraphQL</a> is a data query language developed by Facebook intended to
|
<p><a href="http://graphql.org">GraphQL</a> is a data query language developed by Facebook intended to
|
||||||
|
@ -152,7 +159,7 @@ embedded <a href="https://github.com/graphql/graphiql">Graphiql</a> for easy deb
|
||||||
<li><a href="https://crates.io/crates/juniper">Cargo crate</a></li>
|
<li><a href="https://crates.io/crates/juniper">Cargo crate</a></li>
|
||||||
<li><a href="https://docs.rs/juniper">API Reference</a></li>
|
<li><a href="https://docs.rs/juniper">API Reference</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<a class="header" href="#features" id="features"><h2>Features</h2></a>
|
<h2 id="features"><a class="header" href="#features">Features</a></h2>
|
||||||
<p>Juniper supports the full GraphQL query language according to the
|
<p>Juniper supports the full GraphQL query language according to the
|
||||||
<a href="http://facebook.github.io/graphql">specification</a>, including interfaces, unions, schema
|
<a href="http://facebook.github.io/graphql">specification</a>, including interfaces, unions, schema
|
||||||
introspection, and validations.
|
introspection, and validations.
|
||||||
|
@ -161,8 +168,8 @@ It does not, however, support the schema language.</p>
|
||||||
non-null types by default. A field of type <code>Vec<Episode></code> will be converted into
|
non-null types by default. A field of type <code>Vec<Episode></code> will be converted into
|
||||||
<code>[Episode!]!</code>. The corresponding Rust type for e.g. <code>[Episode]</code> would be
|
<code>[Episode!]!</code>. The corresponding Rust type for e.g. <code>[Episode]</code> would be
|
||||||
<code>Option<Vec<Option<Episode>>></code>.</p>
|
<code>Option<Vec<Option<Episode>>></code>.</p>
|
||||||
<a class="header" href="#integrations" id="integrations"><h2>Integrations</h2></a>
|
<h2 id="integrations"><a class="header" href="#integrations">Integrations</a></h2>
|
||||||
<a class="header" href="#data-types" id="data-types"><h3>Data types</h3></a>
|
<h3 id="data-types"><a class="header" href="#data-types">Data types</a></h3>
|
||||||
<p>Juniper has automatic integration with some very common Rust crates to make
|
<p>Juniper has automatic integration with some very common Rust crates to make
|
||||||
building schemas a breeze. The types from these crates will be usable in
|
building schemas a breeze. The types from these crates will be usable in
|
||||||
your Schemas automatically.</p>
|
your Schemas automatically.</p>
|
||||||
|
@ -172,14 +179,14 @@ your Schemas automatically.</p>
|
||||||
<li><a href="https://crates.io/crates/chrono">chrono</a></li>
|
<li><a href="https://crates.io/crates/chrono">chrono</a></li>
|
||||||
<li><a href="https://crates.io/crates/bson">bson</a></li>
|
<li><a href="https://crates.io/crates/bson">bson</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<a class="header" href="#web-frameworks" id="web-frameworks"><h3>Web Frameworks</h3></a>
|
<h3 id="web-frameworks"><a class="header" href="#web-frameworks">Web Frameworks</a></h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="https://hyper.rs">hyper</a></li>
|
<li><a href="https://hyper.rs">hyper</a></li>
|
||||||
<li><a href="https://rocket.rs">rocket</a></li>
|
<li><a href="https://rocket.rs">rocket</a></li>
|
||||||
<li><a href="https://github.com/iron/iron">iron</a></li>
|
<li><a href="https://github.com/iron/iron">iron</a></li>
|
||||||
<li><a href="https://github.com/seanmonstar/warp">warp</a></li>
|
<li><a href="https://github.com/seanmonstar/warp">warp</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<a class="header" href="#api-stability" id="api-stability"><h2>API Stability</h2></a>
|
<h2 id="api-stability"><a class="header" href="#api-stability">API Stability</a></h2>
|
||||||
<p>Juniper has not reached 1.0 yet, thus some API instability should be expected.</p>
|
<p>Juniper has not reached 1.0 yet, thus some API instability should be expected.</p>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
@ -187,8 +194,9 @@ your Schemas automatically.</p>
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
|
<a rel="next" href="quickstart.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
<i class="fa fa-angle-right"></i>
|
||||||
|
</a>
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
@ -197,8 +205,9 @@ your Schemas automatically.</p>
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
|
||||||
|
<a rel="next" href="quickstart.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
<i class="fa fa-angle-right"></i>
|
||||||
|
</a>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -206,15 +215,15 @@ your Schemas automatically.</p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.playground_copyable = true;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -222,7 +231,5 @@ your Schemas automatically.</p>
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
1981
master/print.html
1981
master/print.html
File diff suppressed because it is too large
Load diff
|
@ -1,14 +1,20 @@
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
<html lang="en" class="sidebar-visible no-js light">
|
||||||
<head>
|
<head>
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Quickstart - Juniper - GraphQL Server for Rust</title>
|
<title>Quickstart - Juniper - GraphQL Server for Rust</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
<link rel="icon" href="favicon.svg">
|
||||||
<link rel="shortcut icon" href="favicon.png">
|
<link rel="shortcut icon" href="favicon.png">
|
||||||
<link rel="stylesheet" href="css/variables.css">
|
<link rel="stylesheet" href="css/variables.css">
|
||||||
<link rel="stylesheet" href="css/general.css">
|
<link rel="stylesheet" href="css/general.css">
|
||||||
|
@ -17,8 +23,7 @@
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
<link rel="stylesheet" href="fonts/fonts.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="highlight.css">
|
<link rel="stylesheet" href="highlight.css">
|
||||||
|
@ -27,12 +32,13 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="light">
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
<script type="text/javascript">var path_to_root = "";</script>
|
<script type="text/javascript">
|
||||||
|
var path_to_root = "";
|
||||||
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -54,9 +60,12 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var theme;
|
var theme;
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||||
document.body.className = theme;
|
var html = document.querySelector('html');
|
||||||
document.querySelector('html').className = theme + ' js';
|
html.classList.remove('no-js')
|
||||||
|
html.classList.remove('light')
|
||||||
|
html.classList.add(theme);
|
||||||
|
html.classList.add('js');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
<!-- Hide / unhide sidebar before it is displayed -->
|
||||||
|
@ -72,15 +81,17 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<ol class="chapter"><li class="affix"><a href="index.html">Introduction</a></li><li class="affix"><a href="quickstart.html" class="active">Quickstart</a></li><li class="affix"><a href="types/index.html">Type System</a></li><li><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
<div class="sidebar-scrollbox">
|
||||||
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="quickstart.html" class="active">Quickstart</a></li><li class="chapter-item expanded affix "><a href="types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
<div id="menu-bar-hover-placeholder"></div>
|
||||||
<div id="menu-bar-sticky-container">
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
||||||
<div class="left-buttons">
|
<div class="left-buttons">
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||||
<i class="fa fa-bars"></i>
|
<i class="fa fa-bars"></i>
|
||||||
|
@ -89,17 +100,15 @@
|
||||||
<i class="fa fa-paint-brush"></i>
|
<i class="fa fa-paint-brush"></i>
|
||||||
</button>
|
</button>
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
@ -108,14 +117,13 @@
|
||||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -124,7 +132,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -136,15 +143,14 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<a class="header" href="#quickstart" id="quickstart"><h1>Quickstart</h1></a>
|
<h1 id="quickstart"><a class="header" href="#quickstart">Quickstart</a></h1>
|
||||||
<p>This page will give you a short introduction to the concepts in Juniper.</p>
|
<p>This page will give you a short introduction to the concepts in Juniper.</p>
|
||||||
<p>Juniper follows a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">code-first approach</a> to defining GraphQL schemas. If you would like to use a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">schema-first approach</a> instead, consider <a href="https://github.com/davidpdrsn/juniper-from-schema">juniper-from-schema</a> for generating code from a schema file.</p>
|
<p>Juniper follows a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">code-first approach</a> to defining GraphQL schemas. If you would like to use a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">schema-first approach</a> instead, consider <a href="https://github.com/davidpdrsn/juniper-from-schema">juniper-from-schema</a> for generating code from a schema file.</p>
|
||||||
<a class="header" href="#installation" id="installation"><h2>Installation</h2></a>
|
<h2 id="installation"><a class="header" href="#installation">Installation</a></h2>
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
<pre><code class="language-toml">[dependencies]
|
||||||
juniper = { git = "https://github.com/graphql-rust/juniper" }
|
juniper = "0.15"
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<a class="header" href="#schema-example" id="schema-example"><h2>Schema example</h2></a>
|
<h2 id="schema-example"><a class="header" href="#schema-example">Schema example</a></h2>
|
||||||
<p>Exposing simple enums and structs as GraphQL is just a matter of adding a custom
|
<p>Exposing simple enums and structs as GraphQL is just a matter of adding a custom
|
||||||
derive attribute to them. Juniper includes support for basic Rust types that
|
derive attribute to them. Juniper includes support for basic Rust types that
|
||||||
naturally map to GraphQL features, such as <code>Option<T></code>, <code>Vec<T></code>, <code>Box<T></code>,
|
naturally map to GraphQL features, such as <code>Option<T></code>, <code>Vec<T></code>, <code>Box<T></code>,
|
||||||
|
@ -153,21 +159,21 @@ naturally map to GraphQL features, such as <code>Option<T></code>, <code>V
|
||||||
types to a GraphQL schema. The most important one is the
|
types to a GraphQL schema. The most important one is the
|
||||||
<a href="https://docs.rs/juniper/latest/juniper/macro.graphql_object.html">graphql_object</a> procedural macro that is used for declaring an object with
|
<a href="https://docs.rs/juniper/latest/juniper/macro.graphql_object.html">graphql_object</a> procedural macro that is used for declaring an object with
|
||||||
resolvers, which you will use for the <code>Query</code> and <code>Mutation</code> roots.</p>
|
resolvers, which you will use for the <code>Query</code> and <code>Mutation</code> roots.</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
||||||
# extern crate juniper;
|
</span><span class="boring">extern crate juniper;
|
||||||
# use std::fmt::Display;
|
</span><span class="boring">use std::fmt::Display;
|
||||||
use juniper::{
|
</span>use juniper::{
|
||||||
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
|
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
|
||||||
GraphQLInputObject, GraphQLObject, ScalarValue,
|
GraphQLInputObject, GraphQLObject, ScalarValue,
|
||||||
};
|
};
|
||||||
#
|
<span class="boring">
|
||||||
# struct DatabasePool;
|
</span><span class="boring">struct DatabasePool;
|
||||||
# impl DatabasePool {
|
</span><span class="boring">impl DatabasePool {
|
||||||
# fn get_connection(&self) -> FieldResult<DatabasePool> { Ok(DatabasePool) }
|
</span><span class="boring"> fn get_connection(&self) -> FieldResult<DatabasePool> { Ok(DatabasePool) }
|
||||||
# fn find_human(&self, _id: &str) -> FieldResult<Human> { Err("")? }
|
</span><span class="boring"> fn find_human(&self, _id: &str) -> FieldResult<Human> { Err("")? }
|
||||||
# fn insert_human(&self, _human: &NewHuman) -> FieldResult<Human> { Err("")? }
|
</span><span class="boring"> fn insert_human(&self, _human: &NewHuman) -> FieldResult<Human> { Err("")? }
|
||||||
# }
|
</span><span class="boring">}
|
||||||
|
</span>
|
||||||
#[derive(GraphQLEnum)]
|
#[derive(GraphQLEnum)]
|
||||||
enum Episode {
|
enum Episode {
|
||||||
NewHope,
|
NewHope,
|
||||||
|
@ -216,7 +222,7 @@ struct Query;
|
||||||
context = Context,
|
context = Context,
|
||||||
)]
|
)]
|
||||||
impl Query {
|
impl Query {
|
||||||
fn apiVersion() -> &str {
|
fn apiVersion() -> &'static str {
|
||||||
"1.0"
|
"1.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,14 +247,13 @@ struct Mutation;
|
||||||
|
|
||||||
#[graphql_object(
|
#[graphql_object(
|
||||||
context = Context,
|
context = Context,
|
||||||
|
|
||||||
// If we need to use `ScalarValue` parametrization explicitly somewhere
|
// If we need to use `ScalarValue` parametrization explicitly somewhere
|
||||||
// in the object definition (like here in `FieldResult`), we should
|
// in the object definition (like here in `FieldResult`), we could
|
||||||
// declare an explicit type parameter for that, and specify it.
|
// declare an explicit type parameter for that, and specify it.
|
||||||
scalar = S,
|
scalar = S: ScalarValue + Display,
|
||||||
)]
|
)]
|
||||||
impl<S: ScalarValue + Display> Mutation {
|
impl Mutation {
|
||||||
fn createHuman(context: &Context, new_human: NewHuman) -> FieldResult<Human, S> {
|
fn createHuman<S: ScalarValue + Display>(context: &Context, new_human: NewHuman) -> FieldResult<Human, S> {
|
||||||
let db = context.pool.get_connection().map_err(|e| e.map_scalar_value())?;
|
let db = context.pool.get_connection().map_err(|e| e.map_scalar_value())?;
|
||||||
let human: Human = db.insert_human(&new_human).map_err(|e| e.map_scalar_value())?;
|
let human: Human = db.insert_human(&new_human).map_err(|e| e.map_scalar_value())?;
|
||||||
Ok(human)
|
Ok(human)
|
||||||
|
@ -258,19 +263,19 @@ impl<S: ScalarValue + Display> Mutation {
|
||||||
// A root schema consists of a query, a mutation, and a subscription.
|
// A root schema consists of a query, a mutation, and a subscription.
|
||||||
// Request queries can be executed against a RootNode.
|
// Request queries can be executed against a RootNode.
|
||||||
type Schema = juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;
|
type Schema = juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;
|
||||||
#
|
<span class="boring">
|
||||||
# fn main() {
|
</span><span class="boring">fn main() {
|
||||||
# let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
|
</span><span class="boring"> let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
|
||||||
# }
|
</span><span class="boring">}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
<p>We now have a very simple but functional schema for a GraphQL server!</p>
|
<p>We now have a very simple but functional schema for a GraphQL server!</p>
|
||||||
<p>To actually serve the schema, see the guides for our various <a href="./servers/index.html">server integrations</a>.</p>
|
<p>To actually serve the schema, see the guides for our various <a href="./servers/index.html">server integrations</a>.</p>
|
||||||
<p>Juniper is a library that can be used in many contexts--it does not require a server and it does not have a dependency on a particular transport or serialization format. You can invoke the executor directly to get a result for a query:</p>
|
<p>Juniper is a library that can be used in many contexts--it does not require a server and it does not have a dependency on a particular transport or serialization format. You can invoke the executor directly to get a result for a query:</p>
|
||||||
<a class="header" href="#executor" id="executor"><h2>Executor</h2></a>
|
<h2 id="executor"><a class="header" href="#executor">Executor</a></h2>
|
||||||
<p>You can invoke <code>juniper::execute</code> directly to run a GraphQL query:</p>
|
<p>You can invoke <code>juniper::execute</code> directly to run a GraphQL query:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># // Only needed due to 2018 edition because the macro is not accessible.
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">// Only needed due to 2018 edition because the macro is not accessible.
|
||||||
# #[macro_use] extern crate juniper;
|
</span><span class="boring">#[macro_use] extern crate juniper;
|
||||||
use juniper::{
|
</span>use juniper::{
|
||||||
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
|
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
|
||||||
GraphQLEnum, Variables, graphql_value,
|
GraphQLEnum, Variables, graphql_value,
|
||||||
};
|
};
|
||||||
|
@ -327,35 +332,27 @@ fn main() {
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
<a rel="prev" href="index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
<a rel="prev" href="index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="types/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
<a rel="next" href="types/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
<a rel="prev" href="index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<a href="index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a rel="next" href="types/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
|
||||||
<a href="types/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -363,15 +360,15 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.playground_copyable = true;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -379,7 +376,5 @@ fn main() {
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,292 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Schemas and mutations - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html" class="active"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#schemas" id="schemas"><h1>Schemas</h1></a>
|
|
||||||
<p>Juniper follows a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">code-first approach</a> to defining GraphQL schemas. If you would like to use a <a href="https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/">schema-first approach</a> instead, consider <a href="https://github.com/davidpdrsn/juniper-from-schema">juniper-from-schema</a> for generating code from a schema file.</p>
|
|
||||||
<p>A schema consists of three types: a query object, a mutation object, and a subscription object.
|
|
||||||
These three define the root query fields, mutations and subscriptions of the schema, respectively.</p>
|
|
||||||
<p>The usage of subscriptions is a little different from the mutation and query objects, so there is a specific <a href="../advanced/subscriptions.html">section</a> that discusses them.</p>
|
|
||||||
<p>Both query and mutation objects are regular GraphQL objects, defined like any
|
|
||||||
other object in Juniper. The mutation and subscription objects, however, are optional since schemas
|
|
||||||
can be read-only and do not require subscriptions. If mutation/subscription functionality is not needed, consider using <a href="https://docs.rs/juniper/0.14.2/juniper/struct.EmptyMutation.html">EmptyMutation</a>/[EmptySubscription][EmptySubscription].</p>
|
|
||||||
<p>In Juniper, the <code>RootNode</code> type represents a schema. When the schema is first created,
|
|
||||||
Juniper will traverse the entire object graph
|
|
||||||
and register all types it can find. This means that if you define a GraphQL
|
|
||||||
object somewhere but never reference it, it will not be exposed in a schema.</p>
|
|
||||||
<a class="header" href="#the-query-root" id="the-query-root"><h2>The query root</h2></a>
|
|
||||||
<p>The query root is just a GraphQL object. You define it like any other GraphQL
|
|
||||||
object in Juniper, most commonly using the <code>graphql_object</code> proc macro:</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
|
||||||
# extern crate juniper;
|
|
||||||
# use juniper::{graphql_object, FieldResult, GraphQLObject};
|
|
||||||
# #[derive(GraphQLObject)] struct User { name: String }
|
|
||||||
struct Root;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Root {
|
|
||||||
fn userWithUsername(username: String) -> FieldResult<Option<User>> {
|
|
||||||
// Look up user in database...
|
|
||||||
# unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#
|
|
||||||
# fn main() { }
|
|
||||||
</code></pre></pre>
|
|
||||||
<a class="header" href="#mutations" id="mutations"><h2>Mutations</h2></a>
|
|
||||||
<p>Mutations are <em>also</em> just GraphQL objects. Each mutation is a single field
|
|
||||||
that performs some mutating side-effect such as updating a database.</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
|
||||||
# extern crate juniper;
|
|
||||||
# use juniper::{graphql_object, FieldResult, GraphQLObject};
|
|
||||||
# #[derive(GraphQLObject)] struct User { name: String }
|
|
||||||
struct Mutations;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Mutations {
|
|
||||||
fn signUpUser(name: String, email: String) -> FieldResult<User> {
|
|
||||||
// Validate inputs and save user in database...
|
|
||||||
# unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#
|
|
||||||
# fn main() { }
|
|
||||||
</code></pre></pre>
|
|
||||||
<a class="header" href="#converting-a-rust-schema-to-the-a-hrefhttpsgraphqlorglearnschematype-languagegraphql-schema-languagea" id="converting-a-rust-schema-to-the-a-hrefhttpsgraphqlorglearnschematype-languagegraphql-schema-languagea"><h1>Converting a Rust schema to the <a href="https://graphql.org/learn/schema/#type-language">GraphQL Schema Language</a></h1></a>
|
|
||||||
<p>Many tools in the GraphQL ecosystem require the schema to be defined in the <a href="https://graphql.org/learn/schema/#type-language">GraphQL Schema Language</a>. You can generate a <a href="https://graphql.org/learn/schema/#type-language">GraphQL Schema Language</a> representation of your schema defined in Rust using the <code>schema-language</code> feature (on by default):</p>
|
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
|
||||||
use juniper::{
|
|
||||||
graphql_object, EmptyMutation, EmptySubscription, FieldResult, RootNode,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Query;
|
|
||||||
|
|
||||||
#[graphql_object]
|
|
||||||
impl Query {
|
|
||||||
fn hello(&self) -> FieldResult<&str> {
|
|
||||||
Ok("hello world")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// Define our schema in Rust.
|
|
||||||
let schema = RootNode::new(
|
|
||||||
Query,
|
|
||||||
EmptyMutation::<()>::new(),
|
|
||||||
EmptySubscription::<()>::new(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Convert the Rust schema into the GraphQL Schema Language.
|
|
||||||
let result = schema.as_schema_language();
|
|
||||||
|
|
||||||
let expected = "\
|
|
||||||
type Query {
|
|
||||||
hello: String!
|
|
||||||
}
|
|
||||||
|
|
||||||
schema {
|
|
||||||
query: Query
|
|
||||||
}
|
|
||||||
";
|
|
||||||
assert_eq!(result, expected);
|
|
||||||
}
|
|
||||||
</code></pre></pre>
|
|
||||||
<p>Note the <code>schema-language</code> feature may be turned off if you do not need this functionality to reduce dependencies and speed up
|
|
||||||
compile times.</p>
|
|
||||||
<!--TODO: Fix This URL when the EmptySubscription become available in the Documentation -->
|
|
||||||
[EmptySubscription]: https://docs.rs/juniper/0.14.2/juniper/struct.EmptySubscription.html
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../types/unions.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../types/unions.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -145,6 +145,11 @@ window.search = window.search || {};
|
||||||
url.push("");
|
url.push("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// encodeURIComponent escapes all chars that could allow an XSS except
|
||||||
|
// for '. Due to that we also manually replace ' with its url-encoded
|
||||||
|
// representation (%27).
|
||||||
|
var searchterms = encodeURIComponent(searchterms.join(" ")).replace(/\'/g, "%27");
|
||||||
|
|
||||||
return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]
|
return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]
|
||||||
+ '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'
|
+ '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'
|
||||||
+ '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">'
|
+ '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">'
|
||||||
|
@ -291,7 +296,7 @@ window.search = window.search || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.params.hasOwnProperty(URL_MARK_PARAM)) {
|
if (url.params.hasOwnProperty(URL_MARK_PARAM)) {
|
||||||
var words = url.params[URL_MARK_PARAM].split(' ');
|
var words = decodeURIComponent(url.params[URL_MARK_PARAM]).split(' ');
|
||||||
marker.mark(words, {
|
marker.mark(words, {
|
||||||
exclude: mark_exclude
|
exclude: mark_exclude
|
||||||
});
|
});
|
||||||
|
@ -311,7 +316,7 @@ window.search = window.search || {};
|
||||||
|
|
||||||
// Eventhandler for keyevents on `document`
|
// Eventhandler for keyevents on `document`
|
||||||
function globalKeyHandler(e) {
|
function globalKeyHandler(e) {
|
||||||
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea') { return; }
|
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text') { return; }
|
||||||
|
|
||||||
if (e.keyCode === ESCAPE_KEYCODE) {
|
if (e.keyCode === ESCAPE_KEYCODE) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
@ -422,6 +427,7 @@ window.search = window.search || {};
|
||||||
delete url.params[URL_MARK_PARAM];
|
delete url.params[URL_MARK_PARAM];
|
||||||
url.hash = "";
|
url.hash = "";
|
||||||
} else {
|
} else {
|
||||||
|
delete url.params[URL_MARK_PARAM];
|
||||||
delete url.params[URL_SEARCH_PARAM];
|
delete url.params[URL_SEARCH_PARAM];
|
||||||
}
|
}
|
||||||
// A new search will also add a new history item, so the user can go back
|
// A new search will also add a new history item, so the user can go back
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,215 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Hyper - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html" class="active"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#integrating-with-hyper" id="integrating-with-hyper"><h1>Integrating with Hyper</h1></a>
|
|
||||||
<p><a href="https://hyper.rs/">Hyper</a> is a fast HTTP implementation that many other Rust web frameworks
|
|
||||||
leverage. It offers asynchronous I/O via the tokio runtime and works on
|
|
||||||
Rust's stable channel.</p>
|
|
||||||
<p>Hyper is not a higher-level web framework and accordingly
|
|
||||||
does not include ergonomic features such as simple endpoint routing,
|
|
||||||
baked-in HTTP responses, or reusable middleware. For GraphQL, those aren't
|
|
||||||
large downsides as all POSTs and GETs usually go through a single endpoint with
|
|
||||||
a few clearly-defined response payloads.</p>
|
|
||||||
<p>Juniper's Hyper integration is contained in the <a href="https://github.com/graphql-rust/juniper/tree/master/juniper_hyper"><code>juniper_hyper</code></a> crate:</p>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = "0.10"
|
|
||||||
juniper_hyper = "0.1.0"
|
|
||||||
</code></pre>
|
|
||||||
<p>Included in the source is a <a href="https://github.com/graphql-rust/juniper/blob/master/juniper_hyper/examples/hyper_server.rs">small example</a> which sets up a basic GraphQL and <a href="https://github.com/graphql/graphiql">GraphiQL</a> handler.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/iron.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/third-party.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/iron.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/third-party.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,221 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Adding A Server - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html" class="active"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#adding-a-server" id="adding-a-server"><h1>Adding A Server</h1></a>
|
|
||||||
<p>To allow using Juniper with the HTTP server of your choice,
|
|
||||||
it does <strong>not</strong> come with a built in HTTP server.</p>
|
|
||||||
<p>To actually get a server up and running, there are multiple official and
|
|
||||||
third-party integration crates that will get you there.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="official.html">Official Server Integrations</a>
|
|
||||||
<ul>
|
|
||||||
<li><a href="warp.html">Warp</a></li>
|
|
||||||
<li><a href="rocket.html">Rocket</a></li>
|
|
||||||
<li><a href="iron.html">Iron</a></li>
|
|
||||||
<li><a href="hyper.html">Hyper</a></li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
<li><a href="third-party.html">Third Party Integrations</a>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://github.com/actix/examples/tree/master/juniper">Actix-Web</a></li>
|
|
||||||
<li><a href="https://github.com/finchers-rs/finchers-juniper">Finchers</a></li>
|
|
||||||
<li><a href="https://github.com/tsukuyomi-rs/tsukuyomi/tree/master/examples/juniper">Tsukuyomi</a></li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../schema/schemas_and_mutations.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/official.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../schema/schemas_and_mutations.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/official.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,301 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Iron - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html" class="active"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#integrating-with-iron" id="integrating-with-iron"><h1>Integrating with Iron</h1></a>
|
|
||||||
<p><a href="https://github.com/iron/iron">Iron</a> is a library that's been around for a while in the Rust sphere but lately
|
|
||||||
hasn't seen much of development. Nevertheless, it's still a solid library with a
|
|
||||||
familiar request/response/middleware architecture that works on Rust's stable
|
|
||||||
channel.</p>
|
|
||||||
<p>Juniper's Iron integration is contained in the <code>juniper_iron</code> crate:</p>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = "0.10"
|
|
||||||
juniper_iron = "0.2.0"
|
|
||||||
</code></pre>
|
|
||||||
<p>Included in the source is a <a href="https://github.com/graphql-rust/juniper_iron/blob/master/examples/iron_server.rs">small
|
|
||||||
example</a>
|
|
||||||
which sets up a basic GraphQL and <a href="https://github.com/graphql/graphiql">GraphiQL</a> handler.</p>
|
|
||||||
<a class="header" href="#basic-integration" id="basic-integration"><h2>Basic integration</h2></a>
|
|
||||||
<p>Let's start with a minimal schema and just get a GraphQL endpoint up and
|
|
||||||
running. We use <a href="https://github.com/iron/mount">mount</a> to attach the GraphQL handler at <code>/graphql</code>.</p>
|
|
||||||
<p>The <code>context_factory</code> function will be executed on every request and can be used
|
|
||||||
to set up database connections, read session token information from cookies, and
|
|
||||||
set up other global data that the schema might require.</p>
|
|
||||||
<p>In this example, we won't use any global data so we just return an empty value.</p>
|
|
||||||
<pre><code class="language-rust ignore">extern crate juniper;
|
|
||||||
extern crate juniper_iron;
|
|
||||||
extern crate iron;
|
|
||||||
extern crate mount;
|
|
||||||
|
|
||||||
use mount::Mount;
|
|
||||||
use iron::prelude::*;
|
|
||||||
use juniper::EmptyMutation;
|
|
||||||
use juniper_iron::GraphQLHandler;
|
|
||||||
|
|
||||||
fn context_factory(_: &mut Request) -> IronResult<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Root;
|
|
||||||
|
|
||||||
#[juniper::graphql_object]
|
|
||||||
impl Root {
|
|
||||||
fn foo() -> String {
|
|
||||||
"Bar".to_owned()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# #[allow(unreachable_code, unused_variables)]
|
|
||||||
fn main() {
|
|
||||||
let mut mount = Mount::new();
|
|
||||||
|
|
||||||
let graphql_endpoint = GraphQLHandler::new(
|
|
||||||
context_factory,
|
|
||||||
Root,
|
|
||||||
EmptyMutation::<()>::new(),
|
|
||||||
);
|
|
||||||
|
|
||||||
mount.mount("/graphql", graphql_endpoint);
|
|
||||||
|
|
||||||
let chain = Chain::new(mount);
|
|
||||||
|
|
||||||
# return;
|
|
||||||
Iron::new(chain).http("0.0.0.0:8080").unwrap();
|
|
||||||
}
|
|
||||||
</code></pre>
|
|
||||||
<a class="header" href="#accessing-data-from-the-request" id="accessing-data-from-the-request"><h2>Accessing data from the request</h2></a>
|
|
||||||
<p>If you want to access e.g. the source IP address of the request from a field
|
|
||||||
resolver, you need to pass this data using Juniper's <a href="../types/objects/using_contexts.html">context feature</a>.</p>
|
|
||||||
<pre><code class="language-rust ignore"># extern crate juniper;
|
|
||||||
# extern crate juniper_iron;
|
|
||||||
# extern crate iron;
|
|
||||||
# use iron::prelude::*;
|
|
||||||
use std::net::SocketAddr;
|
|
||||||
|
|
||||||
struct Context {
|
|
||||||
remote_addr: SocketAddr,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl juniper::Context for Context {}
|
|
||||||
|
|
||||||
fn context_factory(req: &mut Request) -> IronResult<Context> {
|
|
||||||
Ok(Context {
|
|
||||||
remote_addr: req.remote_addr
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Root;
|
|
||||||
|
|
||||||
#[juniper::graphql_object(
|
|
||||||
Context = Context,
|
|
||||||
)]
|
|
||||||
impl Root {
|
|
||||||
field my_addr(context: &Context) -> String {
|
|
||||||
format!("Hello, you're coming from {}", context.remote_addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# fn main() {
|
|
||||||
# let _graphql_endpoint = juniper_iron::GraphQLHandler::new(
|
|
||||||
# context_factory,
|
|
||||||
# Root,
|
|
||||||
# juniper::EmptyMutation::<Context>::new(),
|
|
||||||
# );
|
|
||||||
# }
|
|
||||||
</code></pre>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/rocket.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/hyper.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/rocket.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/hyper.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,208 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Official Server Integrations - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html" class="active"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#official-server-integrations" id="official-server-integrations"><h1>Official Server Integrations</h1></a>
|
|
||||||
<p>Juniper provides official integration crates for several popular Rust server
|
|
||||||
libraries.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="warp.html">Warp</a></li>
|
|
||||||
<li><a href="rocket.html">Rocket</a></li>
|
|
||||||
<li><a href="iron.html">Iron</a></li>
|
|
||||||
<li><a href="hyper.html">Hyper</a></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/warp.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/warp.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,210 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Rocket - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html" class="active"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#integrating-with-rocket" id="integrating-with-rocket"><h1>Integrating with Rocket</h1></a>
|
|
||||||
<p><a href="https://rocket.rs/">Rocket</a> is a web framework for Rust that makes it simple to write fast web applications without sacrificing flexibility or type safety. All with minimal code. Rocket
|
|
||||||
does not work on Rust's stable channel and instead requires the nightly
|
|
||||||
channel.</p>
|
|
||||||
<p>Juniper's Rocket integration is contained in the <a href="https://github.com/graphql-rust/juniper/tree/master/juniper_rocket"><code>juniper_rocket</code></a> crate:</p>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = "0.10"
|
|
||||||
juniper_rocket = "0.2.0"
|
|
||||||
</code></pre>
|
|
||||||
<p>Included in the source is a <a href="https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/rocket_server.rs">small example</a> which sets up a basic GraphQL and <a href="https://github.com/graphql/graphiql">GraphiQL</a> handler.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/warp.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/iron.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/warp.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/iron.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,204 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Third Party Integrations - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html" class="active"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#other-examples" id="other-examples"><h1>Other Examples</h1></a>
|
|
||||||
<p>These examples are not officially maintained by Juniper developers.</p>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://github.com/actix/examples/tree/HEAD/graphql/juniper">Actix Web</a> | <a href="https://github.com/actix/examples/tree/HEAD/graphql/juniper-advanced">Actix Web (advanced)</a></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/hyper.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../advanced/index.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/hyper.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../advanced/index.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,210 +0,0 @@
|
||||||
<!DOCTYPE HTML>
|
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
|
||||||
<head>
|
|
||||||
<!-- Book generated using mdBook -->
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Warp - Juniper - GraphQL Server for Rust</title>
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<meta name="theme-color" content="#ffffff" />
|
|
||||||
|
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
|
||||||
<link rel="stylesheet" href="../css/chrome.css">
|
|
||||||
<link rel="stylesheet" href="../css/print.css" media="print">
|
|
||||||
|
|
||||||
<!-- Fonts -->
|
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
|
||||||
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
||||||
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body class="light">
|
|
||||||
<!-- Provide site root to javascript -->
|
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
try {
|
|
||||||
var theme = localStorage.getItem('mdbook-theme');
|
|
||||||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
||||||
|
|
||||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
||||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
||||||
}
|
|
||||||
} catch (e) { }
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var theme;
|
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
|
||||||
document.body.className = theme;
|
|
||||||
document.querySelector('html').className = theme + ' js';
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
var html = document.querySelector('html');
|
|
||||||
var sidebar = 'hidden';
|
|
||||||
if (document.body.clientWidth >= 1080) {
|
|
||||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
||||||
sidebar = sidebar || 'visible';
|
|
||||||
}
|
|
||||||
html.classList.remove('sidebar-visible');
|
|
||||||
html.classList.add("sidebar-" + sidebar);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html" class="active"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
|
||||||
|
|
||||||
<div class="page">
|
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
|
||||||
<div id="menu-bar-sticky-container">
|
|
||||||
<div class="left-buttons">
|
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
||||||
<i class="fa fa-bars"></i>
|
|
||||||
</button>
|
|
||||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
||||||
<i class="fa fa-paint-brush"></i>
|
|
||||||
</button>
|
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
||||||
<i class="fa fa-search"></i>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
|
||||||
|
|
||||||
<div class="right-buttons">
|
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
||||||
</form>
|
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
|
||||||
<ul id="searchresults">
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
||||||
<script type="text/javascript">
|
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
||||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
||||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
||||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div id="content" class="content">
|
|
||||||
<main>
|
|
||||||
<a class="header" href="#integrating-with-warp" id="integrating-with-warp"><h1>Integrating with Warp</h1></a>
|
|
||||||
<p><a href="https://crates.io/crates/warp">Warp</a> is a super-easy, composable, web server framework for warp speeds.
|
|
||||||
The fundamental building block of warp is the Filter: they can be combined and composed to express rich requirements on requests. Warp is built on <a href="https://hyper.rs/">Hyper</a> and works on
|
|
||||||
Rust's stable channel.</p>
|
|
||||||
<p>Juniper's Warp integration is contained in the <a href="https://github.com/graphql-rust/juniper/tree/master/juniper_warp"><code>juniper_warp</code></a> crate:</p>
|
|
||||||
<p>!FILENAME Cargo.toml</p>
|
|
||||||
<pre><code class="language-toml">[dependencies]
|
|
||||||
juniper = "0.10"
|
|
||||||
juniper_warp = "0.1.0"
|
|
||||||
</code></pre>
|
|
||||||
<p>Included in the source is a <a href="https://github.com/graphql-rust/juniper/blob/master/juniper_warp/examples/warp_server.rs">small example</a> which sets up a basic GraphQL and <a href="https://github.com/graphql/graphiql">GraphiQL</a> handler.</p>
|
|
||||||
|
|
||||||
</main>
|
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
||||||
<!-- Mobile navigation buttons -->
|
|
||||||
|
|
||||||
<a rel="prev" href="../servers/official.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../servers/rocket.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
|
||||||
</nav>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
||||||
|
|
||||||
<a href="../servers/official.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a href="../servers/rocket.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
|
||||||
|
|
||||||
<!-- Custom JS scripts -->
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -94,3 +94,11 @@
|
||||||
.xml .hljs-cdata {
|
.xml .hljs-cdata {
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hljs-addition {
|
||||||
|
color: #718c00;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hljs-deletion {
|
||||||
|
color: #c82829;
|
||||||
|
}
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
<html lang="en" class="sidebar-visible no-js light">
|
||||||
<head>
|
<head>
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Enums - Juniper - GraphQL Server for Rust</title>
|
<title>Enums - Juniper - GraphQL Server for Rust</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
<link rel="icon" href="../favicon.svg">
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
<link rel="shortcut icon" href="../favicon.png">
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
<link rel="stylesheet" href="../css/variables.css">
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
<link rel="stylesheet" href="../css/general.css">
|
||||||
|
@ -17,8 +23,7 @@
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
<link rel="stylesheet" href="../highlight.css">
|
||||||
|
@ -27,12 +32,13 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="light">
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
<script type="text/javascript">
|
||||||
|
var path_to_root = "../";
|
||||||
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -54,9 +60,12 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var theme;
|
var theme;
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||||
document.body.className = theme;
|
var html = document.querySelector('html');
|
||||||
document.querySelector('html').className = theme + ' js';
|
html.classList.remove('no-js')
|
||||||
|
html.classList.remove('light')
|
||||||
|
html.classList.add(theme);
|
||||||
|
html.classList.add('js');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
<!-- Hide / unhide sidebar before it is displayed -->
|
||||||
|
@ -72,15 +81,17 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html" class="active"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
<div class="sidebar-scrollbox">
|
||||||
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html" class="active"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
<div id="menu-bar-hover-placeholder"></div>
|
||||||
<div id="menu-bar-sticky-container">
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
||||||
<div class="left-buttons">
|
<div class="left-buttons">
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||||
<i class="fa fa-bars"></i>
|
<i class="fa fa-bars"></i>
|
||||||
|
@ -89,17 +100,15 @@
|
||||||
<i class="fa fa-paint-brush"></i>
|
<i class="fa fa-paint-brush"></i>
|
||||||
</button>
|
</button>
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
@ -108,14 +117,13 @@
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -124,7 +132,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -136,26 +143,26 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<a class="header" href="#enums" id="enums"><h1>Enums</h1></a>
|
<h1 id="enums"><a class="header" href="#enums">Enums</a></h1>
|
||||||
<p>Enums in GraphQL are string constants grouped together to represent a set of
|
<p>Enums in GraphQL are string constants grouped together to represent a set of
|
||||||
possible values. Simple Rust enums can be converted to GraphQL enums by using a
|
possible values. Simple Rust enums can be converted to GraphQL enums by using a
|
||||||
custom derive attribute:</p>
|
custom derive attribute:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
||||||
#[derive(juniper::GraphQLEnum)]
|
</span>#[derive(juniper::GraphQLEnum)]
|
||||||
enum Episode {
|
enum Episode {
|
||||||
NewHope,
|
NewHope,
|
||||||
Empire,
|
Empire,
|
||||||
Jedi,
|
Jedi,
|
||||||
}
|
}
|
||||||
|
|
||||||
# fn main() {}
|
<span class="boring">fn main() {}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
<p>Juniper converts all enum variants to uppercase, so the corresponding string
|
<p>Juniper converts all enum variants to uppercase, so the corresponding string
|
||||||
values for these variants are <code>NEWHOPE</code>, <code>EMPIRE</code>, and <code>JEDI</code>, respectively. If
|
values for these variants are <code>NEWHOPE</code>, <code>EMPIRE</code>, and <code>JEDI</code>, respectively. If
|
||||||
you want to override this, you can use the <code>graphql</code> attribute, similar to how
|
you want to override this, you can use the <code>graphql</code> attribute, similar to how
|
||||||
it works when <a href="objects/defining_objects.html">defining objects</a>:</p>
|
it works when <a href="objects/defining_objects.html">defining objects</a>:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
||||||
#[derive(juniper::GraphQLEnum)]
|
</span>#[derive(juniper::GraphQLEnum)]
|
||||||
enum Episode {
|
enum Episode {
|
||||||
#[graphql(name="NEW_HOPE")]
|
#[graphql(name="NEW_HOPE")]
|
||||||
NewHope,
|
NewHope,
|
||||||
|
@ -163,13 +170,13 @@ enum Episode {
|
||||||
Jedi,
|
Jedi,
|
||||||
}
|
}
|
||||||
|
|
||||||
# fn main() {}
|
<span class="boring">fn main() {}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
<a class="header" href="#documentation-and-deprecation" id="documentation-and-deprecation"><h2>Documentation and deprecation</h2></a>
|
<h2 id="documentation-and-deprecation"><a class="header" href="#documentation-and-deprecation">Documentation and deprecation</a></h2>
|
||||||
<p>Just like when defining objects, the type itself can be renamed and documented,
|
<p>Just like when defining objects, the type itself can be renamed and documented,
|
||||||
while individual enum variants can be renamed, documented, and deprecated:</p>
|
while individual enum variants can be renamed, documented, and deprecated:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># extern crate juniper;
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">extern crate juniper;
|
||||||
#[derive(juniper::GraphQLEnum)]
|
</span>#[derive(juniper::GraphQLEnum)]
|
||||||
#[graphql(name="Episode", description="An episode of Star Wars")]
|
#[graphql(name="Episode", description="An episode of Star Wars")]
|
||||||
enum StarWarsEpisode {
|
enum StarWarsEpisode {
|
||||||
#[graphql(deprecated="We don't really talk about this one")]
|
#[graphql(deprecated="We don't really talk about this one")]
|
||||||
|
@ -183,54 +190,46 @@ enum StarWarsEpisode {
|
||||||
Jedi,
|
Jedi,
|
||||||
}
|
}
|
||||||
|
|
||||||
# fn main() {}
|
<span class="boring">fn main() {}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
<a class="header" href="#supported-macro-attributes-derive" id="supported-macro-attributes-derive"><h2>Supported Macro Attributes (Derive)</h2></a>
|
<h2 id="supported-macro-attributes-derive"><a class="header" href="#supported-macro-attributes-derive">Supported Macro Attributes (Derive)</a></h2>
|
||||||
<table><thead><tr><th> Name of Attribute </th><th align="center"> Container Support </th><th align="center"> Field Support </th></tr></thead><tbody>
|
<table><thead><tr><th>Name of Attribute</th><th align="center">Container Support</th><th align="center">Field Support</th></tr></thead><tbody>
|
||||||
<tr><td> context </td><td align="center"> ✔ </td><td align="center"> ? </td></tr>
|
<tr><td>context</td><td align="center">✔</td><td align="center">?</td></tr>
|
||||||
<tr><td> deprecated </td><td align="center"> ✔ </td><td align="center"> ✔ </td></tr>
|
<tr><td>deprecated</td><td align="center">✔</td><td align="center">✔</td></tr>
|
||||||
<tr><td> description </td><td align="center"> ✔ </td><td align="center"> ✔ </td></tr>
|
<tr><td>description</td><td align="center">✔</td><td align="center">✔</td></tr>
|
||||||
<tr><td> interfaces </td><td align="center"> ? </td><td align="center"> ✘ </td></tr>
|
<tr><td>interfaces</td><td align="center">?</td><td align="center">✘</td></tr>
|
||||||
<tr><td> name </td><td align="center"> ✔ </td><td align="center"> ✔ </td></tr>
|
<tr><td>name</td><td align="center">✔</td><td align="center">✔</td></tr>
|
||||||
<tr><td> noasync </td><td align="center"> ✔ </td><td align="center"> ? </td></tr>
|
<tr><td>noasync</td><td align="center">✔</td><td align="center">?</td></tr>
|
||||||
<tr><td> scalar </td><td align="center"> ✘ </td><td align="center"> ? </td></tr>
|
<tr><td>scalar</td><td align="center">✘</td><td align="center">?</td></tr>
|
||||||
<tr><td> skip </td><td align="center"> ? </td><td align="center"> ✘ </td></tr>
|
<tr><td>skip</td><td align="center">?</td><td align="center">✘</td></tr>
|
||||||
<tr><td> ✔: supported </td><td align="center"> ✘: not supported </td><td align="center"> ?: not available </td></tr>
|
<tr><td>✔: supported</td><td align="center">✘: not supported</td><td align="center">?: not available</td></tr>
|
||||||
</tbody></table>
|
</tbody></table>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
<a rel="prev" href="../types/other-index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
<a rel="prev" href="../types/other-index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/interfaces.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
<a rel="next" href="../types/interfaces.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
<a rel="prev" href="../types/other-index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<a href="../types/other-index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a rel="next" href="../types/interfaces.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
|
||||||
<a href="../types/interfaces.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -238,15 +237,15 @@ enum StarWarsEpisode {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.playground_copyable = true;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -254,7 +253,5 @@ enum StarWarsEpisode {
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
<html lang="en" class="sidebar-visible no-js light">
|
||||||
<head>
|
<head>
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Type System - Juniper - GraphQL Server for Rust</title>
|
<title>Type System - Juniper - GraphQL Server for Rust</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
<link rel="icon" href="../favicon.svg">
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
<link rel="shortcut icon" href="../favicon.png">
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
<link rel="stylesheet" href="../css/variables.css">
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
<link rel="stylesheet" href="../css/general.css">
|
||||||
|
@ -17,8 +23,7 @@
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
<link rel="stylesheet" href="../highlight.css">
|
||||||
|
@ -27,12 +32,13 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="light">
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
<script type="text/javascript">
|
||||||
|
var path_to_root = "../";
|
||||||
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -54,9 +60,12 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var theme;
|
var theme;
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||||
document.body.className = theme;
|
var html = document.querySelector('html');
|
||||||
document.querySelector('html').className = theme + ' js';
|
html.classList.remove('no-js')
|
||||||
|
html.classList.remove('light')
|
||||||
|
html.classList.add(theme);
|
||||||
|
html.classList.add('js');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
<!-- Hide / unhide sidebar before it is displayed -->
|
||||||
|
@ -72,15 +81,17 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html" class="active">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
<div class="sidebar-scrollbox">
|
||||||
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html" class="active">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
<div id="menu-bar-hover-placeholder"></div>
|
||||||
<div id="menu-bar-sticky-container">
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
||||||
<div class="left-buttons">
|
<div class="left-buttons">
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||||
<i class="fa fa-bars"></i>
|
<i class="fa fa-bars"></i>
|
||||||
|
@ -89,17 +100,15 @@
|
||||||
<i class="fa fa-paint-brush"></i>
|
<i class="fa fa-paint-brush"></i>
|
||||||
</button>
|
</button>
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
@ -108,14 +117,13 @@
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -124,7 +132,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -136,7 +143,7 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<a class="header" href="#type-system" id="type-system"><h1>Type System</h1></a>
|
<h1 id="type-system"><a class="header" href="#type-system">Type System</a></h1>
|
||||||
<p>Most of the work in working with juniper consists of mapping the
|
<p>Most of the work in working with juniper consists of mapping the
|
||||||
GraphQL type system to the Rust types your application uses.</p>
|
GraphQL type system to the Rust types your application uses.</p>
|
||||||
<p>Juniper provides some convenient abstractions that try to make this process
|
<p>Juniper provides some convenient abstractions that try to make this process
|
||||||
|
@ -165,35 +172,27 @@ as painless as possible.</p>
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
<a rel="prev" href="../quickstart.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
<a rel="prev" href="../quickstart.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/objects/defining_objects.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
<a rel="next" href="../types/objects/defining_objects.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
<a rel="prev" href="../quickstart.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<a href="../quickstart.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a rel="next" href="../types/objects/defining_objects.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
|
||||||
<a href="../types/objects/defining_objects.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -201,15 +200,15 @@ as painless as possible.</p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.playground_copyable = true;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -217,7 +216,5 @@ as painless as possible.</p>
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="en" class="sidebar-visible no-js">
|
<html lang="en" class="sidebar-visible no-js light">
|
||||||
<head>
|
<head>
|
||||||
<!-- Book generated using mdBook -->
|
<!-- Book generated using mdBook -->
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Input objects - Juniper - GraphQL Server for Rust</title>
|
<title>Input objects - Juniper - GraphQL Server for Rust</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Custom HTML head -->
|
||||||
|
|
||||||
|
|
||||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
<meta name="description" content="Documentation for juniper, a GraphQL server library for Rust.">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<meta name="theme-color" content="#ffffff" />
|
<meta name="theme-color" content="#ffffff" />
|
||||||
|
|
||||||
|
<link rel="icon" href="../favicon.svg">
|
||||||
<link rel="shortcut icon" href="../favicon.png">
|
<link rel="shortcut icon" href="../favicon.png">
|
||||||
<link rel="stylesheet" href="../css/variables.css">
|
<link rel="stylesheet" href="../css/variables.css">
|
||||||
<link rel="stylesheet" href="../css/general.css">
|
<link rel="stylesheet" href="../css/general.css">
|
||||||
|
@ -17,8 +23,7 @@
|
||||||
|
|
||||||
<!-- Fonts -->
|
<!-- Fonts -->
|
||||||
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
||||||
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<!-- Highlight.js Stylesheets -->
|
<!-- Highlight.js Stylesheets -->
|
||||||
<link rel="stylesheet" href="../highlight.css">
|
<link rel="stylesheet" href="../highlight.css">
|
||||||
|
@ -27,12 +32,13 @@
|
||||||
|
|
||||||
<!-- Custom theme stylesheets -->
|
<!-- Custom theme stylesheets -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body class="light">
|
<body>
|
||||||
<!-- Provide site root to javascript -->
|
<!-- Provide site root to javascript -->
|
||||||
<script type="text/javascript">var path_to_root = "../";</script>
|
<script type="text/javascript">
|
||||||
|
var path_to_root = "../";
|
||||||
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||||||
|
</script>
|
||||||
|
|
||||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -54,9 +60,12 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var theme;
|
var theme;
|
||||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||||
if (theme === null || theme === undefined) { theme = 'light'; }
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||||
document.body.className = theme;
|
var html = document.querySelector('html');
|
||||||
document.querySelector('html').className = theme + ' js';
|
html.classList.remove('no-js')
|
||||||
|
html.classList.remove('light')
|
||||||
|
html.classList.add(theme);
|
||||||
|
html.classList.add('js');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Hide / unhide sidebar before it is displayed -->
|
<!-- Hide / unhide sidebar before it is displayed -->
|
||||||
|
@ -72,15 +81,17 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||||
<ol class="chapter"><li class="affix"><a href="../index.html">Introduction</a></li><li class="affix"><a href="../quickstart.html">Quickstart</a></li><li class="affix"><a href="../types/index.html">Type System</a></li><li><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li><a href="../types/input_objects.html" class="active"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li><li><a href="../schema/schemas_and_mutations.html"><strong aria-hidden="true">3.</strong> Schemas and mutations</a></li><li><a href="../servers/index.html"><strong aria-hidden="true">4.</strong> Adding A Server</a></li><li><ol class="section"><li><a href="../servers/official.html"><strong aria-hidden="true">4.1.</strong> Official Server Integrations</a></li><li><ol class="section"><li><a href="../servers/warp.html"><strong aria-hidden="true">4.1.1.</strong> Warp</a></li><li><a href="../servers/rocket.html"><strong aria-hidden="true">4.1.2.</strong> Rocket</a></li><li><a href="../servers/iron.html"><strong aria-hidden="true">4.1.3.</strong> Iron</a></li><li><a href="../servers/hyper.html"><strong aria-hidden="true">4.1.4.</strong> Hyper</a></li></ol></li><li><a href="../servers/third-party.html"><strong aria-hidden="true">4.2.</strong> Third Party Integrations</a></li></ol></li><li><a href="../advanced/index.html"><strong aria-hidden="true">5.</strong> Advanced Topics</a></li><li><ol class="section"><li><a href="../advanced/introspection.html"><strong aria-hidden="true">5.1.</strong> Introspection</a></li><li><a href="../advanced/non_struct_objects.html"><strong aria-hidden="true">5.2.</strong> Non-struct objects</a></li><li><a href="../advanced/implicit_and_explicit_null.html"><strong aria-hidden="true">5.3.</strong> Implicit and explicit null</a></li><li><a href="../advanced/objects_and_generics.html"><strong aria-hidden="true">5.4.</strong> Objects and generics</a></li><li><a href="../advanced/multiple_ops_per_request.html"><strong aria-hidden="true">5.5.</strong> Multiple operations per request</a></li><li><a href="../advanced/dataloaders.html"><strong aria-hidden="true">5.6.</strong> Dataloaders</a></li><li><a href="../advanced/subscriptions.html"><strong aria-hidden="true">5.7.</strong> Subscriptions</a></li></ol></li></ol>
|
<div class="sidebar-scrollbox">
|
||||||
|
<ol class="chapter"><li class="chapter-item expanded affix "><a href="../index.html">Introduction</a></li><li class="chapter-item expanded affix "><a href="../quickstart.html">Quickstart</a></li><li class="chapter-item expanded affix "><a href="../types/index.html">Type System</a></li><li class="chapter-item expanded "><a href="../types/objects/defining_objects.html"><strong aria-hidden="true">1.</strong> Defining objects</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/objects/complex_fields.html"><strong aria-hidden="true">1.1.</strong> Complex fields</a></li><li class="chapter-item expanded "><a href="../types/objects/using_contexts.html"><strong aria-hidden="true">1.2.</strong> Using contexts</a></li><li class="chapter-item expanded "><a href="../types/objects/error_handling.html"><strong aria-hidden="true">1.3.</strong> Error handling</a></li></ol></li><li class="chapter-item expanded "><a href="../types/other-index.html"><strong aria-hidden="true">2.</strong> Other types</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../types/enums.html"><strong aria-hidden="true">2.1.</strong> Enums</a></li><li class="chapter-item expanded "><a href="../types/interfaces.html"><strong aria-hidden="true">2.2.</strong> Interfaces</a></li><li class="chapter-item expanded "><a href="../types/input_objects.html" class="active"><strong aria-hidden="true">2.3.</strong> Input objects</a></li><li class="chapter-item expanded "><a href="../types/scalars.html"><strong aria-hidden="true">2.4.</strong> Scalars</a></li><li class="chapter-item expanded "><a href="../types/unions.html"><strong aria-hidden="true">2.5.</strong> Unions</a></li></ol></li></ol> </div>
|
||||||
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
|
|
||||||
<div id="menu-bar" class="menu-bar">
|
<div id="menu-bar-hover-placeholder"></div>
|
||||||
<div id="menu-bar-sticky-container">
|
<div id="menu-bar" class="menu-bar sticky bordered">
|
||||||
<div class="left-buttons">
|
<div class="left-buttons">
|
||||||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||||
<i class="fa fa-bars"></i>
|
<i class="fa fa-bars"></i>
|
||||||
|
@ -89,17 +100,15 @@
|
||||||
<i class="fa fa-paint-brush"></i>
|
<i class="fa fa-paint-brush"></i>
|
||||||
</button>
|
</button>
|
||||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||||
<li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
|
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||||
<i class="fa fa-search"></i>
|
<i class="fa fa-search"></i>
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
<h1 class="menu-title">Juniper - GraphQL Server for Rust</h1>
|
||||||
|
@ -108,14 +117,13 @@
|
||||||
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
<a href="../print.html" title="Print this book" aria-label="Print this book">
|
||||||
<i id="print-button" class="fa fa-print"></i>
|
<i id="print-button" class="fa fa-print"></i>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="search-wrapper" class="hidden">
|
<div id="search-wrapper" class="hidden">
|
||||||
<form id="searchbar-outer" class="searchbar-outer">
|
<form id="searchbar-outer" class="searchbar-outer">
|
||||||
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||||
</form>
|
</form>
|
||||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||||
<div id="searchresults-header" class="searchresults-header"></div>
|
<div id="searchresults-header" class="searchresults-header"></div>
|
||||||
|
@ -124,7 +132,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||||
|
@ -136,38 +143,38 @@
|
||||||
|
|
||||||
<div id="content" class="content">
|
<div id="content" class="content">
|
||||||
<main>
|
<main>
|
||||||
<a class="header" href="#input-objects" id="input-objects"><h1>Input objects</h1></a>
|
<h1 id="input-objects"><a class="header" href="#input-objects">Input objects</a></h1>
|
||||||
<p>Input objects are complex data structures that can be used as arguments to
|
<p>Input objects are complex data structures that can be used as arguments to
|
||||||
GraphQL fields. In Juniper, you can define input objects using a custom derive
|
GraphQL fields. In Juniper, you can define input objects using a custom derive
|
||||||
attribute, similar to simple objects and enums:</p>
|
attribute, similar to simple objects and enums:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
||||||
# extern crate juniper;
|
</span><span class="boring">extern crate juniper;
|
||||||
#[derive(juniper::GraphQLInputObject)]
|
</span>#[derive(juniper::GraphQLInputObject)]
|
||||||
struct Coordinate {
|
struct Coordinate {
|
||||||
latitude: f64,
|
latitude: f64,
|
||||||
longitude: f64
|
longitude: f64
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Root;
|
struct Root;
|
||||||
# #[derive(juniper::GraphQLObject)] struct User { name: String }
|
<span class="boring">#[derive(juniper::GraphQLObject)] struct User { name: String }
|
||||||
|
</span>
|
||||||
#[juniper::graphql_object]
|
#[juniper::graphql_object]
|
||||||
impl Root {
|
impl Root {
|
||||||
fn users_at_location(coordinate: Coordinate, radius: f64) -> Vec<User> {
|
fn users_at_location(coordinate: Coordinate, radius: f64) -> Vec<User> {
|
||||||
// Send coordinate to database
|
// Send coordinate to database
|
||||||
// ...
|
// ...
|
||||||
# unimplemented!()
|
<span class="boring">unimplemented!()
|
||||||
}
|
</span> }
|
||||||
}
|
}
|
||||||
|
|
||||||
# fn main() {}
|
<span class="boring">fn main() {}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
<a class="header" href="#documentation-and-renaming" id="documentation-and-renaming"><h2>Documentation and renaming</h2></a>
|
<h2 id="documentation-and-renaming"><a class="header" href="#documentation-and-renaming">Documentation and renaming</a></h2>
|
||||||
<p>Just like the <a href="objects/defining_objects.html">other</a> <a href="enums.html">derives</a>, you can rename
|
<p>Just like the <a href="objects/defining_objects.html">other</a> <a href="enums.html">derives</a>, you can rename
|
||||||
and add documentation to both the type and the fields:</p>
|
and add documentation to both the type and the fields:</p>
|
||||||
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused_variables)]
|
||||||
# extern crate juniper;
|
</span><span class="boring">extern crate juniper;
|
||||||
#[derive(juniper::GraphQLInputObject)]
|
</span>#[derive(juniper::GraphQLInputObject)]
|
||||||
#[graphql(name="Coordinate", description="A position on the globe")]
|
#[graphql(name="Coordinate", description="A position on the globe")]
|
||||||
struct WorldCoordinate {
|
struct WorldCoordinate {
|
||||||
#[graphql(name="lat", description="The latitude")]
|
#[graphql(name="lat", description="The latitude")]
|
||||||
|
@ -178,53 +185,45 @@ struct WorldCoordinate {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Root;
|
struct Root;
|
||||||
# #[derive(juniper::GraphQLObject)] struct User { name: String }
|
<span class="boring">#[derive(juniper::GraphQLObject)] struct User { name: String }
|
||||||
|
</span>
|
||||||
#[juniper::graphql_object]
|
#[juniper::graphql_object]
|
||||||
impl Root {
|
impl Root {
|
||||||
fn users_at_location(coordinate: WorldCoordinate, radius: f64) -> Vec<User> {
|
fn users_at_location(coordinate: WorldCoordinate, radius: f64) -> Vec<User> {
|
||||||
// Send coordinate to database
|
// Send coordinate to database
|
||||||
// ...
|
// ...
|
||||||
# unimplemented!()
|
<span class="boring">unimplemented!()
|
||||||
}
|
</span> }
|
||||||
}
|
}
|
||||||
|
|
||||||
# fn main() {}
|
<span class="boring">fn main() {}
|
||||||
</code></pre></pre>
|
</span></code></pre></pre>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||||
<!-- Mobile navigation buttons -->
|
<!-- Mobile navigation buttons -->
|
||||||
|
|
||||||
<a rel="prev" href="../types/interfaces.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
<a rel="prev" href="../types/interfaces.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a rel="next" href="../types/scalars.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
<a rel="next" href="../types/scalars.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
<div style="clear: both"></div>
|
<div style="clear: both"></div>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||||
|
<a rel="prev" href="../types/interfaces.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||||||
<a href="../types/interfaces.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
||||||
<i class="fa fa-angle-left"></i>
|
<i class="fa fa-angle-left"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a rel="next" href="../types/scalars.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||||||
|
|
||||||
<a href="../types/scalars.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -232,15 +231,15 @@ impl Root {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.playground_copyable = true;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
|
||||||
|
|
||||||
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
|
||||||
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
<script src="../book.js" type="text/javascript" charset="utf-8"></script>
|
||||||
|
@ -248,7 +247,5 @@ impl Root {
|
||||||
<!-- Custom JS scripts -->
|
<!-- Custom JS scripts -->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue