2019-03-23 08:08:09 -05:00
<!DOCTYPE HTML>
2020-02-21 00:35:48 -06:00
< html lang = "en" class = "sidebar-visible no-js" >
2019-03-23 08:08:09 -05:00
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
< title > 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 >
2020-02-21 00:35:48 -06:00
< body class = "light" >
2019-03-23 08:08:09 -05:00
<!-- Provide site root to javascript -->
2020-02-21 00:35:48 -06:00
< script type = "text/javascript" > var path _to _root = "" ; < / script >
2019-03-23 08:08:09 -05:00
<!-- 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;
2020-02-21 00:35:48 -06:00
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';
2019-03-23 08:08:09 -05:00
< / 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" >
2020-10-23 18:39:12 -05:00
< 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 >
2019-03-23 08:08:09 -05:00
< / 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" >
2020-02-21 00:35:48 -06:00
< li role = "none" > < button role = "menuitem" class = "theme" id = "light" > Light < span class = "default" > (default)< / span > < / button > < / li >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< h1 class = "menu-title" > Juniper - GraphQL Server for Rust< / h1 >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#juniper" id = "juniper" > < h1 > Juniper< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#features" id = "features" > < h2 > Features< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#integrations" id = "integrations" > < h2 > Integrations< / h2 > < / a >
< a class = "header" href = "#data-types" id = "data-types" > < h3 > Data types< / h3 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:24:11 -06:00
< li > < a href = "https://crates.io/crates/bson" > bson< / a > < / li >
2019-03-23 08:08:09 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#web-frameworks" id = "web-frameworks" > < h3 > Web Frameworks< / h3 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#api-stability" id = "api-stability" > < h2 > API Stability< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > Juniper has not reached 1.0 yet, thus some API instability should be expected.< / p >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#quickstart" id = "quickstart" > < h1 > Quickstart< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > This page will give you a short introduction to the concepts in Juniper.< / p >
2020-06-05 22:59:45 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#installation" id = "installation" > < h2 > Installation< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > !FILENAME Cargo.toml< / p >
< pre > < code class = "language-toml" > [dependencies]
2020-09-11 03:35:22 -05:00
juniper = { git = " https://github.com/graphql-rust/juniper" }
2019-03-23 08:08:09 -05:00
< / code > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#schema-example" id = "schema-example" > < h2 > Schema example< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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
2020-07-16 02:55:41 -05:00
< 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
2019-03-23 08:08:09 -05:00
resolvers, which you will use for the < code > Query< / code > and < code > Mutation< / code > roots.< / p >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-11-06 20:32:22 -06:00
# use std::fmt::Display;
use juniper::{
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
GraphQLInputObject, GraphQLObject, ScalarValue,
};
#
2020-02-21 00:35:48 -06:00
# struct DatabasePool;
# impl DatabasePool {
# fn get_connection(& self) -> FieldResult< DatabasePool> { Ok(DatabasePool) }
# fn find_human(& self, _id: & str) -> FieldResult< Human> { Err(" " )? }
# fn insert_human(& self, _human: & NewHuman) -> FieldResult< Human> { Err(" " )? }
# }
2020-11-06 20:32:22 -06:00
#[derive(GraphQLEnum)]
2019-03-23 08:08:09 -05:00
enum Episode {
NewHope,
Empire,
Jedi,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
#[graphql(description = " A humanoid creature in the Star Wars universe" )]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
name: String,
appears_in: Vec< Episode> ,
home_planet: String,
}
// There is also a custom derive for mapping GraphQL input objects.
2020-11-06 20:32:22 -06:00
#[derive(GraphQLInputObject)]
#[graphql(description = " A humanoid creature in the Star Wars universe" )]
2019-03-23 08:08:09 -05:00
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
2019-05-14 01:52:54 -05:00
// object macro.
2019-03-23 08:08:09 -05:00
// 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;
2020-11-06 20:32:22 -06:00
#[graphql_object(
2019-05-14 01:52:54 -05:00
// Here we specify the context type for the object.
// We need to do this in every type that
// needs access to the context.
2020-11-06 20:32:22 -06:00
context = Context,
2019-05-14 01:52:54 -05:00
)]
impl Query {
fn apiVersion() -> & str {
2019-03-23 08:08:09 -05:00
" 1.0"
}
// Arguments to resolvers can either be simple types or input objects.
2019-05-14 01:52:54 -05:00
// 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> {
2019-03-23 08:08:09 -05:00
// 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)
}
2019-05-14 01:52:54 -05:00
}
// Now, we do the same for our Mutation type.
2019-03-23 08:08:09 -05:00
struct Mutation;
2020-11-06 20:32:22 -06:00
#[graphql_object(
context = Context,
2019-03-23 08:08:09 -05:00
2020-11-06 20:32:22 -06:00
// 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())?;
2019-03-23 08:08:09 -05:00
Ok(human)
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-07-18 20:35:25 -05:00
// A root schema consists of a query, a mutation, and a subscription.
2019-03-23 08:08:09 -05:00
// Request queries can be executed against a RootNode.
2020-03-18 22:40:03 -05:00
type Schema = juniper::RootNode< 'static, Query, Mutation, EmptySubscription< Context> > ;
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {
2020-03-18 22:40:03 -05:00
# let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
2020-02-21 00:35:48 -06:00
# }
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-16 02:55:41 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#executor" id = "executor" > < h2 > Executor< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > You can invoke < code > juniper::execute< / code > directly to run a GraphQL query:< / p >
2020-02-21 00:35:48 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # // Only needed due to 2018 edition because the macro is not accessible.
# #[macro_use] extern crate juniper;
2020-11-06 20:32:22 -06:00
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
2020-12-28 12:04:30 -06:00
GraphQLEnum, Variables, graphql_value,
2020-11-06 20:32:22 -06:00
};
2019-05-14 01:52:54 -05:00
2020-11-06 20:32:22 -06:00
#[derive(GraphQLEnum, Clone, Copy)]
2019-03-23 08:08:09 -05:00
enum Episode {
NewHope,
Empire,
Jedi,
}
2019-05-14 01:52:54 -05:00
// Arbitrary context data.
struct Ctx(Episode);
impl juniper::Context for Ctx {}
2019-03-23 08:08:09 -05:00
struct Query;
2020-11-06 20:32:22 -06:00
#[graphql_object(context = Ctx)]
2019-05-14 01:52:54 -05:00
impl Query {
fn favoriteEpisode(context: & Ctx) -> FieldResult< Episode> {
Ok(context.0)
2019-03-23 08:08:09 -05:00
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-07-18 20:35:25 -05:00
// A root schema consists of a query, a mutation, and a subscription.
2019-03-23 08:08:09 -05:00
// Request queries can be executed against a RootNode.
2020-03-18 22:40:03 -05:00
type Schema = juniper::RootNode< 'static, Query, EmptyMutation< Ctx> , EmptySubscription< Ctx> > ;
2019-03-23 08:08:09 -05:00
fn main() {
// Create a context object.
let ctx = Ctx(Episode::NewHope);
// Run the executor.
2020-03-10 00:45:33 -05:00
let (res, _errors) = juniper::execute_sync(
2019-03-23 08:08:09 -05:00
" query { favoriteEpisode }" ,
None,
2020-03-18 22:40:03 -05:00
& Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
2019-03-23 08:08:09 -05:00
& Variables::new(),
& ctx,
).unwrap();
// Ensure the value matches.
assert_eq!(
res,
graphql_value!({
2019-04-09 19:26:56 -05:00
" favoriteEpisode" : " NEW_HOPE" ,
2019-03-23 08:08:09 -05:00
})
);
}
< / code > < / pre > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#type-system" id = "type-system" > < h1 > Type System< / h1 > < / a >
< p > Most of the work in working with juniper consists of mapping the
2019-03-23 08:08:09 -05:00
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 >
2020-02-21 00:35:48 -06:00
< li > < a href = "objects/defining_objects.html" > Defining objects< / a >
2019-03-23 08:08:09 -05:00
< ul >
2020-02-21 00:35:48 -06:00
< 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 >
2019-03-23 08:08:09 -05:00
< / ul >
< / li >
2020-02-21 00:35:48 -06:00
< li > < a href = "other-index.html" > Other types< / a >
2019-03-23 08:08:09 -05:00
< ul >
2020-02-21 00:35:48 -06:00
< 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 >
2019-03-23 08:08:09 -05:00
< / ul >
< / li >
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#defining-objects" id = "defining-objects" > < h1 > Defining objects< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
attribute. The other way is described in the < a href = "complex_fields.html" > Complex fields< / a >
2019-03-23 08:08:09 -05:00
chapter.< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
/// 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,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
#[graphql(description = " Information about a person" )]
2019-03-23 08:08:09 -05:00
struct Person {
2020-11-06 20:32:22 -06:00
#[graphql(description = " The person's full name, including both first and last names" )]
2019-03-23 08:08:09 -05:00
name: String,
2020-11-06 20:32:22 -06:00
#[graphql(description = " The person's age in years, rounded down" )]
2019-03-23 08:08:09 -05:00
age: i32,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
#[graphql(description = " This description shows up in GraphQL" )]
2019-03-23 08:08:09 -05:00
/// This description shows up in RustDoc
struct Person {
2020-11-06 20:32:22 -06:00
#[graphql(description = " This description shows up in GraphQL" )]
2019-03-23 08:08:09 -05:00
/// This description shows up in RustDoc
name: String,
/// This description shows up in both RustDoc and GraphQL
age: i32,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#relationships" id = "relationships" > < h2 > Relationships< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct House {
address: Option< String> , // Converted into String (nullable)
inhabitants: Vec< Person> , // Converted into [Person!]!
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#renaming-fields" id = "renaming-fields" > < h2 > Renaming fields< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
first_name: String, // Would be exposed as firstName in the GraphQL schema
last_name: String, // Exposed as lastName
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< p > You can override the name by using the < code > graphql< / code > attribute on individual struct
fields:< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
2020-11-06 20:32:22 -06:00
#[graphql(name = " websiteURL" )]
2019-03-23 08:08:09 -05:00
website_url: Option< String> , // Now exposed as websiteURL in the schema
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#deprecating-fields" id = "deprecating-fields" > < h2 > Deprecating fields< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > To deprecate a field, you specify a deprecation reason using the < code > graphql< / code >
attribute:< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
2019-05-14 01:52:54 -05:00
#[graphql(deprecated = " Please use the name field instead" )]
2019-03-23 08:08:09 -05:00
first_name: String,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#skipping-fields" id = "skipping-fields" > < h2 > Skipping fields< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
#[graphql(skip)]
2020-02-21 00:35:48 -06:00
# #[allow(dead_code)]
password_hash: String, // This cannot be queried or modified from GraphQL
2019-03-23 08:08:09 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#complex-fields" id = "complex-fields" > < h1 > Complex fields< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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:
2020-11-06 20:32:22 -06:00
the < code > #[graphql_object]< / code > procedural macro. This macro lets you define GraphQL object
2020-03-04 11:05:53 -06:00
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
2019-03-23 08:08:09 -05:00
example from the last chapter, this is how you would define < code > Person< / code > using the
macro:< / p >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::graphql_object;
2020-10-06 05:18:31 -05:00
#
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl Person {
fn name(& self) -> & str {
2019-03-23 08:08:09 -05:00
self.name.as_str()
}
2019-05-14 01:52:54 -05:00
fn age(& self) -> i32 {
2019-03-23 08:08:09 -05:00
self.age
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2019-12-14 02:34:44 -06:00
// 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) {
// [...]
}
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, GraphQLObject};
#
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Person {
name: String,
age: i32,
}
struct House {
inhabitants: Vec< Person> ,
}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl House {
2019-03-23 08:08:09 -05:00
// Creates the field inhabitantWithName(name), returning a nullable person
2019-05-14 01:52:54 -05:00
fn inhabitant_with_name(& self, name: String) -> Option< & Person> {
2019-03-23 08:08:09 -05:00
self.inhabitants.iter().find(|p| p.name == name)
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
chapter: < a href = "using_contexts.html" > Using contexts< / a > .< / p >
< a class = "header" href = "#description-renaming-and-deprecation" id = "description-renaming-and-deprecation" > < h2 > Description, renaming, and deprecation< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::graphql_object;
#
struct Person;
2019-03-23 08:08:09 -05:00
2019-05-16 07:19:41 -05:00
/// Doc comments are used as descriptions for GraphQL.
2020-11-06 20:32:22 -06:00
#[graphql_object(
2020-07-14 15:07:30 -05:00
// With this attribute you can change the public GraphQL name of the type.
2019-05-14 01:52:54 -05:00
name = " PersonObject" ,
2020-11-06 20:32:22 -06:00
2019-05-16 07:19:41 -05:00
// You can also specify a description here, which will overwrite
// a doc comment description.
description = " ..." ,
2019-05-14 01:52:54 -05:00
)]
impl Person {
2019-05-16 07:19:41 -05:00
/// 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.
2020-11-06 20:32:22 -06:00
#[graphql(name = " myCustomFieldName" )]
2019-05-16 07:19:41 -05:00
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
2019-03-23 08:08:09 -05:00
}
2019-05-16 07:19:41 -05:00
#[graphql(deprecated = " ..." )]
fn deprecated_graphql() -> bool {
true
}
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
< a class = "header" href = "#customizing-arguments" id = "customizing-arguments" > < h2 > Customizing arguments< / h2 > < / a >
2019-05-16 07:19:41 -05:00
< p > Method field arguments can also be customized.< / p >
< p > They can have custom descriptions and default values.< / p >
2020-02-21 00:35:48 -06:00
< p > < strong > Note< / strong > : The syntax for this is currently a little awkward.
2019-05-16 07:19:41 -05:00
This will become better once the < a href = "https://github.com/rust-lang/rust/issues/60406" > Rust RFC 2565< / a > is implemented.< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::graphql_object;
#
2019-05-16 07:19:41 -05:00
struct Person {}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-16 07:19:41 -05:00
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)
2019-03-23 08:08:09 -05:00
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
< a class = "header" href = "#more-features" id = "more-features" > < h2 > More features< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-11-06 20:32:22 -06:00
< p > These, and more features, are described more thoroughly in < a href = "https://docs.rs/juniper/latest/juniper/macro.object.html" > the reference
2019-03-23 08:08:09 -05:00
documentation< / a > .< / p >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#using-contexts" id = "using-contexts" > < h1 > Using contexts< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
documentation for either the < a href = "../../servers/iron.html" > Iron< / a > or < a href = "../../servers/rocket.html" > Rocket< / a >
2019-03-23 08:08:09 -05:00
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 >
2020-02-21 00:35:48 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# use std::collections::HashMap;
2020-11-06 20:32:22 -06:00
#
2019-03-23 08:08:09 -05:00
struct Database {
users: HashMap< i32, User> ,
}
struct User {
id: i32,
name: String,
friend_ids: Vec< i32> ,
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
the user object.< / p >
< p > To gain access to the context, we need to specify an argument with the same
2019-05-14 01:52:54 -05:00
type as the specified < code > Context< / code > for the type:< / p >
2020-11-06 20:32:22 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# use std::collections::HashMap;
# use juniper::graphql_object;
#
2019-05-14 01:52:54 -05:00
// This struct represents our context.
2019-03-23 08:08:09 -05:00
struct Database {
users: HashMap< i32, User> ,
}
2019-05-14 01:52:54 -05:00
// Mark the Database as a valid context type for Juniper
impl juniper::Context for Database {}
2019-03-23 08:08:09 -05:00
struct User {
id: i32,
name: String,
friend_ids: Vec< i32> ,
}
2019-05-14 01:52:54 -05:00
// Assign Database as the context type for User
2020-11-06 20:32:22 -06:00
#[graphql_object(context = Database)]
2019-05-14 01:52:54 -05:00
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> {
2019-03-23 08:08:09 -05:00
// 5. Use the database to lookup users
self.friend_ids.iter()
2019-05-14 01:52:54 -05:00
.map(|id| context.users.get(id).expect(" Could not find user with ID" ))
2019-03-23 08:08:09 -05:00
.collect()
}
2019-05-14 01:52:54 -05:00
fn name(& self) -> & str {
self.name.as_str()
}
fn id(& self) -> i32 {
self.id
}
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#error-handling" id = "error-handling" > < h1 > Error handling< / h1 > < / a >
2020-04-28 12:03:19 -05:00
< 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 >
< a class = "header" href = "#field-results" id = "field-results" > < h2 > Field Results< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
use std::{
2019-03-23 08:08:09 -05:00
str,
path::PathBuf,
fs::{File},
io::{Read},
};
2020-11-06 20:32:22 -06:00
use juniper::{graphql_object, FieldResult};
2019-03-23 08:08:09 -05:00
struct Example {
filename: PathBuf,
}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl Example {
fn contents() -> FieldResult< String> {
2019-03-23 08:08:09 -05:00
let mut file = File::open(& self.filename)?;
let mut contents = String::new();
file.read_to_string(& mut contents)?;
Ok(contents)
}
2019-05-14 01:52:54 -05:00
fn foo() -> FieldResult< Option< String> > {
2019-03-23 08:08:09 -05:00
// Some invalid bytes.
let invalid = vec![128, 223];
match str::from_utf8(& invalid) {
Ok(s) => Ok(Some(s.to_string())),
Err(e) => Err(e)?,
}
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-16 02:17:45 -05:00
< a class = "header" href = "#error-payloads-null-and-partial-errors" id = "error-payloads-null-and-partial-errors" > < h2 > Error payloads, < code > null< / code > , and partial errors< / h2 > < / a >
< p > Juniper's error behavior conforms to the < a href = "https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability" > GraphQL specification< / a > .< / p >
2019-03-23 08:08:09 -05:00
< 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 >
2020-04-28 12:03:19 -05:00
< a class = "header" href = "#structured-errors" id = "structured-errors" > < h3 > Structured errors< / h3 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #[macro_use] extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, FieldError, IntoFieldError, ScalarValue};
#
2020-02-21 00:35:48 -06:00
enum CustomError {
2019-03-23 08:08:09 -05:00
WhateverNotSet,
}
2020-11-06 20:32:22 -06:00
impl< S: ScalarValue> IntoFieldError< S> for CustomError {
fn into_field_error(self) -> FieldError< S> {
2019-03-23 08:08:09 -05:00
match self {
2020-11-06 20:32:22 -06:00
CustomError::WhateverNotSet => FieldError::new(
2019-03-23 08:08:09 -05:00
" Whatever does not exist" ,
2019-05-02 12:22:08 -05:00
graphql_value!({
2019-03-23 08:08:09 -05:00
" type" : " NO_WHATEVER"
}),
),
}
}
}
struct Example {
whatever: Option< bool> ,
}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl Example {
fn whatever() -> Result< bool, CustomError> {
2019-03-23 08:08:09 -05:00
if let Some(value) = self.whatever {
return Ok(value);
}
Err(CustomError::WhateverNotSet)
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-11-06 20:32:22 -06:00
< pre > < code class = "language-json" > {
" errors" : [{
2019-03-23 08:08:09 -05:00
" message" : " Whatever does not exist" ,
2020-11-06 20:32:22 -06:00
" locations" : [{" line" : 2, " column" : 4}],
2019-03-23 08:08:09 -05:00
" extensions" : {
" type" : " NO_WHATEVER"
}
2020-11-06 20:32:22 -06:00
}]
2019-03-23 08:08:09 -05:00
}
< / code > < / pre >
2020-04-28 12:03:19 -05:00
< a class = "header" href = "#errors-backed-by-graphqls-schema" id = "errors-backed-by-graphqls-schema" > < h2 > Errors Backed by GraphQL's Schema< / h2 > < / a >
< 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 >
< a class = "header" href = "#example-input-validation-simple" id = "example-input-validation-simple" > < h3 > Example Input Validation (simple)< / h3 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct Item {
name: String,
quantity: i32,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct ValidationError {
field: String,
message: String,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct ValidationErrors {
errors: Vec< ValidationError> ,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLUnion)]
2020-04-28 12:03:19 -05:00
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrors),
}
pub struct Mutation;
2020-11-06 20:32:22 -06:00
#[graphql_object]
2020-04-28 12:03:19 -05:00
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 })
}
}
}
2020-11-06 20:32:22 -06:00
#
2020-04-28 12:03:19 -05:00
# fn main() {}
< / 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 >
< a class = "header" href = "#example-input-validation-complex" id = "example-input-validation-complex" > < h3 > Example Input Validation (complex)< / h3 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct Item {
name: String,
quantity: i32,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct ValidationError {
name: Option< String> ,
quantity: Option< String> ,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLUnion)]
2020-04-28 12:03:19 -05:00
pub enum GraphQLResult {
Ok(Item),
Err(ValidationError),
}
pub struct Mutation;
2020-11-06 20:32:22 -06:00
#[graphql_object]
2020-04-28 12:03:19 -05:00
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)
}
}
}
2020-11-06 20:32:22 -06:00
#
2020-04-28 12:03:19 -05:00
# fn main() {}
< / 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 >
< a class = "header" href = "#example-input-validation-complex-with-critical-error" id = "example-input-validation-complex-with-critical-error" > < h3 > Example Input Validation (complex with critical error)< / h3 > < / a >
< 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 >
2020-11-06 20:32:22 -06:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
#
use juniper::{graphql_object, graphql_value, FieldError, GraphQLObject, GraphQLUnion, ScalarValue};
2020-04-28 12:03:19 -05:00
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct Item {
name: String,
quantity: i32,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2020-04-28 12:03:19 -05:00
pub struct ValidationErrorItem {
name: Option< String> ,
quantity: Option< String> ,
}
2020-11-06 20:32:22 -06:00
#[derive(GraphQLUnion)]
2020-04-28 12:03:19 -05:00
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrorItem),
}
pub enum ApiError {
Database,
}
2020-11-06 20:32:22 -06:00
impl< S: ScalarValue> juniper::IntoFieldError< S> for ApiError {
fn into_field_error(self) -> FieldError< S> {
2020-04-28 12:03:19 -05:00
match self {
2020-11-06 20:32:22 -06:00
ApiError::Database => FieldError::new(
2020-04-28 12:03:19 -05:00
" Internal database error" ,
graphql_value!({
" type" : " DATABASE"
}),
),
}
}
}
pub struct Mutation;
2020-11-06 20:32:22 -06:00
#[graphql_object]
2020-04-28 12:03:19 -05:00
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))
}
}
}
2020-11-06 20:32:22 -06:00
#
2020-04-28 12:03:19 -05:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#additional-material" id = "additional-material" > < h2 > Additional Material< / h2 > < / a >
< 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 >
< a class = "header" href = "#comparison" id = "comparison" > < h1 > Comparison< / h1 > < / a >
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#other-types" id = "other-types" > < h1 > Other Types< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > The GraphQL type system provides several types in additon to objects.< / p >
< p > Find out more about each type below:< / p >
< ul >
2020-02-21 00:35:48 -06:00
< 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 >
2019-03-23 08:08:09 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#enums" id = "enums" > < h1 > Enums< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
#[derive(juniper::GraphQLEnum)]
2019-03-23 08:08:09 -05:00
enum Episode {
NewHope,
Empire,
Jedi,
}
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
it works when < a href = "objects/defining_objects.html" > defining objects< / a > :< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
#[derive(juniper::GraphQLEnum)]
2019-03-23 08:08:09 -05:00
enum Episode {
#[graphql(name=" NEW_HOPE" )]
NewHope,
Empire,
Jedi,
}
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#documentation-and-deprecation" id = "documentation-and-deprecation" > < h2 > Documentation and deprecation< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
#[derive(juniper::GraphQLEnum)]
2019-03-23 08:08:09 -05:00
#[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,
}
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2020-04-18 00:12:14 -05:00
< a class = "header" href = "#supported-macro-attributes-derive" id = "supported-macro-attributes-derive" > < h2 > Supported Macro Attributes (Derive)< / h2 > < / a >
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#interfaces" id = "interfaces" > < h1 > Interfaces< / h1 > < / a >
2020-10-06 05:18:31 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#traits" id = "traits" > < h2 > Traits< / h2 > < / a >
2020-10-06 05:18:31 -05:00
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::graphql_interface;
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[graphql_interface]
trait Character {
fn id(& self) -> & str;
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#
# fn main() {}
< / 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 >
< a class = "header" href = "#enum-values-default" id = "enum-values-default" > < h3 > Enum values (default)< / h3 > < / a >
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[graphql_interface(for = [Human, Droid])] // enumerating all implementers is mandatory
2019-03-23 08:08:09 -05:00
trait Character {
fn id(& self) -> & str;
}
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)] // notice enum name, NOT trait name
struct Human {
id: String,
}
#[graphql_interface] // implementing requires macro attribute too, (°o°)!
2019-03-23 08:08:09 -05:00
impl Character for Human {
2020-10-06 05:18:31 -05:00
fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)]
struct Droid {
id: String,
}
#[graphql_interface]
2019-03-23 08:08:09 -05:00
impl Character for Droid {
2020-10-06 05:18:31 -05:00
fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
# fn main() {
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" );
# }
< / code > < / pre > < / pre >
< p > Also, enum name can be specified explicitly, if desired.< / p >
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[graphql_interface(enum = CharaterInterface, for = Human)]
trait Character {
fn id(& self) -> & str;
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = CharaterInterface)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-10-06 05:18:31 -05:00
#[graphql_interface]
impl Character for Human {
fn id(& self) -> & str {
& self.id
}
}
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#trait-object-values" id = "trait-object-values" > < h3 > Trait object values< / h3 > < / a >
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
# extern crate tokio;
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
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = DynCharacter< __S> )] // macro adds `ScalarValue` type parameter to trait,
struct Human { // so it may be specified explicitly when required
2019-03-23 08:08:09 -05:00
id: String,
2020-10-06 05:18:31 -05:00
}
#[graphql_interface(dyn)] // implementing requires to know about dynamic dispatch too
impl Character for Human {
async fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = DynCharacter< __S> )]
struct Droid {
id: String,
}
#[graphql_interface]
impl Character for Droid {
async fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
# #[tokio::main]
# async fn main() {
let human = Human { id: " human-32" .to_owned() };
let character: Box< DynCharacter> = Box::new(human);
assert_eq!(character.id().await, " human-32" );
# }
< / code > < / pre > < / pre >
< a class = "header" href = "#ignoring-trait-methods" id = "ignoring-trait-methods" > < h3 > Ignoring trait methods< / h3 > < / a >
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[graphql_interface(for = Human)]
2019-03-23 08:08:09 -05:00
trait Character {
fn id(& self) -> & str;
2020-10-06 05:18:31 -05:00
2020-11-14 07:58:04 -06:00
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
2020-10-06 05:18:31 -05:00
fn ignored(& self) -> u32 { 0 }
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)]
struct Human {
id: String,
}
#[graphql_interface]
2019-03-23 08:08:09 -05:00
impl Character for Human {
2020-10-06 05:18:31 -05:00
fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#fields-arguments-and-interface-customization" id = "fields-arguments-and-interface-customization" > < h3 > Fields, arguments and interface customization< / h3 > < / a >
< 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 = "playpen" > < code class = "language-rust" > # #![allow(deprecated)]
# extern crate juniper;
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.
2020-11-14 07:58:04 -06:00
#[graphql(name = " myId" )]
2020-10-06 05:18:31 -05:00
// 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.
2020-11-14 07:58:04 -06:00
#[graphql(deprecated = " Do not use it." )]
2020-10-06 05:18:31 -05:00
// Describes the field in GraphQL schema.
2020-11-14 07:58:04 -06:00
#[graphql(description = " ID of my own character." )]
2020-10-06 05:18:31 -05:00
// 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.
2020-11-14 07:58:04 -06:00
#[graphql(name = " myNum" )]
2020-10-06 05:18:31 -05:00
// Describes the argument in GraphQL schema.
2020-11-14 07:58:04 -06:00
#[graphql(description = " ID number of my own character." )]
2020-10-06 05:18:31 -05:00
// Specifies the default value for the argument.
// The concrete value may be omitted, and the `Default::default` one
// will be used in such case.
2020-11-14 07:58:04 -06:00
#[graphql(default = 5)]
2020-10-06 05:18:31 -05:00
num: i32,
) -> & str;
}
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#custom-context" id = "custom-context" > < h3 > Custom context< / h3 > < / a >
2020-10-07 03:24:29 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# use std::collections::HashMap;
use juniper::{graphql_interface, GraphQLObject};
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
struct Database {
humans: HashMap< String, Human> ,
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
impl juniper::Context for Database {}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[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> ;
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
// Otherwise, you may mark it explicitly as a context argument.
2020-11-14 07:58:04 -06:00
fn name(& self, #[graphql(context)] db: & Database) -> Option< & str> ;
2020-10-06 05:18:31 -05:00
}
#[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
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
fn name(& self, db: & Database) -> Option< & str> {
if db.humans.contains_key(& self.id) {
Some(& self.name)
} else {
None
}
}
}
#
2020-02-21 00:35:48 -06:00
# fn main() {}
2020-10-06 05:18:31 -05:00
< / code > < / pre > < / pre >
< a class = "header" href = "#using-executor-and-explicit-generic-scalar" id = "using-executor-and-explicit-generic-scalar" > < h3 > Using executor and explicit generic scalar< / h3 > < / a >
2020-10-07 03:24:29 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_interface, Executor, GraphQLObject, LookAheadMethods as _, ScalarValue};
2020-10-07 03:24:29 -05:00
#[graphql_interface(for = Human, Scalar = S)] // notice specifying `ScalarValue` as existing type parameter
2020-10-06 05:18:31 -05:00
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,
2020-11-14 07:58:04 -06:00
#[graphql(executor)] another: & Executor< '_, '_, (), S> ,
2020-10-06 05:18:31 -05:00
) -> & 'b str
where
S: Send + Sync;
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue< __S> )]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
2020-10-06 05:18:31 -05:00
name: String,
2019-03-23 08:08:09 -05:00
}
2020-11-14 07:58:04 -06:00
#[graphql_interface(scalar = S)]
2020-10-06 05:18:31 -05:00
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()
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
async fn name< 'b> (& 'b self, _: & Executor< '_, '_, (), S> ) -> & 'b str
where
S: Send + Sync,
{
& self.name
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#downcasting" id = "downcasting" > < h3 > Downcasting< / h3 > < / a >
2020-10-07 03:24:29 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
# use std::collections::HashMap;
use juniper::{graphql_interface, GraphQLObject};
2019-03-23 08:08:09 -05:00
struct Database {
droids: HashMap< String, Droid> ,
}
impl juniper::Context for Database {}
2020-11-14 07:58:04 -06:00
#[graphql_interface(for = [Human, Droid], context = Database)]
2020-10-06 05:18:31 -05:00
#[graphql_interface(on Droid = get_droid)] // enables downcasting `Droid` via `get_droid()` function
trait Character {
fn id(& self) -> & str;
2020-11-14 07:58:04 -06:00
#[graphql(downcast)] // makes method a downcast to `Human`, not a field
2020-10-06 05:18:31 -05:00
// 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 {
2019-03-23 08:08:09 -05:00
id: String,
}
2020-10-06 05:18:31 -05:00
#[graphql_interface]
impl Character for Human {
fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
fn as_human(& self) -> Option< & Self> {
Some(self)
}
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue, Context = Database)]
struct Droid {
id: String,
}
#[graphql_interface]
impl Character for Droid {
fn id(& self) -> & str {
& self.id
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
// 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())
}
#
2020-02-21 00:35:48 -06:00
# fn main() {}
2020-10-06 05:18:31 -05:00
< / code > < / pre > < / pre >
2020-10-07 03:24:29 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< a class = "header" href = "#scalarvalue-considerations" id = "scalarvalue-considerations" > < h2 > < code > ScalarValue< / code > considerations< / h2 > < / a >
< 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 = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_interface, DefaultScalarValue, GraphQLObject};
#[graphql_interface(for = [Human, Droid])]
2020-11-14 07:58:04 -06:00
#[graphql_interface(scalar = DefaultScalarValue)] // removing this line will fail compilation
2020-10-06 05:18:31 -05:00
trait Character {
fn id(& self) -> & str;
}
#[derive(GraphQLObject)]
2020-10-07 03:24:29 -05:00
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-11-14 07:58:04 -06:00
#[graphql_interface(scalar = DefaultScalarValue)]
2020-10-06 05:18:31 -05:00
impl Character for Human {
fn id(& self) -> & str {
& self.id
}
}
2019-03-23 08:08:09 -05:00
2020-10-06 05:18:31 -05:00
#[derive(GraphQLObject)]
2020-10-07 03:24:29 -05:00
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
2019-03-23 08:08:09 -05:00
struct Droid {
id: String,
primary_function: String,
}
2020-11-14 07:58:04 -06:00
#[graphql_interface(scalar = DefaultScalarValue)]
2020-10-06 05:18:31 -05:00
impl Character for Droid {
fn id(& self) -> & str {
& self.id
}
2019-03-23 08:08:09 -05:00
}
2020-10-06 05:18:31 -05:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
2020-10-06 05:18:31 -05:00
< / code > < / pre > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#input-objects" id = "input-objects" > < h1 > Input objects< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-07-18 20:35:25 -05:00
#[derive(juniper::GraphQLInputObject)]
2019-03-23 08:08:09 -05:00
struct Coordinate {
latitude: f64,
longitude: f64
}
struct Root;
2020-02-21 00:35:48 -06:00
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object]
2019-05-14 01:52:54 -05:00
impl Root {
fn users_at_location(coordinate: Coordinate, radius: f64) -> Vec< User> {
2019-03-23 08:08:09 -05:00
// Send coordinate to database
2019-05-14 01:52:54 -05:00
// ...
2020-02-21 00:35:48 -06:00
# unimplemented!()
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#documentation-and-renaming" id = "documentation-and-renaming" > < h2 > Documentation and renaming< / h2 > < / a >
< p > Just like the < a href = "objects/defining_objects.html" > other< / a > < a href = "enums.html" > derives< / a > , you can rename
2019-03-23 08:08:09 -05:00
and add documentation to both the type and the fields:< / p >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-07-18 20:35:25 -05:00
#[derive(juniper::GraphQLInputObject)]
2019-03-23 08:08:09 -05:00
#[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;
2020-02-21 00:35:48 -06:00
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object]
2019-05-14 01:52:54 -05:00
impl Root {
fn users_at_location(coordinate: WorldCoordinate, radius: f64) -> Vec< User> {
2019-03-23 08:08:09 -05:00
// Send coordinate to database
2019-05-14 01:52:54 -05:00
// ...
2020-02-21 00:35:48 -06:00
# unimplemented!()
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#scalars" id = "scalars" > < h1 > Scalars< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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
2020-02-21 00:35:48 -06:00
also limited in the data types you can use.< / p >
< p > There are two ways to define custom scalars.< / p >
2019-06-25 12:02:41 -05:00
< ul >
< li > For simple scalars that just wrap a primitive type, you can use the newtype pattern with
2020-02-21 00:35:48 -06:00
a custom derive.< / li >
2019-06-25 12:02:41 -05:00
< li > For more advanced use cases with custom validation, you can use
2020-04-20 15:24:09 -05:00
the < code > graphql_scalar< / code > proc macro.< / li >
2019-06-25 12:02:41 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#built-in-scalars" id = "built-in-scalars" > < h2 > Built-in scalars< / h2 > < / a >
2019-06-25 12:02:41 -05:00
< 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 >
2020-06-24 03:50:55 -05:00
< 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 >
2019-06-25 12:02:41 -05:00
< 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 >
2020-02-21 00:24:11 -06:00
< li > bson::oid::ObjectId< / li >
2019-06-25 12:02:41 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#newtype-pattern" id = "newtype-pattern" > < h2 > newtype pattern< / h2 > < / a >
< p > Often, you might need a custom scalar that just wraps an existing type.< / p >
2019-06-25 12:02:41 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
#[derive(juniper::GraphQLScalarValue)]
2019-06-25 12:02:41 -05:00
pub struct UserId(i32);
#[derive(juniper::GraphQLObject)]
struct User {
id: UserId,
}
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-06-25 12:02:41 -05:00
< p > That's it, you can now user < code > UserId< / code > in your schema.< / p >
< p > The macro also allows for more customization:< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
/// You can use a doc comment to specify a description.
2019-06-25 12:02:41 -05:00
#[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);
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#custom-scalars" id = "custom-scalars" > < h2 > Custom scalars< / h2 > < / a >
< p > For more complex situations where you also need custom parsing or validation,
2020-04-20 15:24:09 -05:00
you can use the < code > graphql_scalar< / code > proc macro.< / p >
2019-06-25 12:02:41 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< 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
2019-06-25 12:02:41 -05:00
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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# mod date {
2020-02-21 00:35:48 -06:00
# pub struct Date;
# impl std::str::FromStr for Date{
# type Err = String; fn from_str(_value: & str) -> Result< Self, Self::Err> { unimplemented!() }
# }
# // And we define how to represent date as a string.
# impl std::fmt::Display for Date {
# fn fmt(& self, _f: & mut std::fmt::Formatter) -> std::fmt::Result {
# unimplemented!()
# }
# }
# }
2019-06-25 12:02:41 -05:00
use juniper::{Value, ParseScalarResult, ParseScalarValue};
use date::Date;
2019-03-23 08:08:09 -05:00
2020-04-20 15:24:09 -05:00
#[juniper::graphql_scalar(description = " Date" )]
impl< S> GraphQLScalar for Date
where
S: ScalarValue
{
2019-06-25 12:02:41 -05:00
// Define how to convert your custom scalar into a primitive type.
2020-04-20 15:24:09 -05:00
fn resolve(& self) -> Value {
2019-06-25 12:02:41 -05:00
Value::scalar(self.to_string())
2019-03-23 08:08:09 -05:00
}
2019-06-25 12:02:41 -05:00
// Define how to parse a primitive type into your custom scalar.
2020-04-20 15:24:09 -05:00
fn from_input_value(v: & InputValue) -> Option< Date> {
2020-02-21 00:24:11 -06:00
v.as_scalar_value()
.and_then(|v| v.as_str())
.and_then(|s| s.parse().ok())
2019-03-23 08:08:09 -05:00
}
2019-06-25 12:02:41 -05:00
// Define how to parse a string value.
2020-04-20 15:24:09 -05:00
fn from_str< 'a> (value: ScalarToken< 'a> ) -> ParseScalarResult< 'a, S> {
2019-06-25 12:02:41 -05:00
< String as ParseScalarValue< S> > ::from_str(value)
2019-03-23 08:08:09 -05:00
}
2020-04-20 15:24:09 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#unions" id = "unions" > < h1 > Unions< / h1 > < / a >
2020-10-07 03:24:29 -05:00
< 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 >
2020-06-04 03:28:07 -05:00
< ul >
< li > < code > #[derive(GraphQLUnion)]< / code > macro for enums and structs.< / li >
< li > < code > #[graphql_union]< / code > for traits.< / li >
< / ul >
2020-10-06 05:18:31 -05:00
< a class = "header" href = "#enums-1" id = "enums-1" > < h2 > Enums< / h2 > < / a >
2020-06-04 03:28:07 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-10-06 05:18:31 -05:00
# extern crate derive_more;
2020-06-04 03:28:07 -05:00
use derive_more::From;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Droid {
id: String,
primary_function: String,
}
2020-06-04 03:28:07 -05:00
#[derive(From, GraphQLUnion)]
enum Character {
Human(Human),
Droid(Droid),
}
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#ignoring-enum-variants" id = "ignoring-enum-variants" > < h3 > Ignoring enum variants< / h3 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-10-06 05:18:31 -05:00
# extern crate derive_more;
2020-07-18 20:35:25 -05:00
# use std::marker::PhantomData;
2020-06-04 03:28:07 -05:00
use derive_more::From;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
struct Human {
id: String,
home_planet: String,
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
#[derive(From, GraphQLUnion)]
enum Character< S> {
Human(Human),
Droid(Droid),
#[from(ignore)]
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
_State(PhantomData< S> ),
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#external-resolver-functions" id = "external-resolver-functions" > < h3 > External resolver functions< / h3 > < / a >
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# extern crate juniper;
2020-06-04 03:28:07 -05:00
use juniper::{GraphQLObject, GraphQLUnion};
2019-03-23 08:08:09 -05:00
2020-06-04 03:28:07 -05:00
#[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)
2019-03-23 08:08:09 -05:00
}
2020-02-21 00:24:11 -06:00
}
2020-06-04 03:28:07 -05:00
#
# fn main() {}
< / 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# extern crate juniper;
2020-06-04 03:28:07 -05:00
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,
}
2019-03-23 08:08:09 -05:00
2020-06-04 03:28:07 -05:00
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
}
}
}
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2020-06-04 03:28:07 -05:00
< a class = "header" href = "#structs" id = "structs" > < h2 > Structs< / h2 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# use std::collections::HashMap;
2020-06-04 03:28:07 -05:00
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
2019-05-14 01:52:54 -05:00
#[graphql(Context = Database)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2019-05-14 01:52:54 -05:00
#[graphql(Context = Database)]
2019-03-23 08:08:09 -05:00
struct Droid {
id: String,
primary_function: String,
}
struct Database {
humans: HashMap< String, Human> ,
droids: HashMap< String, Droid> ,
}
impl juniper::Context for Database {}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLUnion)]
#[graphql(
Context = Database,
on Human = Character::get_human,
on Droid = Character::get_droid,
)]
struct Character {
id: String,
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
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)
}
}
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#traits-1" id = "traits-1" > < h2 > Traits< / h2 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_union, GraphQLObject};
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
struct Human {
id: String,
home_planet: String,
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
#[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 }
}
2020-02-21 00:24:11 -06:00
2020-06-04 03:28:07 -05:00
impl Character for Human {
fn as_human(& self) -> Option< & Human> { Some(& self) }
2020-02-21 00:24:11 -06:00
}
2019-03-23 08:08:09 -05:00
2020-06-04 03:28:07 -05:00
impl Character for Droid {
fn as_droid(& self) -> Option< & Droid> { Some(& self) }
}
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2020-10-06 05:18:31 -05:00
< a class = "header" href = "#custom-context-1" id = "custom-context-1" > < h3 > Custom context< / h3 > < / a >
2020-10-07 03:24:29 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-06-04 03:28:07 -05:00
# use std::collections::HashMap;
use juniper::{graphql_union, GraphQLObject};
#[derive(GraphQLObject)]
2019-05-14 01:52:54 -05:00
#[graphql(Context = Database)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2019-05-14 01:52:54 -05:00
#[graphql(Context = Database)]
2019-03-23 08:08:09 -05:00
struct Droid {
id: String,
primary_function: String,
}
struct Database {
humans: HashMap< String, Human> ,
droids: HashMap< String, Droid> ,
}
impl juniper::Context for Database {}
2020-11-14 07:58:04 -06:00
#[graphql_union(context = Database)]
2020-06-04 03:28:07 -05:00
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 }
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
impl Character for Human {
fn as_human< 'db> (& self, ctx: & 'db Database) -> Option< & 'db Human> {
ctx.humans.get(& self.id)
2019-03-23 08:08:09 -05:00
}
2020-02-21 00:24:11 -06:00
}
2019-03-23 08:08:09 -05:00
2020-06-04 03:28:07 -05:00
impl Character for Droid {
fn as_droid< 'db> (& self, ctx: & 'db Database) -> Option< & 'db Droid> {
ctx.droids.get(& self.id)
}
}
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2020-10-06 05:18:31 -05:00
< a class = "header" href = "#ignoring-trait-methods-1" id = "ignoring-trait-methods-1" > < h3 > Ignoring trait methods< / h3 > < / a >
2020-06-04 03:28:07 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
use juniper::{graphql_union, GraphQLObject};
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct Droid {
id: String,
primary_function: String,
}
2020-06-04 03:28:07 -05:00
#[graphql_union]
trait Character {
fn as_human(& self) -> Option< & Human> { None }
fn as_droid(& self) -> Option< & Droid> { None }
2020-11-14 07:58:04 -06:00
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
2020-06-04 03:28:07 -05:00
fn id(& self) -> & str;
2019-03-23 08:08:09 -05:00
}
2020-06-04 03:28:07 -05:00
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() }
}
#
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#external-resolver-functions-1" id = "external-resolver-functions-1" > < h3 > External resolver functions< / h3 > < / a >
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# use std::collections::HashMap;
2020-06-04 03:28:07 -05:00
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 {}
2020-11-14 07:58:04 -06:00
#[graphql_union(context = Database)]
2020-06-04 03:28:07 -05:00
#[graphql_union(
on Human = DynCharacter::get_human,
on Droid = get_droid,
)]
trait Character {
2020-11-14 07:58:04 -06:00
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
2020-06-04 03:28:07 -05:00
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())
2019-03-23 08:08:09 -05:00
}
2020-02-21 00:24:11 -06:00
}
2019-03-23 08:08:09 -05:00
2020-06-04 03:28:07 -05:00
// 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())
}
#
2020-04-15 03:07:36 -05:00
# fn main() {}
< / code > < / pre > < / pre >
2020-10-06 05:18:31 -05:00
< a class = "header" href = "#scalarvalue-considerations-1" id = "scalarvalue-considerations-1" > < h2 > < code > ScalarValue< / code > considerations< / h2 > < / a >
2020-06-04 03:28:07 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# extern crate juniper;
2020-06-04 03:28:07 -05:00
use juniper::{DefaultScalarValue, GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
#[graphql(Scalar = DefaultScalarValue)]
2020-04-15 03:07:36 -05:00
struct Human {
id: String,
home_planet: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLObject)]
2020-04-15 03:07:36 -05:00
struct Droid {
id: String,
primary_function: String,
}
2020-06-04 03:28:07 -05:00
#[derive(GraphQLUnion)]
#[graphql(Scalar = DefaultScalarValue)] // removing this line will fail compilation
2020-04-15 03:07:36 -05:00
enum Character {
Human(Human),
Droid(Droid),
}
2020-06-04 03:28:07 -05:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
< a class = "header" href = "#schemas" id = "schemas" > < h1 > Schemas< / h1 > < / a >
2020-06-05 22:59:45 -05:00
< 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 >
2020-04-17 01:25:26 -05:00
< 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 >
2019-03-23 08:08:09 -05:00
< p > Both query and mutation objects are regular GraphQL objects, defined like any
2020-07-15 01:49:20 -05:00
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
2019-03-23 08:08:09 -05:00
and register all types it can find. This means that if you define a GraphQL
2020-07-15 01:49:20 -05:00
object somewhere but never reference it, it will not be exposed in a schema.< / p >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#the-query-root" id = "the-query-root" > < h2 > The query root< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > The query root is just a GraphQL object. You define it like any other GraphQL
2020-07-15 01:49:20 -05:00
object in Juniper, most commonly using the < code > graphql_object< / code > proc macro:< / p >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
2020-02-21 00:35:48 -06:00
struct Root;
2019-03-23 08:08:09 -05:00
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl Root {
fn userWithUsername(username: String) -> FieldResult< Option< User> > {
2019-03-23 08:08:09 -05:00
// Look up user in database...
2020-11-06 20:32:22 -06:00
# unimplemented!()
2020-02-21 00:35:48 -06:00
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
< a class = "header" href = "#mutations" id = "mutations" > < h2 > Mutations< / h2 > < / a >
2020-07-15 01:49:20 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
2020-02-21 00:35:48 -06:00
struct Mutations;
2019-03-23 08:08:09 -05:00
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl Mutations {
fn signUpUser(name: String, email: String) -> FieldResult< User> {
2019-03-23 08:08:09 -05:00
// Validate inputs and save user in database...
2020-11-06 20:32:22 -06:00
# unimplemented!()
2020-02-21 00:35:48 -06:00
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() { }
< / code > < / pre > < / pre >
2020-07-15 01:44:41 -05:00
< 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 >
2020-06-05 22:59:45 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult, RootNode,
};
2020-06-05 22:59:45 -05:00
struct Query;
2020-11-06 20:32:22 -06:00
#[graphql_object]
2020-06-05 22:59:45 -05:00
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 >
2020-04-17 01:25:26 -05:00
<!-- TODO: Fix This URL when the EmptySubscription become available in the Documentation -->
[EmptySubscription]: https://docs.rs/juniper/0.14.2/juniper/struct.EmptySubscription.html
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#adding-a-server" id = "adding-a-server" > < h1 > Adding A Server< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< p > To actually get a server up and running, there are multiple official and
2019-03-23 08:08:09 -05:00
third-party integration crates that will get you there.< / p >
< ul >
2020-02-21 00:35:48 -06:00
< li > < a href = "official.html" > Official Server Integrations< / a >
2019-03-23 08:08:09 -05:00
< ul >
2020-02-21 00:35:48 -06:00
< 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 >
2019-03-23 08:08:09 -05:00
< / ul >
< / li >
2020-02-21 00:35:48 -06:00
< li > < a href = "third-party.html" > Third Party Integrations< / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#official-server-integrations" id = "official-server-integrations" > < h1 > Official Server Integrations< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > Juniper provides official integration crates for several popular Rust server
libraries.< / p >
< ul >
2020-02-21 00:35:48 -06:00
< li > < a href = "warp.html" > Warp< / a > < / li >
< li > < a href = "rocket.html" > Rocket< / a > < / li >
< li > < a href = "iron.html" > Iron< / a > < / li >
2020-03-02 03:03:51 -06:00
< li > < a href = "hyper.html" > Hyper< / a > < / li >
2019-03-23 08:08:09 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#integrating-with-warp" id = "integrating-with-warp" > < h1 > Integrating with Warp< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2019-05-25 21:02:23 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#integrating-with-rocket" id = "integrating-with-rocket" > < h1 > Integrating with Rocket< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#integrating-with-iron" id = "integrating-with-iron" > < h1 > Integrating with Iron< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > < a href = "http://ironframework.io" > 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#basic-integration" id = "basic-integration" > < h2 > Basic integration< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< 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;
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object]
2019-05-14 01:52:54 -05:00
impl Root {
fn foo() -> String {
2019-03-23 08:08:09 -05:00
" Bar" .to_owned()
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# #[allow(unreachable_code, unused_variables)]
fn main() {
2019-03-23 08:08:09 -05:00
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);
2020-02-21 00:35:48 -06:00
# return;
Iron::new(chain).http(" 0.0.0.0:8080" ).unwrap();
2019-03-23 08:08:09 -05:00
}
< / code > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#accessing-data-from-the-request" id = "accessing-data-from-the-request" > < h2 > Accessing data from the request< / h2 > < / a >
2019-03-23 08:08:09 -05:00
< p > If you want to access e.g. the source IP address of the request from a field
2020-02-21 00:35:48 -06:00
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;
2019-03-23 08:08:09 -05:00
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;
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object(
2019-05-14 01:52:54 -05:00
Context = Context,
)]
impl Root {
field my_addr(context: & Context) -> String {
2019-03-23 08:08:09 -05:00
format!(" Hello, you're coming from {}" , context.remote_addr)
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# fn main() {
# let _graphql_endpoint = juniper_iron::GraphQLHandler::new(
# context_factory,
# Root,
# juniper::EmptyMutation::< Context> ::new(),
# );
# }
< / code > < / pre >
< a class = "header" href = "#integrating-with-hyper" id = "integrating-with-hyper" > < h1 > Integrating with Hyper< / h1 > < / a >
2020-02-21 00:24:11 -06:00
< p > < a href = "https://hyper.rs/" > Hyper< / a > is a fast HTTP implementation that many other Rust web frameworks
2019-03-23 08:08:09 -05:00
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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#third-party-integrations" id = "third-party-integrations" > < h1 > Third-Party Integrations< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > There are several examples or third party integration crates that are not
officially maintained by Juniper developers.< / p >
< ul >
< li > < a href = "https://github.com/actix/examples/tree/master/juniper" > Actix-Web< / a > < / li >
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#advanced-topics" id = "advanced-topics" > < h1 > Advanced Topics< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< p > The chapters below cover some more advanced scenarios.< / p >
< ul >
2020-02-21 00:35:48 -06:00
< li > < a href = "introspection.html" > Introspection< / a > < / li >
< li > < a href = "non_struct_objects.html" > Non-struct objects< / a > < / li >
2020-10-23 18:39:12 -05:00
< li > < a href = "implicit_and_explicit_null.html" > Implicit and explicit null< / a > < / li >
2020-02-21 00:35:48 -06:00
< 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 >
2020-04-17 01:25:26 -05:00
< li > < a href = "subscriptions.html" > Subscriptions< / a > < / li >
2019-03-23 08:08:09 -05:00
< / ul >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#introspection" id = "introspection" > < h1 > Introspection< / h1 > < / a >
2019-04-09 19:26:56 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#schema-introspection-output-as-json" id = "schema-introspection-output-as-json" > < h2 > Schema introspection output as JSON< / h2 > < / a >
2019-04-09 19:26:56 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(unused_variables)]
# extern crate juniper;
2020-07-18 20:35:25 -05:00
# extern crate serde_json;
2020-11-06 20:32:22 -06:00
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
GraphQLObject, IntrospectionFormat,
};
2019-04-09 19:26:56 -05:00
// Define our schema.
2020-11-06 20:32:22 -06:00
#[derive(GraphQLObject)]
2019-04-09 19:26:56 -05:00
struct Example {
id: String,
}
struct Context;
impl juniper::Context for Context {}
struct Query;
2020-11-06 20:32:22 -06:00
#[graphql_object(context = Context)]
2019-05-14 01:52:54 -05:00
impl Query {
fn example(id: String) -> FieldResult< Example> {
2019-04-09 19:26:56 -05:00
unimplemented!()
}
2019-05-14 01:52:54 -05:00
}
2019-04-09 19:26:56 -05:00
2020-03-18 22:40:03 -05:00
type Schema = juniper::RootNode<
'static,
Query,
EmptyMutation< Context> ,
EmptySubscription< Context>
> ;
2019-04-09 19:26:56 -05:00
fn main() {
// Create a context object.
let ctx = Context{};
// Run the built-in introspection query.
let (res, _errors) = juniper::introspect(
2020-03-18 22:40:03 -05:00
& Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
2019-04-09 19:26:56 -05:00
& 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#non-struct-objects" id = "non-struct-objects" > < h1 > Non-struct objects< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, GraphQLObject};
2020-07-18 20:35:25 -05:00
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2020-11-06 20:32:22 -06:00
#
#[derive(GraphQLObject)]
2019-03-23 08:08:09 -05:00
struct ValidationError {
field: String,
message: String,
}
2020-02-21 00:35:48 -06:00
# #[allow(dead_code)]
enum SignUpResult {
2019-03-23 08:08:09 -05:00
Ok(User),
Error(Vec< ValidationError> ),
}
2020-11-06 20:32:22 -06:00
#[graphql_object]
2019-05-14 01:52:54 -05:00
impl SignUpResult {
fn user(& self) -> Option< & User> {
2019-03-23 08:08:09 -05:00
match *self {
SignUpResult::Ok(ref user) => Some(user),
SignUpResult::Error(_) => None,
}
}
2019-05-14 01:52:54 -05:00
fn error(& self) -> Option< & Vec< ValidationError> > {
2019-03-23 08:08:09 -05:00
match *self {
SignUpResult::Ok(_) => None,
SignUpResult::Error(ref errors) => Some(errors)
}
}
2019-05-14 01:52:54 -05:00
}
2020-11-06 20:32:22 -06:00
#
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-10-23 18:39:12 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#objects-and-generics" id = "objects-and-generics" > < h1 > Objects and generics< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< p > Let's make a slightly more compact but generic implementation of < a href = "non_struct_objects.html" > the last
2019-03-23 08:08:09 -05:00
chapter< / a > :< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate juniper;
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2020-02-21 00:35:48 -06:00
# #[derive(juniper::GraphQLObject)] struct ForumPost { title: String }
2019-03-23 08:08:09 -05:00
#[derive(juniper::GraphQLObject)]
struct ValidationError {
field: String,
message: String,
}
2020-02-21 00:35:48 -06:00
# #[allow(dead_code)]
struct MutationResult< T> (Result< T, Vec< ValidationError> > );
2019-03-23 08:08:09 -05:00
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object(
2019-05-14 01:52:54 -05:00
name = " UserResult" ,
)]
impl MutationResult< User> {
fn user(& self) -> Option< & User> {
2019-03-23 08:08:09 -05:00
self.0.as_ref().ok()
}
2019-05-14 01:52:54 -05:00
fn error(& self) -> Option< & Vec< ValidationError> > {
2019-03-23 08:08:09 -05:00
self.0.as_ref().err()
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:24:11 -06:00
#[juniper::graphql_object(
2019-05-14 01:52:54 -05:00
name = " ForumPostResult" ,
)]
impl MutationResult< ForumPost> {
fn forum_post(& self) -> Option< & ForumPost> {
2019-03-23 08:08:09 -05:00
self.0.as_ref().ok()
}
2019-05-14 01:52:54 -05:00
fn error(& self) -> Option< & Vec< ValidationError> > {
2019-03-23 08:08:09 -05:00
self.0.as_ref().err()
}
2019-05-14 01:52:54 -05:00
}
2019-03-23 08:08:09 -05:00
2020-02-21 00:35:48 -06:00
# fn main() {}
< / code > < / pre > < / pre >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#multiple-operations-per-request" id = "multiple-operations-per-request" > < h1 > Multiple operations per request< / h1 > < / a >
2019-03-23 08:08:09 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< 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 >
2020-02-21 00:24:11 -06:00
< p > A common issue with graphql servers is how the resolvers query their datasource.
2020-04-26 01:35:21 -05:00
This issue results in a large number of unneccessary database queries or http requests.
2020-02-21 00:24:11 -06:00
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 > .
2020-05-13 21:46:58 -05:00
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 >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#what-does-it-look-like" id = "what-does-it-look-like" > < h3 > What does it look like?< / h3 > < / a >
2020-02-21 00:24:11 -06:00
< p > !FILENAME Cargo.toml< / p >
< pre > < code class = "language-toml" > [dependencies]
actix-identity = " 0.2"
actix-rt = " 1.0"
actix-web = {version = " 2.0" , features = []}
2020-05-13 21:46:58 -05:00
juniper = { git = " https://github.com/graphql-rust/juniper" }
2020-02-21 00:24:11 -06:00
futures = " 0.3"
postgres = " 0.15.2"
2020-05-13 21:46:58 -05:00
dataloader = " 0.12.0"
async-trait = " 0.1.30"
2020-02-21 00:24:11 -06:00
< / code > < / pre >
2020-05-13 21:46:58 -05:00
< pre > < code class = "language-rust ignore" > // use dataloader::cached::Loader;
use dataloader::non_cached::Loader;
use dataloader::BatchFn;
2020-02-21 00:24:11 -06:00
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;
2020-05-13 21:46:58 -05:00
#[async_trait]
2020-02-21 00:24:11 -06:00
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.
2020-05-13 21:46:58 -05:00
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
}
2020-02-21 00:24:11 -06:00
}
2020-05-13 21:46:58 -05:00
pub type CultLoader = Loader< i32, Cult, CultBatcher> ;
2020-02-21 00:24:11 -06:00
// To create a new loader
pub fn get_loader() -> CultLoader {
Loader::new(CultBatcher)
2020-05-13 21:46:58 -05:00
// 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)
2020-02-21 00:24:11 -06:00
}
#[juniper::graphql_object(Context = Context)]
impl Cult {
// your resolvers
// To call the dataloader
pub async fn cult_by_id(ctx: & Context, id: i32) -> Cult {
2020-05-13 21:46:58 -05:00
ctx.cult_loader.load(id).await
2020-02-21 00:24:11 -06:00
}
}
< / code > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#how-do-i-call-them" id = "how-do-i-call-them" > < h3 > How do I call them?< / h3 > < / a >
2020-05-13 21:46:58 -05:00
< 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 >
2020-02-21 00:35:48 -06:00
< 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 >
2020-02-21 00:24:11 -06:00
< 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
2020-05-13 21:46:58 -05:00
let res = data.execute(& st, & ctx).await;
2020-02-21 00:24:11 -06:00
let json = serde_json::to_string(& res).map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok()
.content_type(" application/json" )
.body(json))
}
< / code > < / pre >
2020-02-21 00:35:48 -06:00
< a class = "header" href = "#further-example" id = "further-example" > < h3 > Further Example:< / h3 > < / a >
2020-02-21 00:24:11 -06:00
< 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 >
2020-04-17 01:25:26 -05:00
< 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,
2020-07-15 02:06:14 -05:00
but instead of immediately returning a single answer a result is sent every time a particular event happens on the
2020-04-17 01:25:26 -05:00
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
2020-07-15 02:06:14 -05:00
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
2020-04-17 01:25:26 -05:00
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 >
2020-07-15 02:06:14 -05:00
< 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 >
2020-04-17 01:25:26 -05:00
< p > This example shows a subscription operation that returns two events, the strings < code > Hello< / code > and < code > World!< / code >
sequentially:< / p >
2020-07-18 20:35:25 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # extern crate futures;
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate tokio;
2020-11-06 20:32:22 -06:00
# use juniper::{graphql_object, graphql_subscription, FieldError};
2020-07-18 20:35:25 -05:00
# use futures::Stream;
2020-04-17 01:25:26 -05:00
# use std::pin::Pin;
2020-07-18 20:35:25 -05:00
#
2020-04-17 01:25:26 -05:00
# #[derive(Clone)]
# pub struct Database;
# impl juniper::Context for Database {}
2020-07-18 20:35:25 -05:00
2020-04-17 01:25:26 -05:00
# pub struct Query;
2020-11-06 20:32:22 -06:00
# #[graphql_object(context = Database)]
2020-04-17 01:25:26 -05:00
# impl Query {
# fn hello_world() -> & str {
# " Hello World!"
# }
# }
pub struct Subscription;
type StringStream = Pin< Box< dyn Stream< Item = Result< String, FieldError> > + Send> > ;
2020-11-06 20:32:22 -06:00
#[graphql_subscription(context = Database)]
2020-04-17 01:25:26 -05:00
impl Subscription {
async fn hello_world() -> StringStream {
let stream = tokio::stream::iter(vec![
Ok(String::from(" Hello" )),
Ok(String::from(" World!" ))
]);
Box::pin(stream)
}
}
2020-11-06 20:32:22 -06:00
#
2020-04-17 01:25:26 -05:00
# fn main () {}
< / code > < / pre > < / pre >
< a class = "header" href = "#coordinator" id = "coordinator" > < h3 > Coordinator< / h3 > < / a >
2020-07-15 02:06:14 -05:00
< 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
2020-04-17 01:25:26 -05:00
and shutdown logic.< / p >
2020-07-15 02:06:14 -05:00
< 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 >
2020-10-06 05:18:31 -05:00
< pre > < pre class = "playpen" > < code class = "language-rust" > # #![allow(dead_code)]
# extern crate futures;
2020-07-18 20:35:25 -05:00
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate serde_json;
# extern crate tokio;
2020-11-06 20:32:22 -06:00
# use juniper::{
# http::GraphQLRequest,
# graphql_object, graphql_subscription,
# DefaultScalarValue, EmptyMutation, FieldError,
# RootNode, SubscriptionCoordinator,
# };
2020-04-17 01:25:26 -05:00
# 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;
#
2020-11-06 20:32:22 -06:00
# #[graphql_object(context = Database)]
2020-04-17 01:25:26 -05:00
# impl Query {
# fn hello_world() -> & str {
# " Hello World!"
# }
# }
#
# pub struct Subscription;
#
# type StringStream = Pin< Box< dyn Stream< Item = Result< String, FieldError> > + Send> > ;
#
2020-11-06 20:32:22 -06:00
# #[graphql_subscription(context = Database)]
2020-04-17 01:25:26 -05:00
# impl Subscription {
# async fn hello_world() -> StringStream {
# let stream =
# tokio::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(
2020-11-06 20:32:22 -06:00
r#" {
2020-04-17 01:25:26 -05:00
" query" : " subscription { helloWorld }"
2020-11-06 20:32:22 -06:00
}" #,
2020-04-17 01:25:26 -05:00
)
.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());
}
}
2020-11-06 20:32:22 -06:00
#
2020-04-17 01:25:26 -05:00
# 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
2019-03-23 08:08:09 -05:00
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< div style = "clear: both" > < / div >
< / nav >
< / div >
< / div >
< nav class = "nav-wide-wrapper" aria-label = "Page navigation" >
< / 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 -->
< script type = "text/javascript" >
window.addEventListener('load', function() {
window.setTimeout(window.print, 100);
});
< / script >
< / body >
< / html >