2018-12-23 14:41:50 -06:00
# Quickstart
This page will give you a short introduction to the concepts in Juniper.
2020-06-05 22:43:11 -05:00
Juniper follows a [code-first approach][schema_approach] to defining GraphQL schemas. If you would like to use a [schema-first approach][schema_approach] instead, consider [juniper-from-schema][] for generating code from a schema file.
2018-12-23 14:41:50 -06:00
## Installation
!FILENAME Cargo.toml
```toml
[dependencies]
2020-09-11 03:20:27 -05:00
juniper = { git = "https://github.com/graphql-rust/juniper" }
2018-12-23 14:41:50 -06:00
```
## Schema example
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 `Option<T>` , `Vec<T>` , `Box<T>` ,
`String` , `f64` , and `i32` , references, and slices.
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:24:57 -05:00
[graphql_object][graphql_object] procedural macro that is used for declaring an object with
2018-12-23 14:41:50 -06:00
resolvers, which you will use for the `Query` and `Mutation` roots.
```rust
Make interfaces great again! (#682)
* Bootstrap
* Upd
* Bootstrap macro
* Revert stuff
* Correct PoC to compile
* Bootstrap #[graphql_interface] expansion
* Bootstrap #[graphql_interface] meta parsing
* Bootstrap #[graphql_interface] very basic code generation [skip ci]
* Upd trait code generation and fix keywords usage [skip ci]
* Expand trait impls [skip ci]
* Tune up objects [skip ci]
* Finally! Complies at least... [skip ci]
* Parse meta for fields and its arguments [skip ci]
- also, refactor and bikeshed new macros code
* Impl filling fields meta and bootstrap field resolution [skip ci]
* Poking with fields resolution [skip ci]
* Solve Rust's teen async HRTB problems [skip ci]
* Start parsing trait methods [skip ci]
* Finish parsing fields from trait methods [skip ci]
* Autodetect trait asyncness and allow to specify it [skip ci]
* Allow to autogenerate trait object alias via attribute
* Support generics in trait definition and asyncify them correctly
* Temporary disable explicit async
* Cover arguments and custom names/descriptions in tests
* Re-enable tests with explicit async and fix the codegen to satisfy it
* Check implementers are registered in schema and vice versa
* Check argument camelCases
* Test argument defaults, and allow Into coercions for them
* Re-enable markers
* Re-enable markers and relax Sized requirement on IsInputType/IsOutputType marker traits
* Revert 'juniper_actix' fmt
* Fix missing marks for object
* Fix subscriptions marks
* Deduce result type correctly via traits
* Final fixes
* Fmt
* Restore marks checking
* Support custom ScalarValue
* Cover deprecations with tests
* Impl dowcasting via methods
* Impl dowcasting via external functions
* Support custom context, vol. 1
* Support custom context, vol. 2
* Cover fallible field with test
* Impl explicit generic ScalarValue, vol.1
* Impl explicit generic ScalarValue, vol.2
* Allow passing executor into methods
* Generating enum, vol.1
* Generating enum, vol.2
* Generating enum, vol.3
* Generating enum, vol.3
* Generating enum, vol.4
* Generating enum, vol.5
* Generating enum, vol.6
* Generating enum, vol.7
* Generating enum, vol.8
* Refactor juniper stuff
* Fix juniper tests, vol.1
* Fix juniper tests, vol.2
* Polish 'juniper' crate changes, vol.1
* Polish 'juniper' crate changes, vol.2
* Remove redundant stuf
* Polishing 'juniper_codegen', vol.1
* Polishing 'juniper_codegen', vol.2
* Polishing 'juniper_codegen', vol.3
* Polishing 'juniper_codegen', vol.4
* Polishing 'juniper_codegen', vol.5
* Polishing 'juniper_codegen', vol.6
* Polishing 'juniper_codegen', vol.7
* Polishing 'juniper_codegen', vol.8
* Polishing 'juniper_codegen', vol.9
* Fix other crates tests and make Clippy happier
* Fix examples
* Add codegen failure tests, vol. 1
* Add codegen failure tests, vol. 2
* Add codegen failure tests, vol.3
* Fix codegen failure tests accordingly to latest nightly Rust
* Fix codegen when interface has no implementers
* Fix warnings in book tests
* Describing new interfaces in Book, vol.1
Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
2020-10-06 02:21:01 -05:00
# #![allow(unused_variables)]
2020-07-18 20:24:33 -05:00
# extern crate juniper;
2020-11-06 20:15:18 -06:00
# use std::fmt::Display;
use juniper::{
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
GraphQLInputObject, GraphQLObject, ScalarValue,
};
#
2018-12-23 14:41:50 -06:00
# struct DatabasePool;
# impl DatabasePool {
# fn get_connection(&self) -> FieldResult<DatabasePool> { Ok(DatabasePool) }
2019-04-03 01:36:10 -05:00
# fn find_human(&self, _id: &str) -> FieldResult<Human> { Err("")? }
# fn insert_human(&self, _human: &NewHuman) -> FieldResult<Human> { Err("")? }
2018-12-23 14:41:50 -06:00
# }
2020-11-06 20:15:18 -06:00
#[derive(GraphQLEnum)]
2018-12-23 14:41:50 -06:00
enum Episode {
NewHope,
Empire,
Jedi,
}
2020-11-06 20:15:18 -06:00
#[derive(GraphQLObject)]
#[graphql(description = "A humanoid creature in the Star Wars universe")]
2018-12-23 14:41:50 -06: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:15:18 -06:00
#[derive(GraphQLInputObject)]
#[graphql(description = "A humanoid creature in the Star Wars universe")]
2018-12-23 14:41:50 -06: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-13 11:33:45 -05:00
// object macro.
2018-12-23 14:41:50 -06: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:15:18 -06:00
#[graphql_object(
2019-05-07 03:56:06 -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:15:18 -06:00
context = Context,
2019-05-07 03:56:06 -05:00
)]
impl Query {
fn apiVersion() -> & str {
2018-12-23 14:41:50 -06:00
"1.0"
}
// Arguments to resolvers can either be simple types or input objects.
2019-05-07 03:56:06 -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 > {
2018-12-23 14:41:50 -06: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-07 03:56:06 -05:00
}
// Now, we do the same for our Mutation type.
2018-12-23 14:41:50 -06:00
struct Mutation;
2020-11-06 20:15:18 -06:00
#[graphql_object(
context = Context,
2018-12-23 14:41:50 -06:00
2020-11-06 20:15:18 -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())?;
2018-12-23 14:41:50 -06:00
Ok(human)
}
2019-05-07 03:56:06 -05:00
}
2018-12-23 14:41:50 -06:00
2020-07-18 18:29:48 -05:00
// A root schema consists of a query, a mutation, and a subscription.
2018-12-23 14:41:50 -06:00
// Request queries can be executed against a RootNode.
2020-03-18 22:31:36 -05:00
type Schema = juniper::RootNode< 'static, Query, Mutation, EmptySubscription< Context > >;
2020-11-06 20:15:18 -06:00
#
2019-04-03 01:36:10 -05:00
# fn main() {
2020-03-18 22:31:36 -05:00
# let _ = Schema::new(Query, Mutation{}, EmptySubscription::new());
2019-04-03 01:36:10 -05:00
# }
2018-12-23 14:41:50 -06:00
```
We now have a very simple but functional schema for a GraphQL server!
2019-04-03 01:36:10 -05:00
To actually serve the schema, see the guides for our various [server integrations ](./servers/index.md ).
2019-01-11 10:16:21 -06:00
2020-07-16 02:24:57 -05:00
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:
2018-12-23 14:41:50 -06:00
## Executor
You can invoke `juniper::execute` directly to run a GraphQL query:
```rust
2019-01-11 10:16:21 -06:00
# // Only needed due to 2018 edition because the macro is not accessible.
2019-04-22 19:19:06 -05:00
# #[macro_use] extern crate juniper;
2020-11-06 20:15:18 -06:00
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
2020-12-28 11:45:40 -06:00
GraphQLEnum, Variables, graphql_value,
2020-11-06 20:15:18 -06:00
};
2019-05-07 03:56:06 -05:00
2020-11-06 20:15:18 -06:00
#[derive(GraphQLEnum, Clone, Copy)]
2018-12-23 14:41:50 -06:00
enum Episode {
NewHope,
Empire,
Jedi,
}
2019-05-07 03:56:06 -05:00
// Arbitrary context data.
struct Ctx(Episode);
impl juniper::Context for Ctx {}
2018-12-23 14:41:50 -06:00
struct Query;
2020-11-06 20:15:18 -06:00
#[graphql_object(context = Ctx)]
2019-05-07 03:56:06 -05:00
impl Query {
fn favoriteEpisode(context: & Ctx) -> FieldResult< Episode > {
Ok(context.0)
2018-12-23 14:41:50 -06:00
}
2019-05-07 03:56:06 -05:00
}
2018-12-23 14:41:50 -06:00
2020-07-18 18:29:48 -05:00
// A root schema consists of a query, a mutation, and a subscription.
2018-12-23 14:41:50 -06:00
// Request queries can be executed against a RootNode.
2020-03-18 22:31:36 -05:00
type Schema = juniper::RootNode< 'static, Query, EmptyMutation< Ctx > , EmptySubscription< Ctx > >;
2018-12-23 14:41:50 -06:00
fn main() {
// Create a context object.
let ctx = Ctx(Episode::NewHope);
// Run the executor.
2020-03-10 00:40:26 -05:00
let (res, _errors) = juniper::execute_sync(
2018-12-23 14:41:50 -06:00
"query { favoriteEpisode }",
None,
2020-03-18 22:31:36 -05:00
& Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
2018-12-23 14:41:50 -06:00
& Variables::new(),
& ctx,
).unwrap();
// Ensure the value matches.
assert_eq!(
2019-01-11 10:16:21 -06:00
res,
graphql_value!({
2019-04-03 01:36:10 -05:00
"favoriteEpisode": "NEW_HOPE",
2019-01-11 10:16:21 -06:00
})
2018-12-23 14:41:50 -06:00
);
}
```
2020-06-05 22:43:11 -05:00
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema
[schema_approach]: https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/
2018-12-23 14:41:50 -06:00
[hyper]: servers/hyper.md
[warp]: servers/warp.md
[rocket]: servers/rocket.md
[iron]: servers/iron.md
[tutorial]: ./tutorial.html
2020-07-16 02:24:57 -05:00
[graphql_object]: https://docs.rs/juniper/latest/juniper/macro.graphql_object.html