2018-12-23 14:41:50 -06:00
# Schemas
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.
2020-04-17 01:16:00 -05:00
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.
The usage of subscriptions is a little different from the mutation and query objects, so there is a specific [section][section] that discusses them.
2018-12-23 14:41:50 -06:00
Both query and mutation objects are regular GraphQL objects, defined like any
2020-07-15 01:45:42 -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 [EmptyMutation][EmptyMutation]/[EmptySubscription][EmptySubscription].
2018-12-23 14:41:50 -06:00
2020-07-15 01:45:42 -05:00
In Juniper, the `RootNode` type represents a schema. When the schema is first created,
Juniper will traverse the entire object graph
2018-12-23 14:41:50 -06:00
and register all types it can find. This means that if you define a GraphQL
2020-07-15 01:45:42 -05:00
object somewhere but never reference it, it will not be exposed in a schema.
2018-12-23 14:41:50 -06:00
## The query root
The query root is just a GraphQL object. You define it like any other GraphQL
2020-07-15 01:45:42 -05:00
object in Juniper, most commonly using the `graphql_object` proc macro:
2018-12-23 14:41:50 -06:00
```rust
2020-07-18 20:24:33 -05:00
# extern crate juniper;
2018-12-23 14:41:50 -06:00
# use juniper::FieldResult;
2019-01-11 10:16:21 -06:00
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2018-12-23 14:41:50 -06:00
struct Root;
2019-11-15 19:43:39 -06:00
#[juniper::graphql_object]
2019-05-07 03:56:06 -05:00
impl Root {
fn userWithUsername(username: String) -> FieldResult< Option < User > > {
2018-12-23 14:41:50 -06:00
// Look up user in database...
# unimplemented!()
}
2019-05-07 03:56:06 -05:00
}
2018-12-23 14:41:50 -06:00
# fn main() { }
```
## Mutations
2020-07-15 01:45:42 -05:00
Mutations are _also_ just GraphQL objects. Each mutation is a single field
that performs some mutating side-effect such as updating a database.
2018-12-23 14:41:50 -06:00
```rust
2020-07-18 20:24:33 -05:00
# extern crate juniper;
2018-12-23 14:41:50 -06:00
# use juniper::FieldResult;
2019-01-11 10:16:21 -06:00
# #[derive(juniper::GraphQLObject)] struct User { name: String }
2018-12-23 14:41:50 -06:00
struct Mutations;
2019-11-15 19:43:39 -06:00
#[juniper::graphql_object]
2019-05-07 03:56:06 -05:00
impl Mutations {
fn signUpUser(name: String, email: String) -> FieldResult< User > {
2018-12-23 14:41:50 -06:00
// Validate inputs and save user in database...
# unimplemented!()
}
2019-05-07 03:56:06 -05:00
}
2018-12-23 14:41:50 -06:00
# fn main() { }
```
2020-04-17 01:16:00 -05:00
2020-07-15 01:41:08 -05:00
# Converting a Rust schema to the [GraphQL Schema Language][schema_language]
2020-06-05 22:43:11 -05:00
Many tools in the GraphQL ecosystem require the schema to be defined in the [GraphQL Schema Language][schema_language]. You can generate a [GraphQL Schema Language][schema_language] representation of your schema defined in Rust using the `schema-language` feature (on by default):
```rust
2020-07-18 20:24:33 -05:00
# extern crate juniper;
2020-06-05 22:43:11 -05:00
use juniper::{FieldResult, EmptyMutation, EmptySubscription, RootNode};
struct Query;
#[juniper::graphql_object]
impl Query {
fn hello(& self) -> FieldResult< & str> {
Ok("hello world")
}
}
fn main() {
// Define our schema in Rust.
let schema = RootNode::new(
Query,
EmptyMutation::< ()>::new(),
EmptySubscription::< ()>::new(),
);
// Convert the Rust schema into the GraphQL Schema Language.
let result = schema.as_schema_language();
let expected = "\
type Query {
hello: String!
}
schema {
query: Query
}
";
assert_eq!(result, expected);
}
```
Note the `schema-language` feature may be turned off if you do not need this functionality to reduce dependencies and speed up
compile times.
[schema_language]: https://graphql.org/learn/schema/#type-language
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema
[schema_approach]: https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/
2020-04-17 01:16:00 -05:00
[section]: ../advanced/subscriptions.md
[EmptyMutation]: https://docs.rs/juniper/0.14.2/juniper/struct.EmptyMutation.html
<!-- TODO: Fix This URL when the EmptySubscription become available in the Documentation -->
[EmptySubscription]: https://docs.rs/juniper/0.14.2/juniper/struct.EmptySubscription.html