Compare commits

..

1 commit

Author SHA1 Message Date
tyranron
44f4202323
Add badge to juniper_axum 2023-11-27 16:03:33 +01:00
243 changed files with 7374 additions and 11749 deletions

View file

@ -107,7 +107,7 @@ jobs:
- { feature: chrono-clock, crate: juniper }
- { feature: chrono-tz, crate: juniper }
- { feature: expose-test-schema, crate: juniper }
- { feature: jiff, crate: juniper }
- { feature: graphql-parser, crate: juniper }
- { feature: rust_decimal, crate: juniper }
- { feature: schema-language, crate: juniper }
- { feature: time, crate: juniper }
@ -145,7 +145,7 @@ jobs:
strategy:
fail-fast: false
matrix:
msrv: ["1.75.0"]
msrv: ["1.73.0"]
crate:
- juniper_codegen
- juniper
@ -153,17 +153,13 @@ jobs:
- juniper_graphql_ws
- juniper_actix
- juniper_axum
#- juniper_hyper
- juniper_hyper
- juniper_rocket
- juniper_warp
os:
- ubuntu
- macOS
- windows
include:
- { msrv: "1.79.0", crate: "juniper_hyper", os: "ubuntu" }
- { msrv: "1.79.0", crate: "juniper_hyper", os: "macOS" }
- { msrv: "1.79.0", crate: "juniper_hyper", os: "windows" }
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v4
@ -179,7 +175,6 @@ jobs:
- run: make test.cargo crate=${{ matrix.crate }}
package:
name: check (package)
if: ${{ startsWith(github.ref, 'refs/tags/juniper') }}
runs-on: ubuntu-latest
steps:
@ -195,7 +190,7 @@ jobs:
text: ${{ github.ref }}
regex: '^refs/tags/(([a-z_]+)-v([0-9]+\.[0-9]+\.[0-9]+(-.+)?))$'
- run: cargo package -p ${{ steps.tag.outputs.group2 }} --all-features
- run: cargo package -p ${{ steps.tag.outputs.group2 }}
test:
strategy:
@ -264,7 +259,7 @@ jobs:
- uses: dtolnay/rust-toolchain@v1
with:
toolchain: ${{ matrix.toolchain }}
- uses: peaceiris/actions-mdbook@v2
- uses: peaceiris/actions-mdbook@v1
- run: make test.book
@ -274,10 +269,9 @@ jobs:
matrix:
crate:
- juniper
- juniper_axum
target:
- wasm32-unknown-unknown
- wasm32-wasip1
- wasm32-wasi
toolchain:
- stable
- beta
@ -290,13 +284,10 @@ jobs:
toolchain: ${{ matrix.toolchain }}
target: ${{ matrix.target }}
- name: Switch Cargo workspace to `resolver = "2"`
run: sed -i 's/resolver = "1"/resolver = "2"/' Cargo.toml
- run: cargo check --target ${{ matrix.target }} -p ${{ matrix.crate }}
${{ (matrix.crate == 'juniper' && matrix.target == 'wasm32-unknown-unknown')
&& '--features js'
|| '' }}
${{ matrix.target == 'wasm32-unknown-unknown'
&& '--features js'
|| '' }}
@ -323,12 +314,12 @@ jobs:
text: ${{ github.ref }}
regex: '^refs/tags/(([a-z_]+)-v([0-9]+\.[0-9]+\.[0-9]+(-.+)?))$'
- run: cargo publish -p ${{ steps.tag.outputs.group2 }} --all-features
- run: cargo publish -p ${{ steps.tag.outputs.group2 }}
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATESIO_TOKEN }}
release-check:
name: check (release)
name: check release
if: ${{ !startsWith(github.ref, 'refs/tags/juniper') }}
strategy:
fail-fast: false
@ -392,7 +383,7 @@ jobs:
>> $GITHUB_OUTPUT
- name: Create GitHub release
uses: softprops/action-gh-release@v2
uses: softprops/action-gh-release@v1
with:
name: ${{ steps.tag.outputs.group2 }} ${{ steps.tag.outputs.group3 }}
body: |
@ -410,19 +401,19 @@ jobs:
deploy-book:
name: deploy (Book)
if: ${{ github.ref == 'refs/heads/master'
|| startsWith(github.ref, 'refs/tags/juniper') }}
|| startsWith(github.ref, 'refs/tags/juniper@') }}
needs: ["codespell", "test", "test-book"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: peaceiris/actions-mdbook@v2
- uses: peaceiris/actions-mdbook@v1
- run: make book.build out=gh-pages${{ (github.ref == 'refs/heads/master'
&& '/master')
|| '' }}
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
keep_files: true

View file

@ -1,10 +1,6 @@
BSD 2-Clause License
Copyright (c) 2016-2025 Magnus Hallin <mhallin@fastmail.com>,
Christoph Herzog <chris@theduke.at>,
Christian Legnitto <christian@legnitto.com>,
Ilya Solovyiov <ilya.solovyiov@gmail.com>,
Kai Ren <tyranron@gmail.com>
Copyright (c) 2016, Magnus Hallin
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -73,7 +73,6 @@ your Schemas automatically.
- [url][url]
- [chrono][chrono]
- [chrono-tz][chrono-tz]
- [jiff][jiff]
- [time][time]
- [bson][bson]
@ -109,10 +108,10 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
[rocket_examples]: https://github.com/graphql-rust/juniper/tree/master/juniper_rocket/examples
[hyper]: https://hyper.rs
[rocket]: https://rocket.rs
[book]: https://graphql-rust.github.io/juniper
[book]: https://graphql-rust.github.io
[book_master]: https://graphql-rust.github.io/juniper/master
[book_index]: https://graphql-rust.github.io/juniper
[book_quickstart]: https://graphql-rust.github.io/juniper/quickstart.html
[book_index]: https://graphql-rust.github.io
[book_quickstart]: https://graphql-rust.github.io/quickstart.html
[docsrs]: https://docs.rs/juniper
[warp]: https://github.com/seanmonstar/warp
[warp_examples]: https://github.com/graphql-rust/juniper/tree/master/juniper_warp/examples
@ -120,7 +119,6 @@ Juniper has not reached 1.0 yet, thus some API instability should be expected.
[url]: https://crates.io/crates/url
[chrono]: https://crates.io/crates/chrono
[chrono-tz]: https://crates.io/crates/chrono-tz
[jiff]: https://crates.io/crates/jiff
[time]: https://crates.io/crates/time
[bson]: https://crates.io/crates/bson
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema

View file

@ -6,7 +6,6 @@ authors = ["Christoph Herzog <chris@theduke.at>"]
publish = false
[dependencies]
dataloader = "0.18" # for Book only
futures = "0.3"
juniper = { path = "../juniper" }

View file

@ -1,11 +1,8 @@
[book]
title = "Juniper Book"
title = "Juniper Book (GraphQL server for Rust)"
description = "User guide for Juniper (GraphQL server library for Rust)."
language = "en"
multilingual = false
authors = [
"Kai Ren (@tyranron)",
]
src = "src"
[build]
@ -13,7 +10,7 @@ build-dir = "_rendered"
create-missing = false
[output.html]
git_repository_url = "https://github.com/graphql-rust/juniper"
git_repository_url = "https://github.com/graphql-rs/juniper"
[rust]
edition = "2021"

73
book/src/README.md Normal file
View file

@ -0,0 +1,73 @@
# Juniper
Juniper is a [GraphQL] server library for Rust. Build type-safe and fast API
servers with minimal boilerplate and configuration.
[GraphQL][graphql] is a data query language developed by Facebook intended to
serve mobile and web application frontends.
_Juniper_ 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.
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 [Hyper][hyper], [Rocket], and [Warp][warp] frameworks, including
embedded [Graphiql][graphiql] for easy debugging.
- [Cargo crate](https://crates.io/crates/juniper)
- [API Reference][docsrs]
## Features
Juniper supports the full GraphQL query language according to the
[specification (October 2021)][graphql_spec], including interfaces, unions, schema
introspection, and validations.
It does not, however, support the schema language.
As an exception to other GraphQL libraries for other languages, Juniper builds
non-null types by default. A field of type `Vec<Episode>` will be converted into
`[Episode!]!`. The corresponding Rust type for e.g. `[Episode]` would be
`Option<Vec<Option<Episode>>>`.
## Integrations
### Data types
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.
- [uuid][uuid]
- [url][url]
- [chrono][chrono]
- [bson][bson]
### Web Frameworks
- [hyper][hyper]
- [rocket][rocket]
- [warp][warp]
## API Stability
Juniper has not reached 1.0 yet, thus some API instability should be expected.
[graphql]: http://graphql.org
[graphiql]: https://github.com/graphql/graphiql
[graphql_spec]: https://spec.graphql.org/October2021
[test_schema_rs]: https://github.com/graphql-rust/juniper/blob/master/juniper/src/tests/schema.rs
[tokio]: https://github.com/tokio-rs/tokio
[hyper_examples]: https://github.com/graphql-rust/juniper/tree/master/juniper_hyper/examples
[rocket_examples]: https://github.com/graphql-rust/juniper/tree/master/juniper_rocket/examples
[hyper]: https://hyper.rs
[rocket]: https://rocket.rs
[book]: https://graphql-rust.github.io
[book_quickstart]: https://graphql-rust.github.io/quickstart.html
[docsrs]: https://docs.rs/juniper
[warp]: https://github.com/seanmonstar/warp
[warp_examples]: https://github.com/graphql-rust/juniper/tree/master/juniper_warp/examples
[uuid]: https://crates.io/crates/uuid
[url]: https://crates.io/crates/url
[chrono]: https://crates.io/crates/chrono
[bson]: https://crates.io/crates/bson

View file

@ -1,28 +1,39 @@
# Summary
- [Introduction](introduction.md)
- [Introduction](README.md)
- [Quickstart](quickstart.md)
- [Type system](types/index.md)
- [Objects](types/objects/index.md)
- [Complex fields](types/objects/complex_fields.md)
- [Context](types/objects/context.md)
- [Error handling](types/objects/error/index.md)
- [Field errors](types/objects/error/field.md)
- [Schema errors](types/objects/error/schema.md)
- [Generics](types/objects/generics.md)
- [Interfaces](types/interfaces.md)
- [Unions](types/unions.md)
- [Type System](types/index.md)
- [Defining objects](types/objects/defining_objects.md)
- [Complex fields](types/objects/complex_fields.md)
- [Using contexts](types/objects/using_contexts.md)
- [Error handling](types/objects/error_handling.md)
- [Other types](types/other-index.md)
- [Enums](types/enums.md)
- [Interfaces](types/interfaces.md)
- [Input objects](types/input_objects.md)
- [Scalars](types/scalars.md)
- [Schema](schema/index.md)
- [Subscriptions](schema/subscriptions.md)
- [Introspection](schema/introspection.md)
- [Serving](serve/index.md)
- [Batching](serve/batching.md)
- [Unions](types/unions.md)
- [Schemas and mutations](schema/schemas_and_mutations.md)
- [Adding A Server](servers/index.md)
- [Official Server Integrations](servers/official.md) - [Hyper](servers/hyper.md)
- [Warp](servers/warp.md)
- [Rocket](servers/rocket.md)
- [Hyper](servers/hyper.md)
- [Third Party Integrations](servers/third-party.md)
- [Advanced Topics](advanced/index.md)
- [Implicit and explicit `null`](advanced/implicit_and_explicit_null.md)
- [N+1 problem](advanced/n_plus_1.md)
- [DataLoader](advanced/dataloader.md)
- [Look-ahead](advanced/lookahead.md)
- [Eager loading](advanced/eager_loading.md)
- [Introspection](advanced/introspection.md)
- [Non-struct objects](advanced/non_struct_objects.md)
- [Implicit and explicit null](advanced/implicit_and_explicit_null.md)
- [Objects and generics](advanced/objects_and_generics.md)
- [Multiple operations per request](advanced/multiple_ops_per_request.md)
- [Dataloaders](advanced/dataloaders.md)
- [Subscriptions](advanced/subscriptions.md)
# - [Context switching]
# - [Dynamic type system]

View file

@ -1,198 +0,0 @@
DataLoader
==========
DataLoader pattern, named after the correspondent [`dataloader` NPM package][0], represents a mechanism of batching and caching data requests in a delayed manner for solving the [N+1 problem](n_plus_1.md).
> A port of the "Loader" API originally developed by [@schrockn] at Facebook in 2010 as a simplifying force to coalesce the sundry key-value store back-end APIs which existed at the time. At Facebook, "Loader" became one of the implementation details of the "Ent" framework, a privacy-aware data entity loading and caching layer within web server product code. This ultimately became the underpinning for Facebook's GraphQL server implementation and type definitions.
In [Rust] ecosystem, DataLoader pattern is introduced with the [`dataloader` crate][1], naturally usable with [Juniper].
Let's remake our [example of N+1 problem](n_plus_1.md), so it's solved by applying the DataLoader pattern:
```rust
# extern crate anyhow;
# extern crate dataloader;
# extern crate juniper;
# use std::{collections::HashMap, sync::Arc};
# use anyhow::anyhow;
# use dataloader::non_cached::Loader;
# use juniper::{graphql_object, GraphQLObject};
#
# type CultId = i32;
# type UserId = i32;
#
# struct Repository;
#
# impl Repository {
# async fn load_cults_by_ids(&self, cult_ids: &[CultId]) -> anyhow::Result<HashMap<CultId, Cult>> { unimplemented!() }
# async fn load_all_persons(&self) -> anyhow::Result<Vec<Person>> { unimplemented!() }
# }
#
struct Context {
repo: Repository,
cult_loader: CultLoader,
}
impl juniper::Context for Context {}
#[derive(Clone, GraphQLObject)]
struct Cult {
id: CultId,
name: String,
}
struct CultBatcher {
repo: Repository,
}
// Since `BatchFn` doesn't provide any notion of fallible loading, like
// `try_load()` returning `Result<HashMap<K, V>, E>`, we handle possible
// errors as loaded values and unpack them later in the resolver.
impl dataloader::BatchFn<CultId, Result<Cult, Arc<anyhow::Error>>> for CultBatcher {
async fn load(
&mut self,
cult_ids: &[CultId],
) -> HashMap<CultId, Result<Cult, Arc<anyhow::Error>>> {
// Effectively performs the following SQL query:
// SELECT id, name FROM cults WHERE id IN (${cult_id1}, ${cult_id2}, ...)
match self.repo.load_cults_by_ids(cult_ids).await {
Ok(found_cults) => {
found_cults.into_iter().map(|(id, cult)| (id, Ok(cult))).collect()
}
// One could choose a different strategy to deal with fallible loads,
// like consider values that failed to load as absent, or just panic.
// See cksac/dataloader-rs#35 for details:
// https://github.com/cksac/dataloader-rs/issues/35
Err(e) => {
// Since `anyhow::Error` doesn't implement `Clone`, we have to
// work around here.
let e = Arc::new(e);
cult_ids.iter().map(|k| (k.clone(), Err(e.clone()))).collect()
}
}
}
}
type CultLoader = Loader<CultId, Result<Cult, Arc<anyhow::Error>>, CultBatcher>;
fn new_cult_loader(repo: Repository) -> CultLoader {
CultLoader::new(CultBatcher { repo })
// Usually a `Loader` will coalesce all individual loads which occur
// within a single frame of execution before calling a `BatchFn::load()`
// with all the collected keys. However, sometimes this behavior is not
// desirable or optimal (perhaps, a request is expected to be spread out
// over a few subsequent ticks).
// A larger yield count will allow more keys to be appended to the batch,
// but will wait longer before the actual load. For more details see:
// https://github.com/cksac/dataloader-rs/issues/12
// https://github.com/graphql/dataloader#batch-scheduling
.with_yield_count(100)
}
struct Person {
id: UserId,
name: String,
cult_id: CultId,
}
#[graphql_object]
#[graphql(context = Context)]
impl Person {
fn id(&self) -> CultId {
self.id
}
fn name(&self) -> &str {
self.name.as_str()
}
async fn cult(&self, ctx: &Context) -> anyhow::Result<Cult> {
ctx.cult_loader
// Here, we don't run the `CultBatcher::load()` eagerly, but rather
// only register the `self.cult_id` value in the `cult_loader` and
// wait for other concurrent resolvers to do the same.
// The actual batch loading happens once all the resolvers register
// their IDs and there is nothing more to execute.
.try_load(self.cult_id)
.await
// The outer error is the `io::Error` returned by `try_load()` if
// no value is present in the `HashMap` for the specified
// `self.cult_id`, meaning that there is no `Cult` with such ID
// in the `Repository`.
.map_err(|_| anyhow!("No cult exists for ID `{}`", self.cult_id))?
// The inner error is the one returned by the `CultBatcher::load()`
// if the `Repository::load_cults_by_ids()` fails, meaning that
// running the SQL query failed.
.map_err(|arc_err| anyhow!("{arc_err}"))
}
}
struct Query;
#[graphql_object]
#[graphql(context = Context)]
impl Query {
async fn persons(ctx: &Context) -> anyhow::Result<Vec<Person>> {
// Effectively performs the following SQL query:
// SELECT id, name, cult_id FROM persons
ctx.repo.load_all_persons().await
}
}
fn main() {
}
```
And now, performing a [GraphQL query which lead to N+1 problem](n_plus_1.md)
```graphql
query {
persons {
id
name
cult {
id
name
}
}
}
```
will lead to efficient [SQL] queries, just as expected:
```sql
SELECT id, name, cult_id FROM persons;
SELECT id, name FROM cults WHERE id IN (1, 2, 3, 4);
```
## Caching
[`dataloader::cached`] provides a [memoization][2] cache: after `BatchFn::load()` is called once with given keys, the resulting values are cached to eliminate redundant loads.
DataLoader caching does not replace [Redis], [Memcached], 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][3].
> **WARNING**: A DataLoader should be created per-request to avoid risk of bugs where one client is able to load cached/batched data from another client outside its authenticated scope. Creating a DataLoader within an individual resolver will prevent batching from occurring and will nullify any benefits of it.
## Full example
For a full example using DataLoaders in [Juniper] check out the [`jayy-lmao/rust-graphql-docker` repository][4].
[`dataloader::cached`]: https://docs.rs/dataloader/latest/dataloader/cached/index.html
[@schrockn]: https://github.com/schrockn
[Juniper]: https://docs.rs/juniper
[Memcached]: https://memcached.org
[Redis]: https://redis.io
[Rust]: https://www.rust-lang.org
[SQL]: https://en.wikipedia.org/wiki/SQL
[0]: https://github.com/graphql/dataloader
[1]: https://docs.rs/crate/dataloader
[2]: https://en.wikipedia.org/wiki/Memoization
[3]: https://github.com/graphql/dataloader#caching
[4]: https://github.com/jayy-lmao/rust-graphql-docker

View file

@ -0,0 +1,194 @@
# Avoiding the N+1 Problem With Dataloaders
A common issue with graphql servers is how the resolvers query their datasource.
This issue results in a large number of unnecessary database queries or http requests.
Say you were wanting to list a bunch of cults people were in
```graphql
query {
persons {
id
name
cult {
id
name
}
}
}
```
What would be executed by a SQL database would be:
```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;
# ...
```
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.
A common solution to this is to introduce a **dataloader**.
This can be done with Juniper using the crate [cksac/dataloader-rs](https://github.com/cksac/dataloader-rs), which has two types of dataloaders; cached and non-cached.
#### Cached Loader
DataLoader provides a memoization cache, after .load() is called once with a given key, the resulting value is cached to eliminate redundant loads.
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. [(read more)](https://github.com/graphql/dataloader#caching)
### What does it look like?
!FILENAME Cargo.toml
```toml
[dependencies]
actix-identity = "0.4.0-beta.4"
actix-rt = "1.0"
actix-web = "2.0"
async-trait = "0.1.30"
dataloader = "0.12.0"
futures = "0.3"
juniper = "0.16.0"
postgres = "0.15.2"
```
```rust, ignore
// use dataloader::cached::Loader;
use dataloader::non_cached::Loader;
use dataloader::BatchFn;
use std::collections::HashMap;
use postgres::{Connection, TlsMode};
use std::env;
pub fn get_db_conn() -> Connection {
let pg_connection_string = env::var("DATABASE_URI").expect("need a db uri");
println!("Connecting to {pg_connection_string}");
let conn = Connection::connect(&pg_connection_string[..], TlsMode::None).unwrap();
println!("Connection is fine");
conn
}
#[derive(Debug, Clone)]
pub struct Cult {
pub id: i32,
pub name: String,
}
pub fn get_cult_by_ids(hashmap: &mut HashMap<i32, Cult>, ids: Vec<i32>) {
let conn = get_db_conn();
for row in &conn
.query("SELECT id, name FROM cults WHERE id = ANY($1)", &[&ids])
.unwrap()
{
let cult = Cult {
id: row.get(0),
name: row.get(1),
};
hashmap.insert(cult.id, cult);
}
}
pub struct CultBatcher;
#[async_trait]
impl BatchFn<i32, Cult> for CultBatcher {
// A hashmap is used, as we need to return an array which maps each original key to a Cult.
async fn load(&self, keys: &[i32]) -> HashMap<i32, Cult> {
println!("load cult batch {keys:?}");
let mut cult_hashmap = HashMap::new();
get_cult_by_ids(&mut cult_hashmap, keys.to_vec());
cult_hashmap
}
}
pub type CultLoader = Loader<i32, Cult, CultBatcher>;
// To create a new loader
pub fn get_loader() -> CultLoader {
Loader::new(CultBatcher)
// Usually a DataLoader will coalesce all individual loads which occur
// within a single frame of execution before calling your batch function with all requested keys.
// However sometimes this behavior is not desirable or optimal.
// Perhaps you expect requests to be spread out over a few subsequent ticks
// See: https://github.com/cksac/dataloader-rs/issues/12
// More info: https://github.com/graphql/dataloader#batch-scheduling
// A larger yield count will allow more requests to append to batch but will wait longer before actual load.
.with_yield_count(100)
}
#[juniper::graphql_object(Context = Context)]
impl Cult {
// your resolvers
// To call the dataloader
pub async fn cult_by_id(ctx: &Context, id: i32) -> Cult {
ctx.cult_loader.load(id).await
}
}
```
### How do I call them?
Once created, a dataloader has the async functions `.load()` and `.load_many()`.
In the above example `cult_loader.load(id: i32).await` returns `Cult`. If we had used `cult_loader.load_many(Vec<i32>).await` it would have returned `Vec<Cult>`.
### Where do I create my dataloaders?
**Dataloaders** 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.
For example:
_When you declare your context_
```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
}
}
}
```
_Your handler for GraphQL (Note: instantiating context here keeps it per-request)_
```rust, ignore
pub async fn graphql(
st: web::Data<Arc<Schema>>,
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
// Context setup
let cult_loader = get_loader();
let ctx = Context::new(cult_loader);
// Execute
let res = data.execute(&st, &ctx).await;
let json = serde_json::to_string(&res).map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(json))
}
```
### Further Example:
For a full example using Dataloaders and Context check out [jayy-lmao/rust-graphql-docker](https://github.com/jayy-lmao/rust-graphql-docker).

View file

@ -1,280 +0,0 @@
Eager loading
=============
As a further evolution of the [dealing with the N+1 problem via look-ahead](lookahead.md#n1-problem), we may systematically remodel [Rust] types mapping to [GraphQL] ones in the way to encourage doing eager preloading of data for its [fields][0] and using the already preloaded data when resolving a particular [field][0].
At the moment, this approach is represented with the [`juniper-eager-loading`] crate for [Juniper].
> **NOTE**: Since this library requires [`juniper-from-schema`], it's best first to become familiar with it.
<!-- TODO: Provide example of solving the problem from "N+1 chapter" once `juniper-eager-loading` support the latest `juniper`. -->
From ["How this library works at a high level"][11] and ["A real example"][12] sections of [`juniper-eager-loading`] documentation:
> ### How this library works at a high level
>
> If you have a GraphQL type like this
>
> ```graphql
> type User {
> id: Int!
> country: Country!
> }
> ```
>
> You might create the corresponding Rust model type like this:
>
> ```rust
> struct User {
> id: i32,
> country_id: i32,
> }
> ```
>
> However this approach has one big issue. How are you going to resolve the field `User.country`
> without doing a database query? All the resolver has access to is a `User` with a `country_id`
> field. It can't get the country without loading it from the database...
>
> Fundamentally these kinds of model structs don't work for eager loading with GraphQL. So
> this library takes a different approach.
>
> What if we created separate structs for the database models and the GraphQL models? Something
> like this:
>
> ```rust
> # fn main() {}
> #
> mod models {
> pub struct User {
> id: i32,
> country_id: i32
> }
>
> pub struct Country {
> id: i32,
> }
> }
>
> struct User {
> user: models::User,
> country: HasOne<Country>,
> }
>
> struct Country {
> country: models::Country
> }
>
> enum HasOne<T> {
> Loaded(T),
> NotLoaded,
> }
> ```
>
> Now we're able to resolve the query with code like this:
>
> 1. Load all the users (first query).
> 2. Map the users to a list of country ids.
> 3. Load all the countries with those ids (second query).
> 4. Pair up the users with the country with the correct id, so change `User.country` from
> `HasOne::NotLoaded` to `HasOne::Loaded(matching_country)`.
> 5. When resolving the GraphQL field `User.country` simply return the loaded country.
>
> ### A real example
>
> ```rust,ignore
> use juniper::{Executor, FieldResult};
> use juniper_eager_loading::{prelude::*, EagerLoading, HasOne};
> use juniper_from_schema::graphql_schema;
> use std::error::Error;
>
> // Define our GraphQL schema.
> graphql_schema! {
> schema {
> query: Query
> }
>
> type Query {
> allUsers: [User!]! @juniper(ownership: "owned")
> }
>
> type User {
> id: Int!
> country: Country!
> }
>
> type Country {
> id: Int!
> }
> }
>
> // Our model types.
> mod models {
> use std::error::Error;
> use juniper_eager_loading::LoadFrom;
>
> #[derive(Clone)]
> pub struct User {
> pub id: i32,
> pub country_id: i32
> }
>
> #[derive(Clone)]
> pub struct Country {
> pub id: i32,
> }
>
> // This trait is required for eager loading countries.
> // It defines how to load a list of countries from a list of ids.
> // Notice that `Context` is generic and can be whatever you want.
> // It will normally be your Juniper context which would contain
> // a database connection.
> impl LoadFrom<i32> for Country {
> type Error = Box<dyn Error>;
> type Context = super::Context;
>
> fn load(
> employments: &[i32],
> field_args: &(),
> ctx: &Self::Context,
> ) -> Result<Vec<Self>, Self::Error> {
> // ...
> # unimplemented!()
> }
> }
> }
>
> // Our sample database connection type.
> pub struct DbConnection;
>
> impl DbConnection {
> // Function that will load all the users.
> fn load_all_users(&self) -> Vec<models::User> {
> // ...
> # unimplemented!()
> }
> }
>
> // Our Juniper context type which contains a database connection.
> pub struct Context {
> db: DbConnection,
> }
>
> impl juniper::Context for Context {}
>
> // Our GraphQL user type.
> // `#[derive(EagerLoading)]` takes care of generating all the boilerplate code.
> #[derive(Clone, EagerLoading)]
> // You need to set the context and error type.
> #[eager_loading(
> context = Context,
> error = Box<dyn Error>,
>
> // These match the default so you wouldn't have to specify them
> model = models::User,
> id = i32,
> root_model_field = user,
> )]
> pub struct User {
> // This user model is used to resolve `User.id`
> user: models::User,
>
> // Setup a "has one" association between a user and a country.
> //
> // We could also have used `#[has_one(default)]` here.
> #[has_one(
> foreign_key_field = country_id,
> root_model_field = country,
> graphql_field = country,
> )]
> country: HasOne<Country>,
> }
>
> // And the GraphQL country type.
> #[derive(Clone, EagerLoading)]
> #[eager_loading(context = Context, error = Box<dyn Error>)]
> pub struct Country {
> country: models::Country,
> }
>
> // The root query GraphQL type.
> pub struct Query;
>
> impl QueryFields for Query {
> // The resolver for `Query.allUsers`.
> fn field_all_users(
> &self,
> executor: &Executor<'_, Context>,
> trail: &QueryTrail<'_, User, Walked>,
> ) -> FieldResult<Vec<User>> {
> let ctx = executor.context();
>
> // Load the model users.
> let user_models = ctx.db.load_all_users();
>
> // Turn the model users into GraphQL users.
> let mut users = User::from_db_models(&user_models);
>
> // Perform the eager loading.
> // `trail` is used to only eager load the fields that are requested. Because
> // we're using `QueryTrail`s from "juniper_from_schema" it would be a compile
> // error if we eager loaded associations that aren't requested in the query.
> User::eager_load_all_children_for_each(&mut users, &user_models, ctx, trail)?;
>
> Ok(users)
> }
> }
>
> impl UserFields for User {
> fn field_id(
> &self,
> executor: &Executor<'_, Context>,
> ) -> FieldResult<&i32> {
> Ok(&self.user.id)
> }
>
> fn field_country(
> &self,
> executor: &Executor<'_, Context>,
> trail: &QueryTrail<'_, Country, Walked>,
> ) -> FieldResult<&Country> {
> // This will unwrap the country from the `HasOne` or return an error if the
> // country wasn't loaded, or wasn't found in the database.
> Ok(self.country.try_unwrap()?)
> }
> }
>
> impl CountryFields for Country {
> fn field_id(
> &self,
> executor: &Executor<'_, Context>,
> ) -> FieldResult<&i32> {
> Ok(&self.country.id)
> }
> }
> #
> # fn main() {}
> ```
For more details, check out the [`juniper-eager-loading` documentation][`juniper-eager-loading`].
## Full example
For a full example using eager loading in [Juniper] check out the [`davidpdrsn/graphql-app-example` repository][10].
[`juniper-eager-loading`]: https://docs.rs/juniper-eager-loading
[`juniper-from-schema`]: https://docs.rs/juniper-from-schema
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Redis]: https://redis.io
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Language.Fields
[10]: https://github.com/davidpdrsn/graphql-app-example
[11]: https://docs.rs/juniper-eager-loading/latest/juniper_eager_loading#how-this-library-works-at-a-high-level
[12]: https://docs.rs/juniper-eager-loading/latest/juniper_eager_loading#a-real-example

View file

@ -1,89 +1,101 @@
Implicit and explicit `null`
============================
# Implicit and explicit null
> [GraphQL] has two semantically different ways to represent the lack of a value:
> - Explicitly providing the literal value: **null**.
> - Implicitly not providing a value at all.
There are two ways that a client can submit a null argument or field in a query.
There are two ways that a client can submit a [`null` value][0] as an [argument][5] or a [field][4] in a [GraphQL] query:
1. Either use an explicit `null` literal:
```graphql
{
field(arg: null)
}
```
2. Or simply omit the [argument][5], so the implicit default `null` value kicks in:
```graphql
{
field
}
```
They can use a null literal:
There are some situations where it's useful to know which one exactly has been provided.
For example, let's say we have a function that allows users to perform a "patch" operation on themselves. Let's say our users can optionally have favorite and least favorite numbers, and the input for that might look like this:
```rust
/// Updates user attributes. Fields that are [`None`] are left as-is.
struct UserPatch {
/// If [`Some`], updates the user's favorite number.
favorite_number: Option<Option<i32>>,
/// If [`Some`], updates the user's least favorite number.
least_favorite_number: Option<Option<i32>>,
```graphql
{
field(arg: null)
}
#
```
Or they can simply omit the argument:
```graphql
{
field
}
```
The former is an explicit null and the latter is an implicit null.
There are some situations where it's useful to know which one the user provided.
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:
```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() {}
```
To set a user's favorite number to 7, we would set `favorite_number` to `Some(Some(7))`. In [GraphQL], that might look like this:
To set a user's favorite number to 7, you would set `favorite_number` to
`Some(Some(7))`. In GraphQL, that might look like this:
```graphql
mutation { patchUser(patch: { favoriteNumber: 7 }) }
```
To unset the user's favorite number, we would set `favorite_number` to `Some(None)`. In [GraphQL], that might look like this:
To unset the user's favorite number, you would set `favorite_number` to
`Some(None)`. In GraphQL, that might look like this:
```graphql
mutation { patchUser(patch: { favoriteNumber: null }) }
```
And if we want to leave the user's favorite number alone, just set it to `None`. In [GraphQL], that might look like this:
If you want to leave the user's favorite number alone, you would set it to
`None`. In GraphQL, that might look like this:
```graphql
mutation { patchUser(patch: {}) }
```
The last two cases rely on being able to distinguish between [explicit and implicit `null`][1].
The last two cases rely on being able to distinguish between explicit and implicit null.
In Juniper, this can be done using the `Nullable` type:
Unfortunately, plain `Option` is not capable to distinguish them. That's why in [Juniper], this can be done using the [`Nullable`] type:
```rust
# extern crate juniper;
use juniper::{graphql_object, FieldResult, GraphQLInputObject, Nullable};
use juniper::{FieldResult, Nullable};
#[derive(GraphQLInputObject)]
#[derive(juniper::GraphQLInputObject)]
struct UserPatchInput {
favorite_number: Nullable<i32>,
least_favorite_number: Nullable<i32>,
pub favorite_number: Nullable<i32>,
pub least_favorite_number: Nullable<i32>,
}
impl From<UserPatchInput> for UserPatch {
fn from(input: UserPatchInput) -> Self {
Self {
// The `explicit()` function transforms the `Nullable` into an
// `Option<Option<T>>` as expected by the business logic layer.
favorite_number: input.favorite_number.explicit(),
least_favorite_number: input.least_favorite_number.explicit(),
}
}
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(),
}
}
}
# struct UserPatch {
# favorite_number: Option<Option<i32>>,
# least_favorite_number: Option<Option<i32>>,
# 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,
}
@ -91,27 +103,15 @@ impl juniper::Context for Context {}
struct Mutation;
#[graphql_object]
#[graphql(context = Context)]
#[juniper::graphql_object(context = Context)]
impl Mutation {
fn patch_user(patch: UserPatchInput, ctx: &Context) -> FieldResult<bool> {
fn patch_user(ctx: &Context, patch: UserPatchInput) -> FieldResult<bool> {
ctx.session.patch_user(patch.into())?;
Ok(true)
}
}
#
# fn main() {}
```
[`Nullable`]: https://docs.rs/juniper/0.16.1/juniper/enum.Nullable.html
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Null-Value
[1]: https://spec.graphql.org/October2021#sel-EAFdRDHAAEJDAoBxzT
[4]: https://spec.graphql.org/October2021#sec-Language.Fields
[5]: https://spec.graphql.org/October2021#sec-Language.Arguments
This type functions much like `Option`, but has two empty variants so you can
distinguish between implicit and explicit null.

View file

@ -1,10 +1,11 @@
Advanced topics
===============
# Advanced Topics
The chapters below cover some more advanced topics.
The chapters below cover some more advanced scenarios.
- [Implicit and explicit `null`](implicit_and_explicit_null.md)
- [N+1 problem](n_plus_1.md)
- [DataLoader](dataloader.md)
- [Look-ahead](lookahead.md)
- [Eager loading](eager_loading.md)
- [Introspection](introspection.md)
- [Non-struct objects](non_struct_objects.md)
- [Implicit and explicit null](implicit_and_explicit_null.md)
- [Objects and generics](objects_and_generics.md)
- [Multiple operations per request](multiple_ops_per_request.md)
- [Dataloaders](dataloaders.md)
- [Subscriptions](subscriptions.md)

View file

@ -0,0 +1,82 @@
# Introspection
GraphQL defines a special built-in top-level field called `__schema`. Querying
for this field allows one to [introspect the schema](https://graphql.org/learn/introspection/)
at runtime to see what queries and mutations the GraphQL server supports.
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:
```graphql
{
__schema {
types {
name
}
}
}
```
## Schema introspection output as JSON
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 `schema.json`. A complete representation of the schema can be
produced by issuing a specially crafted introspection query.
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
[graphql-client](https://github.com/graphql-rust/graphql-client):
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# extern crate serde_json;
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
GraphQLObject, IntrospectionFormat,
};
// Define our schema.
#[derive(GraphQLObject)]
struct Example {
id: String,
}
struct Context;
impl juniper::Context for Context {}
struct Query;
#[graphql_object(context = Context)]
impl Query {
fn example(id: String) -> FieldResult<Example> {
unimplemented!()
}
}
type Schema = juniper::RootNode<
'static,
Query,
EmptyMutation<Context>,
EmptySubscription<Context>
>;
fn main() {
// Create a context object.
let ctx = Context;
// Run the built-in introspection query.
let (res, _errors) = juniper::introspect(
&Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
&ctx,
IntrospectionFormat::default(),
).unwrap();
// Convert introspection result to json.
let json_result = serde_json::to_string_pretty(&res);
assert!(json_result.is_ok());
}
```

View file

@ -1,229 +0,0 @@
Look-ahead
==========
> In backtracking algorithms, **look ahead** is the generic term for a subprocedure that attempts to foresee the effects of choosing a branching variable to evaluate one of its values. The two main aims of look-ahead are to choose a variable to evaluate next and to choose the order of values to assign to it.
In [GraphQL], look-ahead machinery allows us to introspect the currently [executed][1] [GraphQL operation][2] to see which [fields][3] has been actually selected by it.
In [Juniper], it's represented by the [`Executor::look_ahead()`][20] method.
```rust
# extern crate juniper;
# use juniper::{graphql_object, Executor, GraphQLObject, ScalarValue};
#
# type UserId = i32;
#
#[derive(GraphQLObject)]
struct Person {
id: UserId,
name: String,
}
struct Query;
#[graphql_object]
// NOTICE: Specifying `ScalarValue` as custom named type parameter,
// so its name is similar to the one used in methods.
#[graphql(scalar = S: ScalarValue)]
impl Query {
fn persons<S: ScalarValue>(executor: &Executor<'_, '_, (), S>) -> Vec<Person> {
// Let's see which `Person`'s fields were selected in the client query.
for field_name in executor.look_ahead().children().names() {
dbg!(field_name);
}
// ...
# unimplemented!()
}
}
```
> **TIP**: `S: ScalarValue` type parameter on the method is required here to keep the [`Executor`] being generic over [`ScalarValue`] types. We, instead, could have used the [`DefaultScalarValue`], which is the default [`ScalarValue`] type for the [`Executor`], and make our code more ergonomic, but less flexible and generic.
> ```rust
> # extern crate juniper;
> # use juniper::{graphql_object, DefaultScalarValue, Executor, GraphQLObject};
> #
> # type UserId = i32;
> #
> # #[derive(GraphQLObject)]
> # struct Person {
> # id: UserId,
> # name: String,
> # }
> #
> # struct Query;
> #
> #[graphql_object]
> #[graphql(scalar = DefaultScalarValue)]
> impl Query {
> fn persons(executor: &Executor<'_, '_, ()>) -> Vec<Person> {
> for field_name in executor.look_ahead().children().names() {
> dbg!(field_name);
> }
> // ...
> # unimplemented!()
> }
> }
> ```
## N+1 problem
Naturally, look-ahead machinery allows us to solve [the N+1 problem](n_plus_1.md) by introspecting the requested fields and performing loading in batches eagerly, before actual resolving of those fields:
```rust
# extern crate anyhow;
# extern crate juniper;
# use std::collections::HashMap;
# use anyhow::anyhow;
# use juniper::{graphql_object, Executor, GraphQLObject, ScalarValue};
#
# type CultId = i32;
# type UserId = i32;
#
# struct Repository;
#
# impl juniper::Context for Repository {}
#
# impl Repository {
# async fn load_cult_by_id(&self, cult_id: CultId) -> anyhow::Result<Option<Cult>> { unimplemented!() }
# async fn load_cults_by_ids(&self, cult_ids: &[CultId]) -> anyhow::Result<HashMap<CultId, Cult>> { unimplemented!() }
# async fn load_all_persons(&self) -> anyhow::Result<Vec<Person>> { unimplemented!() }
# }
#
# enum Either<L, R> {
# Absent(L),
# Loaded(R),
# }
#
#[derive(Clone, GraphQLObject)]
struct Cult {
id: CultId,
name: String,
}
struct Person {
id: UserId,
name: String,
cult: Either<CultId, Cult>,
}
#[graphql_object]
#[graphql(context = Repository)]
impl Person {
fn id(&self) -> CultId {
self.id
}
fn name(&self) -> &str {
self.name.as_str()
}
async fn cult(&self, #[graphql(ctx)] repo: &Repository) -> anyhow::Result<Cult> {
match &self.cult {
Either::Loaded(cult) => Ok(cult.clone()),
Either::Absent(cult_id) => {
// Effectively performs the following SQL query:
// SELECT id, name FROM cults WHERE id = ${cult_id} LIMIT 1
repo.load_cult_by_id(*cult_id)
.await?
.ok_or_else(|| anyhow!("No cult exists for ID `{cult_id}`"))
}
}
}
}
struct Query;
#[graphql_object]
#[graphql(context = Repository, scalar = S: ScalarValue)]
impl Query {
async fn persons<S: ScalarValue>(
#[graphql(ctx)] repo: &Repository,
executor: &Executor<'_, '_, Repository, S>,
) -> anyhow::Result<Vec<Person>> {
// Effectively performs the following SQL query:
// SELECT id, name, cult_id FROM persons
let mut persons = repo.load_all_persons().await?;
// If the `Person.cult` field has been requested.
if executor.look_ahead()
.children()
.iter()
.any(|sel| sel.field_original_name() == "cult")
{
// Gather `Cult.id`s to load eagerly.
let cult_ids = persons
.iter()
.filter_map(|p| {
match &p.cult {
Either::Absent(cult_id) => Some(*cult_id),
// If for some reason a `Cult` is already loaded,
// then just skip it.
Either::Loaded(_) => None,
}
})
.collect::<Vec<_>>();
// Load the necessary `Cult`s eagerly.
// Effectively performs the following SQL query:
// SELECT id, name FROM cults WHERE id IN (${cult_id1}, ${cult_id2}, ...)
let cults = repo.load_cults_by_ids(&cult_ids).await?;
// Populate `persons` with the loaded `Cult`s, so they do not perform
// any SQL queries on resolving.
for p in &mut persons {
let Either::Absent(cult_id) = &p.cult else { continue; };
p.cult = Either::Loaded(
cults.get(cult_id)
.ok_or_else(|| anyhow!("No cult exists for ID `{cult_id}`"))?
.clone(),
);
}
}
Ok(persons)
}
}
```
And so, performing a [GraphQL query which lead to N+1 problem](n_plus_1.md)
```graphql
query {
persons {
id
name
cult {
id
name
}
}
}
```
will lead to efficient [SQL] queries, just as expected:
```sql
SELECT id, name, cult_id FROM persons;
SELECT id, name FROM cults WHERE id IN (1, 2, 3, 4);
```
## More features
See more available look-ahead features in the API docs of the [`LookAheadSelection`][21] and the [`LookAheadChildren`][22].
[`DefaultScalarValue`]: https://docs.rs/juniper/0.16.1/juniper/enum.DefaultScalarValue.html
[`Executor`]: https://docs.rs/juniper/0.16.1/juniper/executor/struct.Executor.html
[`ScalarValue`]: https://docs.rs/juniper/0.16.1/juniper/trait.ScalarValue.html
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[SQL]: https://en.wikipedia.org/wiki/SQL
[1]: https://spec.graphql.org/October2021#sec-Execution
[2]: https://spec.graphql.org/October2021#sec-Language.Operations\
[3]: https://spec.graphql.org/October2021#sec-Language.Fields
[20]: https://docs.rs/juniper/0.16.1/juniper/executor/struct.Executor.html#method.look_ahead
[21]: https://docs.rs/juniper/0.16.1/juniper/executor/struct.LookAheadSelection.html
[22]: https://docs.rs/juniper/0.16.1/juniper/executor/struct.LookAheadChildren.html

View file

@ -0,0 +1,73 @@
# Multiple operations per request
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 inefficient.
Some client libraries such as [apollo-link-batch-http](https://www.apollographql.com/docs/link/links/batch-http.html) have added the ability to batch operations in a single HTTP request to save network round-trips and potentially increase performance. There are some [tradeoffs](https://blog.apollographql.com/batching-client-graphql-queries-a685f5bcd41b) that should be considered before batching requests.
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.
Server integration crates maintained by others are **not required** to support batch requests. Batch requests aren't part of the official GraphQL specification.
Assuming an integration supports batch requests, for the following GraphQL query:
```graphql
{
hero {
name
}
}
```
The json data to POST to the server for an individual request would be:
```json
{
"query": "{hero{name}}"
}
```
And the response would be of the form:
```json
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
```
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:
```json
[
{
"query": "{hero{name}}"
},
{
"query": "{hero{name}}"
}
]
```
And the response would be of the form:
```json
[
{
"data": {
"hero": {
"name": "R2-D2"
}
}
},
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
]
```

View file

@ -1,111 +0,0 @@
N+1 problem
===========
A common issue with [GraphQL] server implementations is how the [resolvers][2] query their datasource. With a naive and straightforward approach we quickly run into the N+1 problem, resulting in a large number of unnecessary database queries or [HTTP] requests.
```rust
# extern crate anyhow;
# extern crate juniper;
# use anyhow::anyhow;
# use juniper::{graphql_object, GraphQLObject};
#
# type CultId = i32;
# type UserId = i32;
#
# struct Repository;
#
# impl juniper::Context for Repository {}
#
# impl Repository {
# async fn load_cult_by_id(&self, cult_id: CultId) -> anyhow::Result<Option<Cult>> { unimplemented!() }
# async fn load_all_persons(&self) -> anyhow::Result<Vec<Person>> { unimplemented!() }
# }
#
#[derive(GraphQLObject)]
struct Cult {
id: CultId,
name: String,
}
struct Person {
id: UserId,
name: String,
cult_id: CultId,
}
#[graphql_object]
#[graphql(context = Repository)]
impl Person {
fn id(&self) -> CultId {
self.id
}
fn name(&self) -> &str {
self.name.as_str()
}
async fn cult(&self, #[graphql(ctx)] repo: &Repository) -> anyhow::Result<Cult> {
// Effectively performs the following SQL query:
// SELECT id, name FROM cults WHERE id = ${cult_id} LIMIT 1
repo.load_cult_by_id(self.cult_id)
.await?
.ok_or_else(|| anyhow!("No cult exists for ID `{}`", self.cult_id))
}
}
struct Query;
#[graphql_object]
#[graphql(context = Repository)]
impl Query {
async fn persons(#[graphql(ctx)] repo: &Repository) -> anyhow::Result<Vec<Person>> {
// Effectively performs the following SQL query:
// SELECT id, name, cult_id FROM persons
repo.load_all_persons().await
}
}
```
Let's say we want to list a bunch of `cult`s `persons` were in:
```graphql
query {
persons {
id
name
cult {
id
name
}
}
}
```
Once the `persons` [list][1] has been [resolved][2], a separate [SQL] query is run to find the `cult` of each `Person`. We can see how this could quickly become a problem.
```sql
SELECT id, name, cult_id FROM persons;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 2;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 3;
SELECT id, name FROM cults WHERE id = 4;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 2;
-- and so on...
```
There are several ways how this problem may be resolved in [Juniper]. The most common ones are:
- [DataLoader](dataloader.md)
- [Look-ahead machinery](lookahead.md)
- [Eager loading](eager_loading.md)
[GraphQL]: https://graphql.org
[HTTP]: https://en.wikipedia.org/wiki/HTTP
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[SQL]: https://en.wikipedia.org/wiki/SQL
[1]: https://spec.graphql.org/October2021#sec-List
[2]: https://spec.graphql.org/October2021#sec-Executing-Fields

View file

@ -0,0 +1,58 @@
# Non-struct objects
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 _have_ to be mapped into GraphQL
interfaces.
Using `Result`-like enums can be a useful way of reporting e.g. validation
errors from a mutation:
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
# #[derive(juniper::GraphQLObject)] struct User { name: String }
#
#[derive(GraphQLObject)]
struct ValidationError {
field: String,
message: String,
}
# #[allow(dead_code)]
enum SignUpResult {
Ok(User),
Error(Vec<ValidationError>),
}
#[graphql_object]
impl SignUpResult {
fn user(&self) -> Option<&User> {
match *self {
SignUpResult::Ok(ref user) => Some(user),
SignUpResult::Error(_) => None,
}
}
fn error(&self) -> Option<&Vec<ValidationError>> {
match *self {
SignUpResult::Ok(_) => None,
SignUpResult::Error(ref errors) => Some(errors)
}
}
}
#
# fn main() {}
```
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.
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
[some](https://github.com/facebook/graphql/issues/117#issuecomment-170180628)
[comments](https://github.com/graphql/graphql-js/issues/560#issuecomment-259508214)
from one of the authors of GraphQL on how they intended "hard" field errors to
be used, and how to model expected errors.

View file

@ -0,0 +1,66 @@
# Objects and generics
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.
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. `Result<T, E>` into a GraphQL type, but you _can_ make e.g.
`Result<User, String>` into a GraphQL type.
Let's make a slightly more compact but generic implementation of [the last
chapter](non_struct_objects.md):
```rust
# extern crate juniper;
# #[derive(juniper::GraphQLObject)] struct User { name: String }
# #[derive(juniper::GraphQLObject)] struct ForumPost { title: String }
#[derive(juniper::GraphQLObject)]
struct ValidationError {
field: String,
message: String,
}
# #[allow(dead_code)]
struct MutationResult<T>(Result<T, Vec<ValidationError>>);
#[juniper::graphql_object(
name = "UserResult",
)]
impl MutationResult<User> {
fn user(&self) -> Option<&User> {
self.0.as_ref().ok()
}
fn error(&self) -> Option<&Vec<ValidationError>> {
self.0.as_ref().err()
}
}
#[juniper::graphql_object(
name = "ForumPostResult",
)]
impl MutationResult<ForumPost> {
fn forum_post(&self) -> Option<&ForumPost> {
self.0.as_ref().ok()
}
fn error(&self) -> Option<&Vec<ValidationError>> {
self.0.as_ref().err()
}
}
# fn main() {}
```
Here, we've made a wrapper around `Result` and exposed some concrete
instantiations of `Result<T, E>` 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 `Result` and Juniper's internal GraphQL trait are from third-party
sources.
Because we're using generics, we also need to specify a name for our
instantiated types. Even if Juniper _could_ figure out the name,
`MutationResult<User>` wouldn't be a valid GraphQL type name.

View file

@ -0,0 +1,175 @@
# Subscriptions
### How to achieve realtime data with GraphQL subscriptions
GraphQL subscriptions are a way to push data from the server to clients requesting real-time messages
from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client,
but instead of immediately returning a single answer a result is sent every time a particular event happens on the
server.
In order to execute subscriptions you need a coordinator (that spawns connections)
and a GraphQL object that can be resolved into a stream--elements of which will then
be returned to the end user. The [`juniper_subscriptions`][juniper_subscriptions] crate
provides a default connection implementation. Currently subscriptions are only supported on the `master` branch. Add the following to your `Cargo.toml`:
```toml
[dependencies]
juniper = "0.16.0"
juniper_subscriptions = "0.17.0"
```
### Schema Definition
The `Subscription` 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 [Stream][Stream].
This example shows a subscription operation that returns two events, the strings `Hello` and `World!`
sequentially:
```rust
# extern crate futures;
# extern crate juniper;
# use std::pin::Pin;
# use futures::Stream;
# use juniper::{graphql_object, graphql_subscription, FieldError};
#
# #[derive(Clone)]
# pub struct Database;
# impl juniper::Context for Database {}
# pub struct Query;
# #[graphql_object(context = Database)]
# impl Query {
# fn hello_world() -> &'static str {
# "Hello World!"
# }
# }
pub struct Subscription;
type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
#[graphql_subscription(context = Database)]
impl Subscription {
async fn hello_world() -> StringStream {
let stream = futures::stream::iter(vec![
Ok(String::from("Hello")),
Ok(String::from("World!"))
]);
Box::pin(stream)
}
}
#
# fn main () {}
```
### Coordinator
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 [`SubscriptionCoordinator`][SubscriptionCoordinator] trait provides coordination logic to enable functionality like DOS attack mitigation and resource limits.
The [`SubscriptionCoordinator`][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,
the [`SubscriptionCoordinator`][SubscriptionCoordinator] spawns a [`SubscriptionConnection`][SubscriptionConnection]. The [`SubscriptionConnection`][SubscriptionConnection] handles a single connection, providing resolver logic for a client stream as well as reconnection
and shutdown logic.
While you can implement [`SubscriptionCoordinator`][SubscriptionCoordinator] yourself, Juniper contains a simple and generic implementation called [`Coordinator`][Coordinator]. The `subscribe`
operation returns a [`Future`][Future] with an `Item` value of a `Result<Connection, GraphQLError>`,
where [`Connection`][Connection] is a `Stream` of values returned by the operation and [`GraphQLError`][GraphQLError] is the error when the subscription fails.
```rust
# #![allow(dead_code)]
# extern crate futures;
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate serde_json;
# use juniper::{
# http::GraphQLRequest,
# graphql_object, graphql_subscription,
# DefaultScalarValue, EmptyMutation, FieldError,
# RootNode, SubscriptionCoordinator,
# };
# use juniper_subscriptions::Coordinator;
# use futures::{Stream, StreamExt};
# use std::pin::Pin;
#
# #[derive(Clone)]
# pub struct Database;
#
# impl juniper::Context for Database {}
#
# impl Database {
# fn new() -> Self {
# Self
# }
# }
#
# pub struct Query;
#
# #[graphql_object(context = Database)]
# impl Query {
# fn hello_world() -> &'static str {
# "Hello World!"
# }
# }
#
# pub struct Subscription;
#
# type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
#
# #[graphql_subscription(context = Database)]
# impl Subscription {
# async fn hello_world() -> StringStream {
# let stream =
# futures::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
# Box::pin(stream)
# }
# }
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
fn schema() -> Schema {
Schema::new(Query, EmptyMutation::new(), Subscription)
}
async fn run_subscription() {
let schema = schema();
let coordinator = Coordinator::new(schema);
let req: GraphQLRequest<DefaultScalarValue> = serde_json::from_str(
r#"{
"query": "subscription { helloWorld }"
}"#,
)
.unwrap();
let ctx = Database::new();
let mut conn = coordinator.subscribe(&req, &ctx).await.unwrap();
while let Some(result) = conn.next().await {
println!("{}", serde_json::to_string(&result).unwrap());
}
}
#
# fn main() { }
```
### Web Integration and Examples
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.
- [Warp Subscription Example](https://github.com/graphql-rust/juniper/tree/master/juniper_warp/examples/subscription.rs)
- [Small Example](https://github.com/graphql-rust/juniper/tree/master/juniper_subscriptions/examples/basic.rs)
[juniper_subscriptions]: https://github.com/graphql-rust/juniper/tree/master/juniper_subscriptions
[Stream]: https://docs.rs/futures/0.3.4/futures/stream/trait.Stream.html
<!-- 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

View file

@ -1,85 +0,0 @@
Introduction
============
> [GraphQL] is a query language for APIs and a runtime for fulfilling those queries with your existing data. [GraphQL] provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
[Juniper] is a library for creating [GraphQL] servers in [Rust]. Build type-safe and fast API servers with minimal boilerplate and configuration (we do try to make declaring and resolving [GraphQL] schemas as convenient as [Rust] will allow).
[Juniper] doesn't include a web server itself, instead, it provides building blocks to make integration with existing web servers straightforward. It optionally provides a pre-built integration for some widely used web server frameworks in [Rust] ecosystem.
- [Cargo crate](https://crates.io/crates/juniper)
- [API reference][`juniper`]
## Features
[Juniper] supports the full GraphQL query language according to the [specification (October 2021)][GraphQL spec].
> **NOTE**: As an exception to other [GraphQL] libraries for other languages, [Juniper] builds non-`null` types by default. A field of type `Vec<Episode>` will be converted into `[Episode!]!`. The corresponding Rust type for a `null`able `[Episode]` would be `Option<Vec<Option<Episode>>>` instead.
## Integrations
### Types
[Juniper] provides out-of-the-box integration for some very common [Rust] crates to make building schemas a breeze. The types from these crates will be usable in your schemas automatically after enabling the correspondent self-titled [Cargo feature]:
- [`bigdecimal`]
- [`bson`]
- [`chrono`], [`chrono-tz`]
- [`jiff`]
- [`rust_decimal`]
- [`time`]
- [`url`]
- [`uuid`]
### Web server frameworks
- [`actix-web`] ([`juniper_actix`] crate)
- [`axum`] ([`juniper_axum`] crate)
- [`hyper`] ([`juniper_hyper`] crate)
- [`rocket`] ([`juniper_rocket`] crate)
- [`warp`] ([`juniper_warp`] crate)
## API stability
[Juniper] has not reached 1.0 yet, thus some API instability should be expected.
[`actix-web`]: https://docs.rs/actix-web
[`axum`]: https://docs.rs/axum
[`bigdecimal`]: https://docs.rs/bigdecimal
[`bson`]: https://docs.rs/bson
[`chrono`]: https://docs.rs/chrono
[`chrono-tz`]: https://docs.rs/chrono-tz
[`jiff`]: https://docs.rs/jiff
[`juniper`]: https://docs.rs/juniper
[`juniper_actix`]: https://docs.rs/juniper_actix
[`juniper_axum`]: https://docs.rs/juniper_axum
[`juniper_hyper`]: https://docs.rs/juniper_hyper
[`juniper_rocket`]: https://docs.rs/juniper_rocket
[`juniper_warp`]: https://docs.rs/juniper_warp
[`hyper`]: https://docs.rs/hyper
[`rocket`]: https://docs.rs/rocket
[`rust_decimal`]: https://docs.rs/rust_decimal
[`time`]: https://docs.rs/time
[`url`]: https://docs.rs/url
[`uuid`]: https://docs.rs/uuid
[`warp`]: https://docs.rs/warp
[Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
[GraphQL]: https://graphql.org
[GraphQL spec]: https://spec.graphql.org/October2021
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org

View file

@ -1,39 +1,34 @@
Quickstart
==========
This page will give you a short introduction to the concepts in [Juniper].
**[Juniper] follows a [code-first] approach to define a [GraphQL] schema.**
> **TIP**: For a [schema-first] approach, consider using a [`juniper-from-schema`] crate for generating a [`juniper`]-based code from a [schema] file.
# Quickstart
This page will give you a short introduction to the concepts in Juniper.
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.
## Installation
```toml
[dependencies]
juniper = "0.16.1"
juniper = "0.16.0"
```
## 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.
## Schema
Exposing simple enums and structs as [GraphQL] types 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>`, `Arc<T>`, `String`, `f64`, `i32`, references, slices and arrays.
For more advanced mappings, [Juniper] provides multiple macros to map your [Rust] types to a [GraphQL schema][schema]. The most important one is the [`#[graphql_object]` attribute][2] that is used for declaring a [GraphQL object] with resolvers (typically used for declaring [`Query` and `Mutation` roots][1]).
For more advanced mappings, Juniper provides multiple macros to map your Rust
types to a GraphQL schema. The most important one is the
[graphql_object][graphql_object] procedural macro that is used for declaring an object with
resolvers, which you will use for the `Query` and `Mutation` roots.
```rust
# # ![allow(unused_variables)]
# #![allow(unused_variables)]
# extern crate juniper;
#
# use std::fmt::Display;
#
use juniper::{
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
graphql_object, EmptySubscription, FieldResult, GraphQLEnum,
GraphQLInputObject, GraphQLObject, ScalarValue,
};
#
@ -61,6 +56,7 @@ struct Human {
}
// There is also a custom derive for mapping GraphQL input objects.
#[derive(GraphQLInputObject)]
#[graphql(description = "A humanoid creature in the Star Wars universe")]
struct NewHuman {
@ -69,58 +65,52 @@ struct NewHuman {
home_planet: String,
}
// Now, we create our root `Query` and `Mutation` types with resolvers by using
// the `#[graphql_object]` attribute.
// Now, we create our root Query and Mutation types with resolvers by using the
// object macro.
// Objects can have contexts that allow accessing shared state like a database
// pool.
// Resolvers can have a context that allows accessing shared state like a
// database pool.
struct Context {
// Use your real database pool here.
db: DatabasePool,
pool: DatabasePool,
}
// To make our `Context` usable by `juniper`, we have to implement a marker
// trait.
// To make our context usable by Juniper, we have to implement a marker trait.
impl juniper::Context for Context {}
struct Query;
// Here we specify the context type for the object.
// We need to do this in every type that needs access to the `Context`.
#[graphql_object]
#[graphql(context = Context)]
#[graphql_object(
// Here we specify the context type for the object.
// We need to do this in every type that
// needs access to the context.
context = Context,
)]
impl Query {
// Note, that the field name will be automatically converted to the
// `camelCased` variant, just as GraphQL conventions imply.
fn api_version() -> &'static str {
fn apiVersion() -> &'static str {
"1.0"
}
fn human(
// Arguments to resolvers can either be simple scalar types, enums or
// input objects.
id: String,
// To gain access to the `Context`, we specify a `context`-named
// argument referring the correspondent `Context` type, and `juniper`
// will inject it automatically.
context: &Context,
) -> FieldResult<Human> {
// Get a `db` connection.
let conn = context.db.get_connection()?;
// Execute a `db` query.
// Arguments to resolvers can either be simple types or input objects.
// To gain access to the context, we specify a argument
// that is a reference to the Context type.
// Juniper automatically injects the correct context here.
fn human(context: &Context, id: String) -> FieldResult<Human> {
// Get a db connection.
let connection = context.pool.get_connection()?;
// Execute a db query.
// Note the use of `?` to propagate errors.
let human = conn.find_human(&id)?;
let human = connection.find_human(&id)?;
// Return the result.
Ok(human)
}
}
// Now, we do the same for our `Mutation` type.
// Now, we do the same for our Mutation type.
struct Mutation;
#[graphql_object]
#[graphql(
#[graphql_object(
context = Context,
// If we need to use `ScalarValue` parametrization explicitly somewhere
// in the object definition (like here in `FieldResult`), we could
@ -128,48 +118,42 @@ struct Mutation;
scalar = S: ScalarValue + Display,
)]
impl Mutation {
fn create_human<S: ScalarValue + Display>(
new_human: NewHuman,
context: &Context,
) -> FieldResult<Human, S> {
let db = context.db.get_connection().map_err(|e| e.map_scalar_value())?;
fn createHuman<S: ScalarValue + Display>(context: &Context, new_human: NewHuman) -> FieldResult<Human, S> {
let db = context.pool.get_connection().map_err(|e| e.map_scalar_value())?;
let human: Human = db.insert_human(&new_human).map_err(|e| e.map_scalar_value())?;
Ok(human)
}
}
// Root schema consists of a query, a mutation, and a subscription.
// Request queries can be executed against a `RootNode`.
// A root schema consists of a query, a mutation, and a subscription.
// Request queries can be executed against a RootNode.
type Schema = juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;
#
# fn main() {
# _ = Schema::new(Query, Mutation, EmptySubscription::new());
# let _ = Schema::new(Query, Mutation, EmptySubscription::new());
# }
```
Now we have a very simple but functional schema for a [GraphQL] server!
We now have a very simple but functional schema for a GraphQL server!
To actually serve the [schema], see the guides for our various [server integrations](serve/index.md).
To actually serve the schema, see the guides for our various [server integrations](./servers/index.md).
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:
## Executor
## Execution
[Juniper] is a library that can be used in many contexts: it doesn't require a server, nor it has a dependency on a particular transport or serialization format. You can invoke the `juniper::execute()` directly to get a result for a [GraphQL] query:
You can invoke `juniper::execute` directly to run a GraphQL query:
```rust
# // Only needed due to 2018 edition because the macro is not accessible.
# #[macro_use] extern crate juniper;
use juniper::{
graphql_object, graphql_value, EmptyMutation, EmptySubscription,
GraphQLEnum, Variables,
graphql_object, EmptyMutation, EmptySubscription, FieldResult,
GraphQLEnum, Variables, graphql_value,
};
#[derive(GraphQLEnum, Clone, Copy)]
enum Episode {
// Note, that the enum value will be automatically converted to the
// `SCREAMING_SNAKE_CASE` variant, just as GraphQL conventions imply.
NewHope,
Empire,
Jedi,
@ -182,21 +166,22 @@ impl juniper::Context for Ctx {}
struct Query;
#[graphql_object]
#[graphql(context = Ctx)]
#[graphql_object(context = Ctx)]
impl Query {
fn favorite_episode(context: &Ctx) -> Episode {
context.0
fn favoriteEpisode(context: &Ctx) -> FieldResult<Episode> {
Ok(context.0)
}
}
// A root schema consists of a query, a mutation, and a subscription.
// Request queries can be executed against a RootNode.
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Ctx>, EmptySubscription<Ctx>>;
fn main() {
// Create a context.
// Create a context object.
let ctx = Ctx(Episode::NewHope);
// Run the execution.
// Run the executor.
let (res, _errors) = juniper::execute_sync(
"query { favoriteEpisode }",
None,
@ -205,28 +190,20 @@ fn main() {
&ctx,
).unwrap();
// Ensure the value matches.
assert_eq!(
res,
graphql_value!({
"favoriteEpisode": "NEW_HOPE",
}),
})
);
}
```
[`juniper`]: https://docs.rs/juniper
[`juniper-from-schema`]: https://docs.rs/juniper-from-schema
[code-first]: https://www.apollographql.com/blog/backend/architecture/schema-first-vs-code-only-graphql#code-only
[derive attribute]: https://doc.rust-lang.org/stable/reference/attributes/derive.html#derive
[GraphQL]: https://graphql.org
[GraphQL object]: https://spec.graphql.org/October2021#sec-Objects
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[schema]: https://graphql.org/learn/schema
[schema-first]: https://www.apollographql.com/blog/backend/architecture/schema-first-vs-code-only-graphql#schema-first
[1]: https://spec.graphql.org/October2021#sec-Root-Operation-Types
[2]: https://docs.rs/juniper/0.16.1/juniper/macro.graphql_object.html
[juniper-from-schema]: https://github.com/davidpdrsn/juniper-from-schema
[schema_approach]: https://blog.logrocket.com/code-first-vs-schema-first-development-graphql/
[hyper]: servers/hyper.md
[warp]: servers/warp.md
[rocket]: servers/rocket.md
[tutorial]: ./tutorial.html
[graphql_object]: https://docs.rs/juniper/latest/juniper/macro.graphql_object.html

View file

@ -1,185 +0,0 @@
Schema
======
**[Juniper] follows a [code-first] approach to define a [GraphQL] schema.**
> **TIP**: For a [schema-first] approach, consider using a [`juniper-from-schema`] crate for generating a [`juniper`]-based code from a [schema] file.
[GraphQL schema][0] consists of three [object types][4]: a [query root][1], a [mutation root][2], and a [subscription root][3].
> The **query** root operation type must be provided and must be an [Object][4] type.
>
> The **mutation** root operation type is optional; if it is not provided, the service does not support mutations. If it is provided, it must be an [Object][4] type.
>
> Similarly, the **subscription** root operation type is also optional; if it is not provided, the service does not support subscriptions. If it is provided, it must be an [Object][4] type.
>
> The **query**, **mutation**, and **subscription** root types must all be different types if provided.
In [Juniper], the [`RootNode`] type represents a [schema][0]. When the [schema][0] is first created, [Juniper] will traverse the entire object graph and register all types it can find. This means that if we [define a GraphQL object](../types/objects/index.md) somewhere but never use or reference it, it won't be exposed in a [GraphQL schema][0].
Both [query][1] and [mutation][2] objects are regular [GraphQL objects][4], defined like [any other object in Juniper](../types/objects/index.md). The [mutation][2] and [subscription][3] objects, however, are optional, since [schemas][0] can be read-only and do not require [subscriptions][3].
> **TIP**: If [mutation][2]/[subscription][3] functionality is not needed, consider using the predefined [`EmptyMutation`]/[`EmptySubscription`] types for stubbing them in a [`RootNode`].
```rust
# extern crate juniper;
# use juniper::{
# graphql_object, EmptySubscription, FieldResult, GraphQLObject, RootNode,
# };
#
#[derive(GraphQLObject)]
struct User {
name: String,
}
struct Query;
#[graphql_object]
impl Query {
fn user_with_username(username: String) -> FieldResult<Option<User>> {
// Look up user in database...
# unimplemented!()
}
}
struct Mutation;
#[graphql_object]
impl Mutation {
fn sign_up_user(name: String, email: String) -> FieldResult<User> {
// Validate inputs and save user in database...
# unimplemented!()
}
}
type Schema = RootNode<'static, Query, Mutation, EmptySubscription>;
#
# fn main() {}
```
> **NOTE**: It's considered a [good practice][5] to name [query][1], [mutation][2], and [subscription][3] root types as `Query`, `Mutation`, and `Subscription` respectively.
The usage of [subscriptions][3] is a little different from the [mutation][2] and [query][1] [objects][4], so they are discussed in the [separate chapter](subscriptions.md).
## Export
Many tools in [GraphQL] ecosystem require a [schema] definition to operate on. With [Juniper] we can export our [GraphQL schema][0] defined in [Rust] code either represented in the [GraphQL schema language][6] or in [JSON].
### SDL (schema definition language)
To generate an [SDL (schema definition language)][6] representation of a [GraphQL schema][0] defined in [Rust] code, the [`as_sdl()` method][20] should be used for the direct extraction (requires enabling the `schema-language` [Juniper] feature):
```rust
# extern crate juniper;
# use juniper::{
# graphql_object, EmptyMutation, EmptySubscription, FieldResult, RootNode,
# };
#
struct Query;
#[graphql_object]
impl Query {
fn hello(&self) -> FieldResult<&str> {
Ok("hello world")
}
}
fn main() {
// Define our schema in Rust.
let schema = RootNode::new(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
// Convert the Rust schema into the GraphQL SDL schema.
let result = schema.as_sdl();
let expected = "\
schema {
query: Query
}
type Query {
hello: String!
}
";
# #[cfg(not(target_os = "windows"))]
assert_eq!(result, expected);
}
```
### JSON
To export a [GraphQL schema][0] defined in [Rust] code as [JSON] (often referred to as `schema.json`), the specially crafted [introspection query][21] should be issued. [Juniper] provides a [convenience `introspect()` function][22] to [introspect](introspection.md) the entire [schema][0], which result can be serialized into [JSON]:
```rust
# extern crate juniper;
# extern crate serde_json;
# use juniper::{
# graphql_object, EmptyMutation, EmptySubscription, GraphQLObject,
# IntrospectionFormat, RootNode,
# };
#
#[derive(GraphQLObject)]
struct Example {
id: String,
}
struct Query;
#[graphql_object]
impl Query {
fn example(id: String) -> Example {
unimplemented!()
}
}
type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
fn main() {
// Run the built-in introspection query.
let (res, _errors) = juniper::introspect(
&Schema::new(Query, EmptyMutation::new(), EmptySubscription::new()),
&(),
IntrospectionFormat::default(),
).unwrap();
// Serialize the introspection result into JSON.
let json_result = serde_json::to_string_pretty(&res);
assert!(json_result.is_ok());
}
```
> **TIP**: We still can convert the generated [JSON] into a [GraphQL schema language][6] representation by using tools like [`graphql-json-to-sdl` command line utility][30].
[`EmptyMutation`]: https://docs.rs/juniper/0.16.1/juniper/struct.EmptyMutation.html
[`EmptySubscription`]: https://docs.rs/juniper/0.16.1/juniper/struct.EmptySubscription.html
[`juniper`]: https://docs.rs/juniper
[`juniper-from-schema`]: https://docs.rs/juniper-from-schema
[`RootNode`]: https://docs.rs/juniper/0.16.1/juniper/struct.RootNode.html
[code-first]: https://www.apollographql.com/blog/backend/architecture/schema-first-vs-code-only-graphql#code-only
[schema-first]: https://www.apollographql.com/blog/backend/architecture/schema-first-vs-code-only-graphql#schema-first
[GraphQL]: https://graphql.org
[JSON]: https://www.json.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[schema]: https://graphql.org/learn/schema
[0]: https://spec.graphql.org/October2021#sec-Schema
[1]: https://spec.graphql.org/October2021#sel-FAHTRFCAACChCtpG
[2]: https://spec.graphql.org/October2021#sel-FAHTRHCAACCuE9yD
[3]: https://spec.graphql.org/October2021#sel-FAHTRJCAACC3EhsX
[4]: https://spec.graphql.org/October2021#sec-Objects
[5]: https://spec.graphql.org/October2021#sec-Root-Operation-Types.Default-Root-Operation-Type-Names
[6]: https://graphql.org/learn/schema#type-language
[20]: https://docs.rs/juniper/0.16.1/juniper/struct.RootNode.html#method.as_sdl
[21]: https://docs.rs/crate/juniper/latest/source/src/introspection/query.graphql
[22]: https://docs.rs/juniper/0.16.1/juniper/fn.introspect.html
[30]: https://npmjs.com/package/graphql-json-to-sdl

View file

@ -1,83 +0,0 @@
Introspection
=============
> The [schema introspection][1] system is accessible from the meta-fields `__schema` and `__type` which are accessible from the type of the root of a query operation.
> ```graphql
> __schema: __Schema!
> __type(name: String!): __Type
> ```
> Like all meta-fields, these are implicit and do not appear in the fields list in the root type of the query operation.
[GraphQL] provides [introspection][0], allowing to see what [queries][2], [mutations][3] and [subscriptions][4] a [GraphQL] server supports at runtime.
Because [introspection][0] queries are just regular [GraphQL queries][2], [Juniper] supports them natively. For example, to get all the names of the types supported, we could [execute][5] the following [query][2] against [Juniper]:
```graphql
{
__schema {
types {
name
}
}
}
```
## Disabling
> Disabling introspection in production is a widely debated topic, but we believe its one of the first things you can do to harden your GraphQL API in production.
[Some security requirements and considerations][10] may mandate to disable [GraphQL schema introspection][1] in production environments. In [Juniper] this can be achieved by using the [`RootNode::disable_introspection()`][9] method:
```rust
# extern crate juniper;
# use juniper::{
# graphql_object, graphql_vars, EmptyMutation, EmptySubscription, GraphQLError,
# RootNode,
# };
#
pub struct Query;
#[graphql_object]
impl Query {
fn some() -> bool {
true
}
}
type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
fn main() {
let schema = Schema::new(Query, EmptyMutation::new(), EmptySubscription::new())
.disable_introspection();
let query = "query { __schema { queryType { name } } }";
match juniper::execute_sync(query, None, &schema, &graphql_vars! {}, &()) {
Err(GraphQLError::ValidationError(errs)) => {
assert_eq!(
errs.first().unwrap().message(),
"GraphQL introspection is not allowed, but the operation contained `__schema`",
);
}
res => panic!("expected `ValidationError`, returned: {res:#?}"),
}
}
```
> **NOTE**: Attempt to execute an [introspection query][1] results in [validation][11] error, rather than [execution][5] error.
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[0]: https://spec.graphql.org/October2021#sec-Introspection
[1]: https://spec.graphql.org/October2021#sec-Schema-Introspection
[2]: https://spec.graphql.org/October2021#sel-GAFRJBABABF_jB
[3]: https://spec.graphql.org/October2021#sel-GAFRJDABABI5C
[4]: https://spec.graphql.org/October2021#sel-GAFRJFABABMvpN
[5]: https://spec.graphql.org/October2021#sec-Execution
[9]: https://docs.rs/juniper/0.16.1/juniper/struct.RootNode.html#method.disable_introspection
[10]: https://www.apollographql.com/blog/why-you-should-disable-graphql-introspection-in-production
[11]: https://spec.graphql.org/October2021#sec-Validation

View file

@ -0,0 +1,119 @@
# Schemas
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.
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.
Both query and mutation objects are regular GraphQL objects, defined like any
other object in Juniper. The mutation and subscription objects, however, are optional since schemas
can be read-only and do not require subscriptions. If mutation/subscription functionality is not needed, consider using [EmptyMutation][EmptyMutation]/[EmptySubscription][EmptySubscription].
In Juniper, the `RootNode` type represents a schema. When the schema is first created,
Juniper will traverse the entire object graph
and register all types it can find. This means that if you define a GraphQL
object somewhere but never reference it, it will not be exposed in a schema.
## The query root
The query root is just a GraphQL object. You define it like any other GraphQL
object in Juniper, most commonly using the `graphql_object` proc macro:
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
struct Root;
#[graphql_object]
impl Root {
fn userWithUsername(username: String) -> FieldResult<Option<User>> {
// Look up user in database...
# unimplemented!()
}
}
#
# fn main() { }
```
## Mutations
Mutations are _also_ just GraphQL objects. Each mutation is a single field
that performs some mutating side-effect such as updating a database.
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::{graphql_object, FieldResult, GraphQLObject};
# #[derive(GraphQLObject)] struct User { name: String }
struct Mutations;
#[graphql_object]
impl Mutations {
fn signUpUser(name: String, email: String) -> FieldResult<User> {
// Validate inputs and save user in database...
# unimplemented!()
}
}
#
# fn main() { }
```
# Converting a Rust schema to the [GraphQL Schema Language][schema_language]
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
# extern crate juniper;
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldResult, RootNode,
};
struct Query;
#[graphql_object]
impl Query {
fn hello(&self) -> FieldResult<&str> {
Ok("hello world")
}
}
fn main() {
// Define our schema in Rust.
let schema = RootNode::new(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
// Convert the Rust schema into the GraphQL Schema Language.
let result = schema.as_schema_language();
let expected = "\
type Query {
hello: String!
}
schema {
query: Query
}
";
# #[cfg(not(target_os = "windows"))]
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/
[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

View file

@ -1,176 +0,0 @@
Subscriptions
=============
[GraphQL subscriptions][9] are a way to push data from a server to clients requesting real-time messages from a server. [Subscriptions][9] are similar to [queries][7] in that they specify a set of fields to be delivered to a client, but instead of immediately returning a single answer a result is sent every time a particular event happens on a server.
In order to execute [subscriptions][9] in [Juniper], we need a coordinator (spawning long-lived connections) and a [GraphQL object][4] with [fields][5] resolving into a [`Stream`] of elements which will then be returned to a client. The [`juniper_subscriptions` crate][30] provides a default implementation of these abstractions.
The [subscription root][3] is just a [GraphQL object][4], similar to the [query root][1] and [mutations root][2] that we define for operations in our [GraphQL schema][0]. For [subscriptions][9] all fields should be `async` and return a [`Stream`] of some [GraphQL type][6] values, rather than direct values.
```rust
# extern crate futures;
# extern crate juniper;
# use std::pin::Pin;
# use futures::Stream;
# use juniper::{graphql_object, graphql_subscription, FieldError};
#
# #[derive(Clone)]
# pub struct Database;
#
# impl juniper::Context for Database {}
#
# pub struct Query;
#
# #[graphql_object]
# #[graphql(context = Database)]
# impl Query {
# fn hello_world() -> &'static str {
# "Hello World!"
# }
# }
#
type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
pub struct Subscription;
#[graphql_subscription]
#[graphql(context = Database)]
impl Subscription {
// This subscription operation emits two values sequentially:
// the `String`s "Hello" and "World!".
async fn hello_world() -> StringStream {
let stream = futures::stream::iter([
Ok(String::from("Hello")),
Ok(String::from("World!")),
]);
Box::pin(stream)
}
}
#
# fn main () {}
```
## Coordinator
[GraphQL subscriptions][9] require a bit more resources than regular [queries][7] and provide a great vector for [DoS attacks][20]. This can can bring down a server easily if not handled correctly. The [`SubscriptionCoordinator` trait][`SubscriptionCoordinator`] provides coordination logic to enable functionality like [DoS attacks][20] mitigation and resource limits.
The [`SubscriptionCoordinator`] contains the [schema][0] and can keep track of opened connections, handle [subscription][9] start and end, and maintain a global ID for each [subscription][9]. Each time a connection is established, the [`SubscriptionCoordinator`] spawns a [32], which handles a single connection, providing resolver logic for a client stream as well as reconnection and shutdown logic.
While we can implement [`SubscriptionCoordinator`] ourselves, [Juniper] contains a simple and generic implementation called [`Coordinator`]. The `subscribe` method returns a [`Future`] resolving into a `Result<Connection, GraphQLError>`, where [`Connection`] is a [`Stream`] of [values][10] returned by the operation, and a [`GraphQLError`] is the error when the [subscription operation][9] fails.
```rust
# extern crate futures;
# extern crate juniper;
# extern crate juniper_subscriptions;
# extern crate serde_json;
# use std::pin::Pin;
# use futures::{Stream, StreamExt as _};
# use juniper::{
# http::GraphQLRequest,
# graphql_object, graphql_subscription,
# DefaultScalarValue, EmptyMutation, FieldError,
# RootNode, SubscriptionCoordinator,
# };
# use juniper_subscriptions::Coordinator;
#
# #[derive(Clone)]
# pub struct Database;
#
# impl juniper::Context for Database {}
#
# impl Database {
# fn new() -> Self {
# Self
# }
# }
#
# pub struct Query;
#
# #[graphql_object]
# #[graphql(context = Database)]
# impl Query {
# fn hello_world() -> &'static str {
# "Hello World!"
# }
# }
#
# type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
#
# pub struct Subscription;
#
# #[graphql_subscription]
# #[graphql(context = Database)]
# impl Subscription {
# async fn hello_world() -> StringStream {
# let stream = futures::stream::iter([
# 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 db = Database::new();
let req: GraphQLRequest<DefaultScalarValue> = serde_json::from_str(
r#"{
"query": "subscription { helloWorld }"
}"#,
).unwrap();
let mut conn = coordinator.subscribe(&req, &db).await.unwrap();
while let Some(result) = conn.next().await {
println!("{}", serde_json::to_string(&result).unwrap());
}
}
#
# fn main() {}
```
## WebSocket
For information about serving [GraphQL subscriptions][9] over [WebSocket], see the ["Serving" chapter](../serve/index.md#websocket).
[`Coordinator`]: https://docs.rs/juniper_subscriptions/0.17.0/juniper_subscriptions/struct.Coordinator.html
[`Connection`]: https://docs.rs/juniper_subscriptions/0.17.0/juniper_subscriptions/struct.Connection.html
[`Future`]: https://doc.rust-lang.org/stable/std/future/trait.Future.html
[`GraphQLError`]: https://docs.rs/juniper/0.16.1/juniper/enum.GraphQLError.html
[`Stream`]: https://docs.rs/futures/latest/futures/stream/trait.Stream.html
[`SubscriptionCoordinator`]: https://docs.rs/juniper/0.16.1/juniper/trait.SubscriptionCoordinator.html
[`SubscriptionConnection`]: https://docs.rs/juniper/0.16.1/juniper/trait.SubscriptionConnection.html
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[WebSocket]: https://en.wikipedia.org/wiki/WebSocket
[0]: https://spec.graphql.org/October2021#sec-Schema
[1]: https://spec.graphql.org/October2021#sel-FAHTRFCAACChCtpG
[2]: https://spec.graphql.org/October2021#sel-FAHTRHCAACCuE9yD
[3]: https://spec.graphql.org/October2021#sel-FAHTRJCAACC3EhsX
[4]: https://spec.graphql.org/October2021#sec-Objects
[5]: https://spec.graphql.org/October2021#sec-Language.Fields
[6]: https://spec.graphql.org/October2021#sec-Types
[7]: https://spec.graphql.org/October2021#sec-Query
[8]: https://spec.graphql.org/October2021#sec-Mutation
[9]: https://spec.graphql.org/October2021#sec-Subscription
[10]: https://spec.graphql.org/October2021#sec-Values
[20]: https://en.wikipedia.org/wiki/Denial-of-service_attack
[30]: https://docs.rs/juniper_subscriptions

View file

@ -1,81 +0,0 @@
Batching
========
The [GraphQL] standard generally assumes that there will be one server request per each client operation to perform (such as a query or mutation). This is conceptually simple but potentially inefficient.
Some client libraries (such as [`apollo-link-batch-http`][1]) have the ability to batch operations in a single [HTTP] request to save network round-trips and potentially increase performance. There are [some tradeoffs][3], though, that should be considered before [batching operations][2].
[Juniper]'s [server integration crates](index.md#officially-supported) support [batching multiple operations][2] in a single [HTTP] request out-of-the-box via [JSON] arrays. This makes them compatible with client libraries that support [batch operations][2] without any special configuration.
> **NOTE**: If you use a custom server integration, it's **not a hard requirement** to support [batching][2], as it's not a part of the [official GraphQL specification][0].
Assuming an integration supports [operations batching][2], for the following GraphQL query:
```graphql
{
hero {
name
}
}
```
The [JSON] `data` to [POST] for an individual request would be:
```json
{
"query": "{hero{name}}"
}
```
And the response would be in the form:
```json
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
```
However, if we want to run the same query twice in a single [HTTP] request, the batched [JSON] `data` to [POST] would be:
```json
[
{
"query": "{hero{name}}"
},
{
"query": "{hero{name}}"
}
]
```
And then, the response would be in the following array form:
```json
[
{
"data": {
"hero": {
"name": "R2-D2"
}
}
},
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
]
```
[GraphQL]: https://graphql.org
[HTTP]: https://en.wikipedia.org/wiki/HTTP
[JSON]: https://www.json.org
[Juniper]: https://docs.rs/juniper
[POST]: https://en.wikipedia.org/wiki/POST_(HTTP)
[0]: https://spec.graphql.org/October2021
[1]: https://www.apollographql.com/docs/link/links/batch-http.html
[2]: https://www.apollographql.com/blog/batching-client-graphql-queries
[3]: https://www.apollographql.com/blog/batching-client-graphql-queries#what-are-the-tradeoffs-with-batching

View file

@ -1,69 +0,0 @@
Serving
=======
Once we have built a [GraphQL schema][1], the next obvious step would be to serve it, so clients can interact with our [GraphQL] API. Usually, [GraphQL] APIs are served via [HTTP].
## Web server frameworks
Though the [`juniper`] crate doesn't provide a built-in [HTTP] server, the surrounding ecosystem does.
### Officially supported
[Juniper] officially supports the following widely used and adopted web server frameworks in [Rust] ecosystem:
- [`actix-web`] ([`juniper_actix`] crate)
- [`axum`] ([`juniper_axum`] crate)
- [`hyper`] ([`juniper_hyper`] crate)
- [`rocket`] ([`juniper_rocket`] crate)
- [`warp`] ([`juniper_warp`] crate)
See their API docs and usage examples (accessible from API docs) for further details of how they should be used.
> **NOTE**: All the officially supported web server framework integrations provide a simple and convenient way for exposing [GraphiQL] and/or [GraphQL Playground] with the [GraphQL schema][1] along. These powerful tools ease the development process by enabling you to explore and send client requests to the [GraphQL] API under development.
## WebSocket
> **NOTE**: [WebSocket] is a crucial part for serving [GraphQL subscriptions][2] over [HTTP].
There are two widely adopted protocols for serving [GraphQL] over [WebSocket]:
1. [Legacy `graphql-ws` GraphQL over WebSocket Protocol][ws-old], formerly used by [Apollo] and the [`subscriptions-transport-ws` npm package], and now being deprecated.
2. [New `graphql-transport-ws` GraphQL over WebSocket Protocol][ws-new], provided by the [`graphql-ws` npm package] and being used by [Apollo] as for now.
In the [Juniper] ecosystem, both implementations are provided by the [`juniper_graphql_ws`] crate. Most of the [officially supported web server framework integrations](#officially-supported) are able to serve a [GraphQL schema][1] over [WebSocket] (including [subscriptions][2]) and even support [auto-negotiation of the correct protocol based on the `Sec-Websocket-Protocol` HTTP header value][3]. See their API docs and usage examples (accessible from API docs) for further details of how to do so.
[`actix-web`]: https://docs.rs/actix-web
[`axum`]: https://docs.rs/axum
[`graphql-ws` npm package]: https://npmjs.com/package/graphql-ws
[`juniper`]: https://docs.rs/juniper
[`juniper_actix`]: https://docs.rs/juniper_actix
[`juniper_axum`]: https://docs.rs/juniper_axum
[`juniper_graphql_ws`]: https://docs.rs/juniper_graphql_ws
[`juniper_rocket`]: https://docs.rs/juniper_rocket
[`juniper_warp`]: https://docs.rs/juniper_warp
[`hyper`]: https://docs.rs/hyper
[`rocket`]: https://docs.rs/rocket
[`subscriptions-transport-ws` npm package]: https://npmjs.com/package/subscriptions-transport-ws
[`warp`]: https://docs.rs/warp
[Apollo]: https://www.apollographql.com
[GraphiQL]: https://github.com/graphql/graphiql
[GraphQL]: https://graphql.org
[GraphQL Playground]: https://github.com/prisma/graphql-playground
[HTTP]: https://en.wikipedia.org/wiki/HTTP
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[WebSocket]: https://en.wikipedia.org/wiki/WebSocket
[ws-new]: https://github.com/enisdenjo/graphql-ws/blob/v5.14.0/PROTOCOL.md
[ws-old]: https://github.com/apollographql/subscriptions-transport-ws/blob/v0.11.0/PROTOCOL.md
[1]: ../schema/index.md
[2]: ../schema/subscriptions.md
[3]: https://developer.mozilla.org/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#subprotocols

28
book/src/servers/hyper.md Normal file
View file

@ -0,0 +1,28 @@
# Integrating with Hyper
[Hyper] is a fast HTTP implementation that many other Rust web frameworks
leverage. It offers asynchronous I/O via the tokio runtime and works on
Rust's stable channel.
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.
Juniper's Hyper integration is contained in the [`juniper_hyper`][juniper_hyper] crate:
!FILENAME Cargo.toml
```toml
[dependencies]
juniper = "0.16.0"
juniper_hyper = "0.9.0"
```
Included in the source is a [small example][example] which sets up a basic GraphQL and [GraphiQL] handler.
[graphiql]: https://github.com/graphql/graphiql
[hyper]: https://hyper.rs/
[juniper_hyper]: https://github.com/graphql-rust/juniper/tree/master/juniper_hyper
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_hyper/examples/hyper_server.rs

16
book/src/servers/index.md Normal file
View file

@ -0,0 +1,16 @@
# Adding A Server
To allow using Juniper with the HTTP server of your choice,
it does **not** come with a built in HTTP server.
To actually get a server up and running, there are multiple official and
third-party integration crates that will get you there.
- [Official Server Integrations](official.md)
- [Warp](warp.md)
- [Rocket](rocket.md)
- [Hyper](hyper.md)
- [Third Party Integrations](third-party.md)
- [Actix-Web](https://github.com/actix/examples/tree/master/graphql/juniper)
- [Finchers](https://github.com/finchers-rs/finchers-juniper)
- [Tsukuyomi](https://github.com/tsukuyomi-rs/tsukuyomi/tree/master/examples/juniper)

View file

@ -0,0 +1,8 @@
# Official Server Integrations
Juniper provides official integration crates for several popular Rust server
libraries.
- [Warp](warp.md)
- [Rocket](rocket.md)
- [Hyper](hyper.md)

View file

@ -0,0 +1,22 @@
# Integrating with Rocket
[Rocket] 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.
Juniper's Rocket integration is contained in the [`juniper_rocket`][juniper_rocket] crate:
!FILENAME Cargo.toml
```toml
[dependencies]
juniper = "0.16.0"
juniper_rocket = "0.9.0"
```
Included in the source is a [small example][example] which sets up a basic GraphQL and [GraphiQL] handler.
[graphiql]: https://github.com/graphql/graphiql
[rocket]: https://rocket.rs/
[juniper_rocket]: https://github.com/graphql-rust/juniper/tree/master/juniper_rocket
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/simple.rs

View file

@ -0,0 +1,5 @@
# Other Examples
These examples are not officially maintained by Juniper developers.
- [Actix Web](https://github.com/actix/examples/tree/HEAD/graphql/juniper) | [Actix Web (advanced)](https://github.com/actix/examples/tree/HEAD/graphql/juniper-advanced)

24
book/src/servers/warp.md Normal file
View file

@ -0,0 +1,24 @@
# Integrating with Warp
[Warp] 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 [Hyper] and works on
Rust's stable channel.
Juniper's Warp integration is contained in the [`juniper_warp`][juniper_warp] crate:
!FILENAME Cargo.toml
```toml
[dependencies]
juniper = "0.16.0"
juniper_warp = "0.8.0"
```
Included in the source is a [small example][example] which sets up a basic GraphQL and [GraphiQL]/[GraphQL Playground] handlers with subscriptions support.
[GraphiQL]: https://github.com/graphql/graphiql
[GraphQL Playground]: https://github.com/prisma/graphql-playground
[hyper]: https://hyper.rs/
[warp]: https://crates.io/crates/warp
[juniper_warp]: https://github.com/graphql-rust/juniper/tree/master/juniper_warp
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_warp/examples/subscription.rs

View file

@ -1,151 +1,73 @@
Enums
=====
# Enums
> [GraphQL enum][0] types, like [scalar][1] types, also represent leaf values in a GraphQL type system. However [enum][0] types describe the set of possible values.
>
> [Enums][0] are not references for a numeric value, but are unique values in their own right. They may serialize as a string: the name of the represented value.
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:
With [Juniper] a [GraphQL enum][0] may be defined by using the [`#[derive(GraphQLEnum)]`][2] attribute on a [Rust enum][3] as long as its variants do not have any fields:
```rust
# extern crate juniper;
# use juniper::GraphQLEnum;
#
#[derive(GraphQLEnum)]
#[derive(juniper::GraphQLEnum)]
enum Episode {
NewHope,
Empire,
Jedi,
}
#
# fn main() {}
```
Juniper converts all enum variants to uppercase, so the corresponding string
values for these variants are `NEWHOPE`, `EMPIRE`, and `JEDI`, respectively. If
you want to override this, you can use the `graphql` attribute, similar to how
it works when [defining objects](objects/defining_objects.md):
### Renaming
By default, [enum][3] variants are converted from [Rust]'s standard `PascalCase` naming convention into [GraphQL]'s `SCREAMING_SNAKE_CASE` convention:
```rust
# extern crate juniper;
# use juniper::GraphQLEnum;
#
#[derive(GraphQLEnum)]
#[derive(juniper::GraphQLEnum)]
enum Episode {
NewHope, // exposed as `NEW_HOPE` in GraphQL schema
Empire, // exposed as `EMPIRE` in GraphQL schema
Jedi, // exposed as `JEDI` in GraphQL schema
}
#
# fn main() {}
```
We can override the name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLEnum;
#
#[derive(GraphQLEnum)]
#[graphql(name = "WrongEpisode")] // now exposed as `WrongEpisode` in GraphQL schema
enum Episode {
#[graphql(name = "LAST_HOPE")]
NewHope, // exposed as `LAST_HOPE` in GraphQL schema
#[graphql(name="NEW_HOPE")]
NewHope,
Empire,
Jedi,
}
#
# fn main() {}
```
Or provide a different renaming policy for all the [enum][3] variants:
## Documentation and deprecation
Just like when defining objects, the type itself can be renamed and documented,
while individual enum variants can be renamed, documented, and deprecated:
```rust
# extern crate juniper;
# use juniper::GraphQLEnum;
#
#[derive(GraphQLEnum)]
#[graphql(rename_all = "none")] // disables any renaming
enum Episode {
NewHope, // exposed as `NewHope` in GraphQL schema
Empire, // exposed as `Empire` in GraphQL schema
Jedi, // exposed as `Jedi` in GraphQL schema
}
#
# fn main() {}
```
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
### Documentation and deprecation
Just like when [defining GraphQL objects](objects/index.md#documentation), the [GraphQL enum][0] type and its values could be [documented][4] and [deprecated][5] via `#[graphql(description = "...")]` and `#[graphql(deprecated = "...")]`/[`#[deprecated]`][13] attributes:
```rust
# extern crate juniper;
# use juniper::GraphQLEnum;
#
/// This doc comment is visible only in Rust API docs.
#[derive(GraphQLEnum)]
#[graphql(description = "An episode of Star Wars")]
#[derive(juniper::GraphQLEnum)]
#[graphql(name="Episode", description="An episode of Star Wars")]
enum StarWarsEpisode {
/// This doc comment is visible only in Rust API docs.
#[graphql(description = "This description is visible only in GraphQL schema.")]
#[graphql(deprecated="We don't really talk about this one")]
ThePhantomMenace,
#[graphql(name="NEW_HOPE")]
NewHope,
/// This doc comment is visible only in Rust API docs.
#[graphql(desc = "Arguably the best one in the trilogy.")]
// ^^^^ shortcut for a `description` argument
Empire,
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
Jedi,
#[deprecated(note = "Only visible in Rust.")]
#[graphql(deprecated = "We don't really talk about this one.")]
// ^^^^^^^^^^ takes precedence over Rust's `#[deprecated]` attribute
ThePhantomMenace, // has no description in GraphQL schema
}
#
# fn main() {}
```
> **NOTE**: Only [GraphQL object][6]/[interface][7] fields and [GraphQL enum][0] values can be [deprecated][5].
### Ignoring
By default, all [enum][3] variants are included in the generated [GraphQL enum][0] type as values. To prevent including a specific variant, annotate it with the `#[graphql(ignore)]` attribute:
```rust
# #![allow(dead_code)]
# extern crate juniper;
# use juniper::GraphQLEnum;
#
#[derive(GraphQLEnum)]
enum Episode<T> {
NewHope,
#[graphql(description="Arguably the best one in the trilogy")]
Empire,
Jedi,
#[graphql(ignore)]
Legends(T), // cannot be queried from GraphQL
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
CloneWars(T), // cannot be queried from GraphQL
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[derive(GraphQLEnum)]`][2] attribute.
## Supported Macro Attributes (Derive)
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Enums
[1]: https://spec.graphql.org/October2021#sec-Scalars
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLEnum.html
[3]: https://doc.rust-lang.org/reference/items/enumerations.html
[4]: https://spec.graphql.org/October2021#sec-Descriptions
[5]: https://spec.graphql.org/October2021#sec--deprecated
[6]: https://spec.graphql.org/October2021#sec-Objects
[7]: https://spec.graphql.org/October2021#sec-Interfaces
[13]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
| Name of Attribute | Container Support | Field Support |
|-------------------|:-----------------:|:----------------:|
| context | ✔ | ? |
| deprecated | ✔ | ✔ |
| description | ✔ | ✔ |
| interfaces | ? | ✘ |
| name | ✔ | ✔ |
| noasync | ✔ | ? |
| scalar | ✘ | ? |
| skip | ? | ✘ |
| ✔: supported | ✘: not supported | ?: not available |

View file

@ -1,28 +1,20 @@
Type system
===========
# Type System
Most of the work in working with [Juniper] consists of mapping the [GraphQL type system][0] to the [Rust] types our application uses.
Most of the work in working with juniper consists of mapping the
GraphQL type system to the Rust types your application uses.
[Juniper] provides some convenient abstractions making this process as painless as possible.
Juniper provides some convenient abstractions that try to make this process
as painless as possible.
Find out more in the individual chapters below:
- [Objects](objects/index.md)
- [Complex fields](objects/complex_fields.md)
- [Context](objects/Context.md)
- [Error handling](objects/error/index.md)
- [Field errors](objects/error/field.md)
- [Schema errors](objects/error/schema.md)
- [Generics](objects/generics.md)
- [Interfaces](interfaces.md)
- [Unions](unions.md)
- [Enums](enums.md)
- [Input objects](input_objects.md)
- [Scalars](scalars.md)
Find out more in the individual chapters below.
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Type-System
- [Defining objects](objects/defining_objects.md)
- [Complex fields](objects/complex_fields.md)
- [Using contexts](objects/using_contexts.md)
- [Error handling](objects/error_handling.md)
- [Other types](other-index.md)
- [Enums](enums.md)
- [Interfaces](interfaces.md)
- [Input objects](input_objects.md)
- [Scalars](scalars.md)
- [Unions](unions.md)

View file

@ -1,163 +1,62 @@
Input objects
=============
# Input objects
> [Fields][4] may accept [arguments][5] to configure their behavior. These inputs are often [scalars][12] or [enums][10], but they sometimes need to represent more complex values.
>
> A [GraphQL input object][0] defines a set of input fields; the input fields are either [scalars][12], [enums][10], or other [input objects][0]. This allows [arguments][5] to accept arbitrarily complex structs.
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:
In [Juniper], defining a [GraphQL input object][0] is quite straightforward and similar to how [trivial GraphQL objects are defined](objects/index.md) - by using the [`#[derive(GraphQLInputObject)]` attribute][2] on a [Rust struct][struct]:
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::{graphql_object, GraphQLInputObject, GraphQLObject};
#
#[derive(GraphQLInputObject)]
#[derive(juniper::GraphQLInputObject)]
struct Coordinate {
latitude: f64,
longitude: f64
}
struct Root;
# #[derive(GraphQLObject)] struct User { name: String }
# #[derive(juniper::GraphQLObject)] struct User { name: String }
#[graphql_object]
#[juniper::graphql_object]
impl Root {
fn users_at_location(coordinate: Coordinate, radius: f64) -> Vec<User> {
// Send coordinate to database
// ...
# unimplemented!()
# unimplemented!()
}
}
#
# fn main() {}
```
## Documentation and renaming
### Renaming
Just like the [other](objects/defining_objects.md) [derives](enums.md), you can rename
and add documentation to both the type and the fields:
Just as with [defining GraphQL objects](objects/index.md#renaming), by default [struct] fields are converted from [Rust]'s standard `snake_case` naming convention into [GraphQL]'s `camelCase` convention:
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use juniper::GraphQLInputObject;
#
#[derive(GraphQLInputObject)]
struct Person {
first_name: String, // exposed as `firstName` in GraphQL schema
last_name: String, // exposed as `lastName` in GraphQL schema
#[derive(juniper::GraphQLInputObject)]
#[graphql(name="Coordinate", description="A position on the globe")]
struct WorldCoordinate {
#[graphql(name="lat", description="The latitude")]
latitude: f64,
#[graphql(name="long", description="The longitude")]
longitude: f64
}
#
struct Root;
# #[derive(juniper::GraphQLObject)] struct User { name: String }
#[juniper::graphql_object]
impl Root {
fn users_at_location(coordinate: WorldCoordinate, radius: f64) -> Vec<User> {
// Send coordinate to database
// ...
# unimplemented!()
}
}
# fn main() {}
```
We can override the name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLInputObject;
#
#[derive(GraphQLInputObject)]
#[graphql(name = "WebPerson")] // now exposed as `WebPerson` in GraphQL schema
struct Person {
name: String,
age: i32,
#[graphql(name = "websiteURL")]
website_url: Option<String>, // now exposed as `websiteURL` in GraphQL schema
}
#
# fn main() {}
```
Or provide a different renaming policy for all the [struct] fields:
```rust
# extern crate juniper;
# use juniper::GraphQLInputObject;
#
#[derive(GraphQLInputObject)]
#[graphql(rename_all = "none")] // disables any renaming
struct Person {
name: String,
age: i32,
website_url: Option<String>, // exposed as `website_url` in GraphQL schema
}
#
# fn main() {}
```
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
### Documentation
Similarly, [GraphQL descriptions][7] may be provided by either using [Rust doc comments][6] or with the `#[graphql(description = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLInputObject;
#
/// This doc comment is visible only in Rust API docs.
#[derive(GraphQLInputObject)]
#[graphql(description = "This description is visible only in GraphQL schema.")]
struct Person {
/// This doc comment is visible only in Rust API docs.
#[graphql(desc = "This description is visible only in GraphQL schema.")]
// ^^^^ shortcut for a `description` argument
name: String,
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
age: i32,
}
#
# fn main() {}
```
> **NOTE**: As of [October 2021 GraphQL specification][spec], [GraphQL input object][0]'s fields **cannot be** [deprecated][9].
### Ignoring
By default, all [struct] fields are included into the generated [GraphQL input object][0] type. To prevent inclusion of a specific field annotate it with the `#[graphql(ignore)]` attribute:
> **WARNING**: Ignored fields must either implement `Default` or be annotated with the `#[graphql(default = <expression>)]` argument.
```rust
# extern crate juniper;
# use juniper::GraphQLInputObject;
#
enum System {
Cartesian,
}
#[derive(GraphQLInputObject)]
struct Point2D {
x: f64,
y: f64,
#[graphql(ignore, default = System::Cartesian)]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This attribute is required, as we need to be able to construct
// a `Point2D` value from the `{ x: 0.0, y: 0.0 }` GraphQL input value,
// received from client-side.
system: System,
// `Default::default()` value is used, if no
// `#[graphql(default = <expression>)]` is specified.
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
shift: f64,
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[derive(GraphQLInputObject)]`][2] attribute.
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[struct]: https://doc.rust-lang.org/reference/items/structs.html
[spec]: https://spec.graphql.org/October2021
[0]: https://spec.graphql.org/October2021#sec-Input-Objects
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLInputObject.html
[4]: https://spec.graphql.org/October2021#sec-Language.Fields
[5]: https://spec.graphql.org/October2021#sec-Language.Arguments
[6]: https://doc.rust-lang.org/reference/comments.html#doc-comments
[7]: https://spec.graphql.org/October2021#sec-Descriptions
[9]: https://spec.graphql.org/October2021#sec--deprecated
[10]: https://spec.graphql.org/October2021#sec-Enums
[12]: https://spec.graphql.org/October2021#sec-Scalars

View file

@ -1,45 +1,76 @@
Interfaces
==========
> [GraphQL interfaces][0] represent a list of named [fields][4] and their [arguments][5]. [GraphQL objects][10] and [interfaces][0] can then implement these [interfaces][0] which requires that the implementing type will define all [fields][4] defined by those [interfaces][0].
[GraphQL interfaces][1] 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 [GraphQL interfaces][1] are Rust traits, and the main difference is that in GraphQL an [interface type][1] serves both as an _abstraction_ and a _boxed value (downcastable to concrete implementers)_, while in Rust, a trait is an _abstraction only_ and _to represent such a boxed value a separate type is required_, 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 [GraphQL interfaces][1] in Rust, but on the other hand gives you full control over which type is backing your interface, and how it's resolved.
[GraphQL interfaces][0] 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 [GraphQL interfaces][0] are [Rust traits][20], but the main difference is that in [GraphQL] an [interface type][0] serves both as an _abstraction_ and a _boxed value (dispatchable to concrete implementers)_, while in [Rust], a [trait][20] is an _abstraction only_, and _to represent such a boxed value a separate type is required_, like a [trait object][21] or an [enum][22] consisting of implementer types, because [Rust trait][20] doesn't represent a type itself, and so, can have no values.
For implementing [GraphQL interfaces][1] Juniper provides the `#[graphql_interface]` macro.
Another notable difference is that [GraphQL interfaces][0] are more like [structurally-typed][30] contracts: they _only declare a list of [fields][4]_ a [GraphQL] type should already have. [Rust traits][20], on the other hand, are [type classes][31], which don't really care about existing methods, but, rather, _require to provide implementations for required methods_ despite the fact whether the type already has such methods or not. This difference makes the [trait implementation][23] not a good fit for expressing a [GraphQL interface][0] implementation, because _we don't really need to implement any [fields][4]_, the [GraphQL] type implementing a [GraphQL interface][0] has those [fields][4] already. _We only need to check that [fields'][4] signatures match_.
That's why [Juniper] takes the following approach to represent [GraphQL interfaces][0], which consists of two parts:
1. Either a [struct][24], or a [trait][20] (in case [fields][4] have [arguments][5]), which acts only as a blueprint describing the required list of [fields][4], and is not used in runtime at all.
2. An auto-generated [enum][22], representing a dispatchable value-type for the [GraphQL interfaces][0], which may be referred and returned by other [fields][4].
This may be done by using either the [`#[graphql_interface]` attribute][3] or the [`#[derive(GraphQLInterface)]`][2]:
## Traits
Defining a trait is mandatory for defining a [GraphQL interface][1], because this is the _obvious_ way we describe an _abstraction_ in Rust. All [interface][1] fields are defined as computed ones via trait methods.
```rust
# extern crate juniper;
# use juniper::{graphql_interface, GraphQLInterface, GraphQLObject};
#
// By default a `CharacterValue` enum is generated by macro to represent
// values of this GraphQL interface.
#[derive(GraphQLInterface)]
#[graphql(for = Human)] // enumerating all implementers is mandatory
struct Character {
id: String,
}
use juniper::graphql_interface;
// Using a trait to describe the required fields is fine too.
#[graphql_interface]
#[graphql(enum = HasHomeEnum, for = Human)]
// ^^^^ the generated value-type enum can be renamed, if required
trait HasHome {
fn home_planet(&self) -> &str;
trait Character {
fn id(&self) -> &str;
}
#
# fn main() {}
```
However, to return values of such [interface][1], we should provide its implementers and the Rust type representing a _boxed value of this trait_. The last one can be represented in two flavors: enum and [trait object][2].
### Enum values (default)
By default, Juniper generates an enum representing the values of the defined [GraphQL interface][1], and names it straightforwardly, `{Interface}Value`.
```rust
# extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
#[graphql_interface(for = [Human, Droid])] // enumerating all implementers is mandatory
trait Character {
fn id(&self) -> &str;
}
#[derive(GraphQLObject)]
#[graphql(impl = [CharacterValue, HasHomeEnum])]
// ^^^^^^^^^^^^^^ ^^^^^^^^^^^
// Notice the enum type names, neither the trait name nor the struct name
// is used to refer the GraphQL interface.
#[graphql(impl = CharacterValue)] // notice enum name, NOT trait name
struct Human {
id: String, // also resolves `Character.id` field
home_planet: String, // also resolves `HasHome.homePlanet` field
id: String,
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)]
struct Droid {
id: String,
}
#
# fn main() {}
```
Also, enum name can be specified explicitly, if desired.
```rust
# extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
#[graphql_interface(enum = CharacterInterface, for = Human)]
trait Character {
fn id(&self) -> &str;
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterInterface)]
struct Human {
id: String,
home_planet: String,
}
#
# fn main() {}
@ -48,19 +79,18 @@ struct Human {
### Interfaces implementing other interfaces
[GraphQL] allows implementing [interfaces][0] on other [interfaces][0] in addition to [objects][10]:
GraphQL allows implementing interfaces on other interfaces in addition to objects.
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLInterface, ID};
#
#[derive(GraphQLInterface)]
#[graphql(for = [HumanValue, Luke])]
use juniper::{graphql_interface, graphql_object, ID};
#[graphql_interface(for = [HumanValue, Luke])]
struct Node {
id: ID,
}
#[derive(GraphQLInterface)]
#[graphql(impl = NodeValue, for = Luke)]
#[graphql_interface(impl = NodeValue, for = Luke)]
struct Human {
id: ID,
home_planet: String,
@ -70,16 +100,15 @@ struct Luke {
id: ID,
}
#[graphql_object]
#[graphql(impl = [HumanValue, NodeValue])]
#[graphql_object(impl = [HumanValue, NodeValue])]
impl Luke {
fn id(&self) -> &ID {
&self.id
}
// As `String` and `&str` aren't distinguished by GraphQL spec and
// represent the same `!String` GraphQL scalar, we can use them
// interchangeably. The same is applied for `Cow<'a, str>`.
// As `String` and `&str` aren't distinguished by
// GraphQL spec, you can use them interchangeably.
// Same is applied for `Cow<'a, str>`.
// ⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
fn home_planet() -> &'static str {
"Tatooine"
@ -89,84 +118,78 @@ impl Luke {
# fn main() {}
```
> **NOTE**: Every [interface][0] has to specify all other [interfaces][0]/[objects][0] it implements or implemented for. Missing one of `for = ` or `impl = ` attribute arguments is a **compile-time error**.
> ```rust,compile_fail
> # extern crate juniper;
> # use juniper::{GraphQLInterface, GraphQLObject};
> #
> #[derive(GraphQLObject)]
> pub struct ObjA {
> id: String,
> }
>
> #[derive(GraphQLInterface)]
> #[graphql(for = ObjA)]
> // ^^^^^^^^^^ the evaluated program panicked at
> // 'Failed to implement interface `Character` on `ObjA`: missing interface
> // reference in implementer's `impl` attribute.'
> struct Character {
> id: String,
> }
> #
> # fn main() {}
> ```
> __NOTE:__ Every interface has to specify all other interfaces/objects it implements or implemented for. Missing one of `for = ` or `impl = ` attributes is a compile-time error.
```compile_fail
# extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
#[derive(GraphQLObject)]
pub struct ObjA {
id: String,
}
#[graphql_interface(for = ObjA)]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at
// 'Failed to implement interface `Character` on `ObjA`: missing interface reference in implementer's `impl` attribute.'
struct Character {
id: String,
}
fn main() {}
```
### Subtyping and additional `null`able arguments
### GraphQL subtyping and additional `null`able fields
[GraphQL] allows implementers (both [objects][10] and other [interfaces][0]) to return "subtypes" instead of an original value. Basically, this allows to impose additional bounds on the implementation.
GraphQL allows implementers (both objects and other interfaces) to return "subtypes" instead of an original value. Basically, this allows you to impose additional bounds on the implementation.
Valid "subtypes" are:
- [interface][0] implementer instead of an [interface][0] itself:
- interface implementer instead of an interface itself:
- `I implements T` in place of a `T`;
- `Vec<I implements T>` in place of a `Vec<T>`.
- [non-`null`][6] value in place of a `null`able:
- non-null value in place of a nullable:
- `T` in place of a `Option<T>`;
- `Vec<T>` in place of a `Vec<Option<T>>`.
These rules are recursively applied, so `Vec<Vec<I implements T>>` is a valid "subtype" of a `Option<Vec<Option<Vec<Option<T>>>>>`.
Also, [GraphQL] allows implementers to add `null`able [field arguments][5], which aren't present on an original interface.
Also, GraphQL allows implementers to add `null`able fields, which aren't present on an original interface.
```rust
# extern crate juniper;
# use juniper::{graphql_interface, graphql_object, GraphQLInterface, ID};
#
#[derive(GraphQLInterface)]
#[graphql(for = [HumanValue, Luke])]
use juniper::{graphql_interface, graphql_object, ID};
#[graphql_interface(for = [HumanValue, Luke])]
struct Node {
id: ID,
}
#[derive(GraphQLInterface)]
#[graphql(for = HumanConnectionValue)]
#[graphql_interface(for = HumanConnectionValue)]
struct Connection {
nodes: Vec<NodeValue>,
}
#[derive(GraphQLInterface)]
#[graphql(impl = NodeValue, for = Luke)]
#[graphql_interface(impl = NodeValue, for = Luke)]
struct Human {
id: ID,
home_planet: String,
}
#[derive(GraphQLInterface)]
#[graphql(impl = ConnectionValue)]
#[graphql_interface(impl = ConnectionValue)]
struct HumanConnection {
nodes: Vec<HumanValue>,
// ^^^^^^^^^^ notice not `NodeValue`
// This can happen, because every `Human` is a `Node` too, so we just
// impose additional bounds, which still can be resolved with
// `... on Connection { nodes }` syntax.
// This can happen, because every `Human` is a `Node` too, so we are just
// imposing additional bounds, which still can be resolved with
// `... on Connection { nodes }`.
}
struct Luke {
id: ID,
}
#[graphql_object]
#[graphql(impl = [HumanValue, NodeValue])]
#[graphql_object(impl = [HumanValue, NodeValue])]
impl Luke {
fn id(&self) -> &ID {
&self.id
@ -174,13 +197,13 @@ impl Luke {
fn home_planet(language: Option<String>) -> &'static str {
// ^^^^^^^^^^^^^^
// Notice additional `null`able field argument, which is missing on
// `Human`. Resolving `...on Human { homePlanet }` will provide `None`
// for this argument (default argument value).
// Notice additional `null`able field, which is missing on `Human`.
// Resolving `...on Human { homePlanet }` will provide `None` for this
// argument.
match language.as_deref() {
None | Some("en") => "Tatooine",
Some("ko") => "타투인",
_ => unimplemented!(),
_ => todo!(),
}
}
}
@ -188,229 +211,271 @@ impl Luke {
# fn main() {}
```
> **NOTE**: Violating [GraphQL] "subtyping" or additional `null`able [argument][5] rules is a **compile-time error**.
>
> ```rust,compile_fail
> # extern crate juniper;
> # use juniper::{graphql_object, GraphQLInterface};
> #
> pub struct ObjA {
> id: String,
> }
>
> #[graphql_object]
> #[graphql(impl = CharacterValue)]
> impl ObjA {
> fn id(&self, is_present: bool) -> &str {
> // ^^ the evaluated program panicked at
> // 'Failed to implement interface `Character` on `ObjA`: Field `id`: Argument
> // `isPresent` of type `Boolean!` isn't present on the interface and so has
> // to be nullable.'
> is_present.then_some(&self.id).unwrap_or("missing")
> }
> }
>
> #[derive(GraphQLInterface)]
> #[graphql(for = ObjA)]
> struct Character {
> id: String,
> }
> #
> # fn main() {}
> ```
>
> ```rust,compile_fail
> # extern crate juniper;
> # use juniper::{GraphQLInterface, GraphQLObject};
> #
> #[derive(GraphQLObject)]
> #[graphql(impl = CharacterValue)]
> pub struct ObjA {
> id: Vec<String>,
> // ^^ the evaluated program panicked at
> // 'Failed to implement interface `Character` on `ObjA`: Field `id`:
> // implementer is expected to return a subtype of interface's return
> // object: `[String!]!` is not a subtype of `String!`.'
> }
>
> #[derive(GraphQLInterface)]
> #[graphql(for = ObjA)]
> struct Character {
> id: String,
> }
> #
> # fn main() {}
> ```
Violating GraphQL "subtyping" or additional nullable field rules is a compile-time error.
### Default arguments
[Similarly to GraphQL object fields](objects/complex_fields.md#default-arguments), [GraphQL arguments][4] of [interfaces][0] are able to have default values, though [Rust] doesn't have such notion:
```rust
```compile_fail
# extern crate juniper;
# use juniper::graphql_interface;
#
#[graphql_interface]
trait Person {
fn field1(
// Default value can be any valid Rust expression, including a function
// call, etc.
#[graphql(default = true)]
arg1: bool,
// If default expression is not specified, then the `Default::default()`
// value is used.
#[graphql(default)]
arg2: i32,
) -> String;
}
#
# fn main() {}
```
use juniper::{graphql_interface, graphql_object};
### Renaming
Just as with [defining GraphQL objects](objects/index.md#renaming), by default, [fields][4] are converted from [Rust]'s standard `snake_case` naming convention into [GraphQL]'s `camelCase` convention:
We can override the name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::{graphql_interface, GraphQLInterface};
#
#[derive(GraphQLInterface)]
#[graphql(name = "CharacterInterface")]
struct Character { // exposed as `CharacterInterface` in GraphQL schema
#[graphql(name = "myCustomFieldName")]
renamed_field: bool, // exposed as `myCustomFieldName` in GraphQL schema
pub struct ObjA {
id: String,
}
#[graphql_interface]
#[graphql(name = "PersonInterface")]
trait Person { // exposed as `PersonInterface` in GraphQL schema
#[graphql(name = "myCustomFieldName")]
fn renamed_field( // exposed as `myCustomFieldName` in GraphQL schema
#[graphql(name = "myArgument")]
renamed_argument: bool, // exposed as `myArgument` in GraphQL schema
) -> bool;
#[graphql_object(impl = CharacterValue)]
impl ObjA {
fn id(&self, is_present: bool) -> &str {
// ^^ the evaluated program panicked at
// 'Failed to implement interface `Character` on `ObjA`: Field `id`: Argument `isPresent` of type `Boolean!`
// isn't present on the interface and so has to be nullable.'
is_present.then_some(&self.id).unwrap_or("missing")
}
}
#
# fn main() {}
```
Or provide a different renaming policy for all the defined [fields][4]:
```rust
# extern crate juniper;
# use juniper::graphql_interface;
#
#[graphql_interface]
#[graphql(rename_all = "none")] // disables any renaming
trait Person {
fn renamed_field( // exposed as `renamed_field` in GraphQL schema
renamed_argument: bool, // exposed as `renamed_argument` in GraphQL schema
) -> bool;
}
#
# fn main() {}
```
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
### Documentation and deprecation
Similarly, [GraphQL fields][4] of [interfaces][0] may also be [documented][7] and [deprecated][9] via `#[graphql(description = "...")]` and `#[graphql(deprecated = "...")]`/[`#[deprecated]`][13] attributes:
```rust
# extern crate juniper;
# use juniper::graphql_interface;
#
/// This doc comment is visible only in Rust API docs.
#[graphql_interface]
#[graphql(description = "This description overwrites the one from doc comment.")]
trait Person {
/// This doc comment is visible only in Rust API docs.
#[graphql(description = "This description is visible only in GraphQL schema.")]
fn empty() -> &'static str;
#[graphql(desc = "This description is visible only in GraphQL schema.")]
// ^^^^ shortcut for a `description` argument
fn field(
#[graphql(desc = "This description is visible only in GraphQL schema.")]
arg: bool,
) -> bool;
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
#[graphql(deprecated = "Just because.")]
fn deprecated_graphql() -> bool;
// Standard Rust's `#[deprecated]` attribute works too!
#[deprecated(note = "Reason is optional, btw!")]
fn deprecated_standard() -> bool; // has no description in GraphQL schema
}
#
# fn main() {}
```
> **NOTE**: Only [GraphQL interface][0]/[object][10] fields and [GraphQL enum][11] values can be [deprecated][9].
### Ignoring
By default, all [struct][24] fields or [trait][20] methods are considered as [GraphQL fields][4]. If a helper method is needed, or it should be ignored for some reason, then it should be marked with the `#[graphql(ignore)]` attribute:
```rust
# #![allow(dead_code)]
# extern crate juniper;
# use std::marker::PhantomPinned;
# use juniper::{graphql_interface, GraphQLInterface};
#
#[derive(GraphQLInterface)]
#[graphql_interface(for = ObjA)]
struct Character {
id: i32,
#[graphql(ignore)]
_pin: PhantomPinned,
id: String,
}
#
# fn main() {}
```
```compile_fail
# extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)]
pub struct ObjA {
id: Vec<String>,
// ^^ the evaluated program panicked at
// 'Failed to implement interface `Character` on `ObjA`: Field `id`: implementer is expected to return a subtype of
// interface's return object: `[String!]!` is not a subtype of `String!`.'
}
#[graphql_interface]
trait Person {
fn name(&self) -> &str;
#[graphql_interface(for = ObjA)]
struct Character {
id: String,
}
#
# fn main() {}
```
fn age(&self) -> i32;
#[graphql(ignore)]
fn hidden_from_graphql(&self) {
// Ignored methods are allowed to have a default implementation!
### Ignoring trait methods
We may want to omit some trait methods to be assumed as [GraphQL interface][1] fields and ignore them.
```rust
# extern crate juniper;
use juniper::{graphql_interface, GraphQLObject};
#[graphql_interface(for = Human)]
trait Character {
fn id(&self) -> &str;
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
fn ignored(&self) -> u32 { 0 }
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue)]
struct Human {
id: String,
}
#
# fn main() {}
```
### Fields, arguments and interface customization
Similarly to [GraphQL objects][5] Juniper allows to fully customize [interface][1] fields and their arguments.
```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.
#[graphql(name = "myId")]
// Deprecates the field in GraphQL schema.
// Usual Rust `#[deprecated]` attribute is supported too as field deprecation,
// but `deprecated` attribute argument takes precedence over it, if specified.
#[graphql(deprecated = "Do not use it.")]
// Describes the field in GraphQL schema.
#[graphql(description = "ID of my own character.")]
// Usual Rust docs are supported too as field description,
// but `description` attribute argument takes precedence over them, if specified.
/// This description is absent in GraphQL schema.
fn id(
&self,
// Renames the argument in GraphQL schema.
#[graphql(name = "myNum")]
// Describes the argument in GraphQL schema.
#[graphql(description = "ID number of my own character.")]
// Specifies the default value for the argument.
// The concrete value may be omitted, and the `Default::default` one
// will be used in such case.
#[graphql(default = 5)]
num: i32,
) -> &str;
}
#
# fn main() {}
```
Renaming policies for all [GraphQL interface][1] fields and arguments are supported as well:
```rust
# #![allow(deprecated)]
# extern crate juniper;
use juniper::graphql_interface;
#[graphql_interface(rename_all = "none")] // disables any renaming
trait Character {
// Now exposed as `my_id` and `my_num` in the schema
fn my_id(&self, my_num: i32) -> &str;
}
#
# fn main() {}
```
### Custom context
If a [`Context`][6] is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
```rust
# extern crate juniper;
# use std::collections::HashMap;
use juniper::{graphql_interface, GraphQLObject};
struct Database {
humans: HashMap<String, Human>,
}
impl juniper::Context for Database {}
#[graphql_interface(for = Human)] // look, ma, context type is inferred! (^o^)
trait Character { // while still can be specified via `Context = ...` attribute argument
// If a field argument is named `context` or `ctx`, it's automatically assumed
// as a context argument.
fn id(&self, context: &Database) -> Option<&str>;
// Otherwise, you may mark it explicitly as a context argument.
fn name(&self, #[graphql(context)] db: &Database) -> Option<&str>;
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue, Context = Database)]
struct Human {
id: String,
name: String,
}
#
# fn main() {}
```
### Using executor and explicit generic scalar
If an [`Executor`][4] is required in a trait method to resolve a [GraphQL interface][1] field, specify it as an argument.
This requires to explicitly parametrize over [`ScalarValue`][3], as [`Executor`][4] does so.
```rust
# extern crate juniper;
use juniper::{graphql_interface, graphql_object, Executor, LookAheadMethods as _, ScalarValue};
#[graphql_interface(for = Human, Scalar = S)] // notice specifying `ScalarValue` as existing type parameter
trait Character<S: ScalarValue> {
// If a field argument is named `executor`, it's automatically assumed
// as an executor argument.
fn id<'a>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str;
// Otherwise, you may mark it explicitly as an executor argument.
fn name<'b>(
&'b self,
#[graphql(executor)] another: &Executor<'_, '_, (), S>,
) -> &'b str;
fn home_planet(&self) -> &str;
}
struct Human {
id: String,
name: String,
home_planet: String,
}
#[graphql_object(scalar = S: ScalarValue, impl = CharacterValue<S>)]
impl Human {
async fn id<'a, S>(&self, executor: &'a Executor<'_, '_, (), S>) -> &'a str
where
S: ScalarValue,
{
executor.look_ahead().field_name()
}
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
fn also_hidden_from_graphql(&self);
async fn name<'b, S>(&'b self, #[graphql(executor)] _: &Executor<'_, '_, (), S>) -> &'b str {
&self.name
}
fn home_planet<'c, S>(&'c self, #[graphql(executor)] _: &Executor<'_, '_, (), S>) -> &'c str {
// Executor may not be present on the trait method ^^^^^^^^^^^^^^^^^^^^^^^^
&self.home_planet
}
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[graphql_interface]`][3] attribute.
## `ScalarValue` considerations
By default, `#[graphql_interface]` macro generates code, which is generic over a [`ScalarValue`][3] type. This may introduce a problem when at least one of [GraphQL interface][1] implementers is restricted to a concrete [`ScalarValue`][3] type in its implementation. To resolve such problem, a concrete [`ScalarValue`][3] type should be specified.
```rust
# extern crate juniper;
use juniper::{graphql_interface, DefaultScalarValue, GraphQLObject};
#[graphql_interface(for = [Human, Droid])]
#[graphql_interface(scalar = DefaultScalarValue)] // removing this line will fail compilation
trait Character {
fn id(&self) -> &str;
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(impl = CharacterValue, Scalar = DefaultScalarValue)]
struct Droid {
id: String,
primary_function: String,
}
#
# fn main() {}
```
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Interfaces
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLInterface.html
[3]: https://docs.rs/juniper/0.16.1/juniper/attr.graphql_interface.html
[4]: https://spec.graphql.org/October2021#sec-Language.Fields
[5]: https://spec.graphql.org/October2021#sec-Language.Arguments
[6]: https://spec.graphql.org/October2021#sec-Non-Null
[7]: https://spec.graphql.org/October2021#sec-Descriptions
[9]: https://spec.graphql.org/October2021#sec--deprecated
[10]: https://spec.graphql.org/October2021#sec-Objects
[11]: https://spec.graphql.org/October2021#sec-Enums
[13]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
[20]: https://doc.rust-lang.org/reference/items/traits.html#traits
[21]: https://doc.rust-lang.org/reference/types/trait-object.html#trait-objects
[22]: https://doc.rust-lang.org/reference/items/enumerations.html#enumerations
[23]: https://doc.rust-lang.org/reference/items/implementations.html#trait-implementations
[24]: https://doc.rust-lang.org/reference/items/structs.html
[30]: https://en.wikipedia.org/wiki/Structural_type_system
[31]: https://en.wikipedia.org/wiki/Type_class
[1]: https://spec.graphql.org/October2021#sec-Interfaces
[2]: https://doc.rust-lang.org/reference/types/trait-object.html
[3]: https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html
[4]: https://docs.rs/juniper/latest/juniper/struct.Executor.html
[5]: https://spec.graphql.org/October2021#sec-Objects
[6]: https://docs.rs/juniper/0.14.2/juniper/trait.Context.html

View file

@ -1,169 +1,15 @@
Complex fields
==============
# Complex fields
Using a plain [Rust struct][struct] for representing a [GraphQL object][0] is easy and trivial but does not cover every case. What if we need to express something non-trivial as a [GraphQL field][4], such as:
- Calling non-trivial logic while [executing][1] the [field][4] (like querying database, etc.).
- Accepting [field arguments][5].
- Defining a circular [GraphQL object][0], where one of its [fields][4] returns the type itself.
- Using some other (non-[struct]) [Rust] type to represent a [GraphQL object][0].
If you've got a struct that can't be mapped directly to GraphQL, that contains
computed fields or circular structures, you have to use a more powerful tool:
the `#[graphql_object]` procedural macro. This macro lets you define GraphQL object
fields in a Rust `impl` block for a type. Note, that GraphQL fields are defined in
this `impl` block by default. If you want to define normal methods on the struct,
you have to do so either in a separate "normal" `impl` block, or mark them with
`#[graphql(ignore)]` attribute to be omitted by the macro. Continuing with the
example from the last chapter, this is how you would define `Person` using the
macro:
To support these more complicated use cases, we need a way to define a [GraphQL field][4] as a function. In [Juniper] this is achievable by placing the [`#[graphql_object]` attribute][3] on an [`impl` block][6], which turns its methods into [GraphQL fields][4]:
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
struct House {
inhabitants: Vec<Person>,
}
// Defines the `House` GraphQL object.
#[graphql_object]
impl House {
// Creates the field `inhabitantWithName(name: String!)`,
// returning a `null`able `Person`.
fn inhabitant_with_name(&self, name: String) -> Option<&Person> {
self.inhabitants.iter().find(|p| p.name == name)
}
}
#
# fn main() {}
```
> **NOTE**: To access global data such as database connections or authentication information, a _context_ is used. To learn more about this, see the ["Context" chapter](context.md).
### Default arguments
Though [Rust] doesn't have the notion of default arguments, [GraphQL arguments][4] are able to have default values. These default values are used when a GraphQL operation doesn't specify the argument explicitly. In [Juniper], defining a default value for a [GraphQL argument][4] is enabled by the `#[graphql(default)]` attribute:
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
#[graphql_object]
impl Person {
fn field1(
// Default value can be any valid Rust expression, including a function
// call, etc.
#[graphql(default = true)]
arg1: bool,
// If default expression is not specified, then the `Default::default()`
// value is used.
#[graphql(default)]
arg2: i32,
) -> String {
format!("{arg1} {arg2}")
}
}
#
# fn main() {}
```
### Renaming
Like with the [`#[derive(GraphQLObject)]` attribute on structs](index.md#renaming), [field][4] names are converted from [Rust]'s standard `snake_case` naming convention into [GraphQL]'s `camelCase` convention.
We can override the name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
#[graphql_object]
#[graphql(name = "PersonObject")]
impl Person { // exposed as `PersonObject` in GraphQL schema
#[graphql(name = "myCustomFieldName")]
fn renamed_field( // exposed as `myCustomFieldName` in GraphQL schema
#[graphql(name = "myArgument")]
renamed_argument: bool, // exposed as `myArgument` in GraphQL schema
) -> bool {
renamed_argument
}
}
#
# fn main() {}
```
Or provide a different renaming policy for all the defined [fields][4]:
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
#[graphql_object]
#[graphql(rename_all = "none")] // disables any renaming
impl Person {
fn renamed_field( // exposed as `renamed_field` in GraphQL schema
renamed_argument: bool, // exposed as `renamed_argument` in GraphQL schema
) -> bool {
renamed_argument
}
}
#
# fn main() {}
```
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
### Documentation and deprecation
Similarly, [GraphQL fields][4] may also be [documented][7] and [deprecated][9] via `#[graphql(description = "...")]` and `#[graphql(deprecated = "...")]`/[`#[deprecated]`][13] attributes:
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
/// This doc comment is visible only in Rust API docs.
#[graphql_object]
#[graphql(description = "This description overwrites the one from doc comment.")]
impl Person {
/// This doc comment is visible only in Rust API docs.
#[graphql(description = "This description is visible only in GraphQL schema.")]
fn empty() -> &'static str {
""
}
#[graphql(desc = "This description is visible only in GraphQL schema.")]
// ^^^^ shortcut for a `description` argument
fn field(
#[graphql(desc = "This description is visible only in GraphQL schema.")]
arg: bool,
) -> bool {
arg
}
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
#[graphql(deprecated = "Just because.")]
fn deprecated_graphql() -> bool {
true
}
// Standard Rust's `#[deprecated]` attribute works too!
#[deprecated(note = "Reason is optional, btw!")]
fn deprecated_standard() -> bool { // has no description in GraphQL schema
false
}
}
#
# fn main() {}
```
> **NOTE**: Only [GraphQL object][0]/[interface][11] fields and [GraphQL enum][10] values can be [deprecated][9].
### Ignoring
By default, all methods of an [`impl` block][6] are exposed as [GraphQL fields][4]. If a method should not be exposed as a [GraphQL field][4], it should be defined in a separate [`impl` block][6] or marked with the `#[graphql(ignore)]` attribute:
```rust
# #![allow(dead_code)]
# extern crate juniper;
@ -186,44 +32,175 @@ impl Person {
#[graphql(ignore)]
pub fn hidden_from_graphql(&self) {
// whatever goes...
}
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
pub fn also_hidden_from_graphql(&self) {
// whatever goes...
// [...]
}
}
impl Person {
pub fn not_even_considered_for_graphql(&self) {
// whatever goes...
pub fn hidden_from_graphql2(&self) {
// [...]
}
}
#
# fn main() { }
```
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:
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
struct House {
inhabitants: Vec<Person>,
}
#[graphql_object]
impl House {
// Creates the field `inhabitantWithName(name)`, returning a nullable `Person`.
fn inhabitant_with_name(&self, name: String) -> Option<&Person> {
self.inhabitants.iter().find(|p| p.name == name)
}
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[graphql_object]`][3] attribute.
To access global data such as database connections or authentication
information, a _context_ is used. To learn more about this, see the next
chapter: [Using contexts](using_contexts.md).
## Description, renaming, and deprecation
Like with the derive attribute, field names will be converted from `snake_case`
to `camelCase`. If you need to override the conversion, you can simply rename
the field. Also, the type name can be changed with an alias:
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[struct]: https://doc.rust-lang.org/reference/items/structs.html
/// Doc comments are used as descriptions for GraphQL.
#[graphql_object(
// With this attribute you can change the public GraphQL name of the type.
name = "PersonObject",
[0]: https://spec.graphql.org/October2021#sec-Objects
[1]: https://spec.graphql.org/October2021#sec-Execution
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLObject.html
[3]: https://docs.rs/juniper/0.16.1/juniper/attr.graphql_object.html
[4]: https://spec.graphql.org/October2021#sec-Language.Fields
[5]: https://spec.graphql.org/October2021#sec-Language.Arguments
[6]: https://doc.rust-lang.org/reference/items/implementations.html#inherent-implementations
[7]: https://spec.graphql.org/October2021#sec-Descriptions
[9]: https://spec.graphql.org/October2021#sec--deprecated
[10]: https://spec.graphql.org/October2021#sec-Enums
[11]: https://spec.graphql.org/October2021#sec-Interfaces
[13]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
// You can also specify a description here, which will overwrite
// a doc comment description.
description = "...",
)]
impl Person {
/// A doc comment on the field will also be used for GraphQL.
#[graphql(
// Or provide a description here.
description = "...",
)]
fn doc_comment(&self) -> &str {
""
}
// Fields can also be renamed if required.
#[graphql(name = "myCustomFieldName")]
fn renamed_field() -> bool {
true
}
// Deprecations also work as you'd expect.
// Both the standard Rust syntax and a custom attribute is accepted.
#[deprecated(note = "...")]
fn deprecated_standard() -> bool {
false
}
#[graphql(deprecated = "...")]
fn deprecated_graphql() -> bool {
true
}
}
#
# fn main() { }
```
Or provide a different renaming policy on a `impl` block for all its fields:
```rust
# extern crate juniper;
# use juniper::graphql_object;
struct Person;
#[graphql_object(rename_all = "none")] // disables any renaming
impl Person {
// Now exposed as `renamed_field` in the schema
fn renamed_field() -> bool {
true
}
}
#
# fn main() {}
```
## Customizing arguments
Method field arguments can also be customized.
They can have custom descriptions and default values.
```rust
# extern crate juniper;
# use juniper::graphql_object;
#
struct Person;
#[graphql_object]
impl Person {
fn field1(
&self,
#[graphql(
// Arguments can also be renamed if required.
name = "arg",
// 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..."
)]
arg1: bool,
// If default expression is not specified then `Default::default()` value is used.
#[graphql(default)]
arg2: i32,
) -> String {
format!("{arg1} {arg2}")
}
}
#
# fn main() { }
```
Provide a different renaming policy on a `impl` block also implies for arguments:
```rust
# extern crate juniper;
# use juniper::graphql_object;
struct Person;
#[graphql_object(rename_all = "none")] // disables any renaming
impl Person {
// Now exposed as `my_arg` in the schema
fn field(my_arg: bool) -> bool {
my_arg
}
}
#
# fn main() {}
```
## More features
These, and more features, are described more thoroughly in [the reference documentation](https://docs.rs/juniper/latest/juniper/attr.graphql_object.html).

View file

@ -1,156 +0,0 @@
Context
=======
_Context_ is a feature in [Juniper] that lets [field][4] resolvers access global data, most commonly database connections or authentication information.
Let's say that we have a simple `User`s database in a `HashMap`:
```rust
# #![allow(dead_code)]
# use std::collections::HashMap;
#
struct Database {
users: HashMap<i32, User>,
}
struct User {
id: i32,
name: String,
friend_ids: Vec<i32>,
}
#
# fn main() {}
```
We would like to define a `friends` [field][4] on `User` that returns a list of `User` [objects][0]. In order to write such a [field][4] we need to query a `Database`. To accomplish this we must first mark the `Database` as a valid context type and then assign it to the `User` [object][0]. To gain access to the context in the `friends` [field][4], we need to specify an argument with the same type as the specified context:
```rust
# extern crate juniper;
# use std::collections::HashMap;
# use juniper::graphql_object;
#
struct Database {
users: HashMap<i32, User>,
}
// Mark the `Database` as a valid context type for Juniper.
impl juniper::Context for Database {}
struct User {
id: i32,
name: String,
friend_ids: Vec<i32>,
}
#[graphql_object]
#[graphql(context = Database)] // assign `Database` as the context type
impl User {
// Inject the `Database` context by specifying an argument with the
// context type:
// - the type must be a reference;
// - the name of the argument SHOULD be `context` (or `ctx`).
fn friends<'db>(&self, context: &'db Database) -> Vec<&'db User> {
// ^^^^^^^ or `ctx`, up to your preference
self.friend_ids.iter()
.map(|id| {
context.users.get(&id).expect("could not find `User` with ID")
})
.collect()
}
fn friend<'db>(
&self,
id: i32,
// Alternatively, the context argument may be marked with an attribute,
// and thus, named arbitrary.
#[graphql(context)] db: &'db Database,
// ^^^^^^^ or `ctx`, up to your preference
) -> Option<&'db User> {
self.friend_ids.contains(&id).then(|| {
db.users.get(&id).expect("could not find `User` with ID")
})
}
fn name(&self) -> &str {
self.name.as_str()
}
fn id(&self) -> i32 {
self.id
}
}
#
# fn main() {}
```
### Mutating and mutable references
Context cannot be a mutable reference as [fields][4] may be resolved concurrently. If something in the context requires a mutable reference, the context type should leverage the [_interior mutability_ pattern][5] (e.g. use `RwLock`, `RefCell` or similar).
For example, when using async runtime with [work stealing][6] (like [`tokio`]), which obviously requires thread safety in addition, we will need to use a corresponding async version of `RwLock`:
```rust
# extern crate juniper;
# extern crate tokio;
# use std::collections::HashMap;
# use juniper::graphql_object;
use tokio::sync::RwLock;
struct Database {
requested_count: HashMap<i32, i32>,
}
// Since we cannot directly implement `juniper::Context`
// for `RwLock`, we use the newtype idiom.
struct DatabaseContext(RwLock<Database>);
impl juniper::Context for DatabaseContext {}
struct User {
id: i32,
name: String
}
#[graphql_object]
#[graphql(context = DatabaseContext)]
impl User {
async fn times_requested<'db>(&self, ctx: &'db DatabaseContext) -> i32 {
// Acquire a mutable reference and `.await` if async `RwLock` is used,
// which is necessary if context consists of async operations like
// querying remote databases.
// Obtain base type.
let DatabaseContext(db) = ctx;
// If context is immutable use `.read()` on `RwLock` instead.
let mut db = db.write().await;
// Perform a mutable operation.
db.requested_count
.entry(self.id)
.and_modify(|e| *e += 1)
.or_insert(1)
.clone()
}
fn name(&self) -> &str {
self.name.as_str()
}
fn id(&self) -> i32 {
self.id
}
}
#
# fn main() {}
```
> **TIP**: Replace `tokio::sync::RwLock` with `std::sync::RwLock` (or similar) if you don't intend to use async resolving.
[`tokio`]: https://docs.rs/tokio
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Objects
[4]: https://spec.graphql.org/October2021#sec-Language.Fields
[5]: https://doc.rust-lang.org/reference/interior-mutability.html#interior-mutability
[6]: https://en.wikipedia.org/wiki/Work_stealing

View file

@ -0,0 +1,216 @@
# Defining objects
While any type in Rust can be exposed as a GraphQL object, the most common one
is a struct.
There are two ways to create a GraphQL object in Juniper. If you've got a simple
struct you want to expose, the easiest way is to use the custom derive
attribute. The other way is described in the [Complex fields](complex_fields.md)
chapter.
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
#
# fn main() {}
```
This will create a GraphQL object type called `Person`, with two fields: `name`
of type `String!`, and `age` of type `Int!`. Because of Rust's type system,
everything is exported as non-null by default. If you need a nullable field, you
can use `Option<T>`.
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:
!FILENAME GraphQL descriptions via Rust doc comments
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
/// Information about a person
struct Person {
/// The person's full name, including both first and last names
name: String,
/// The person's age in years, rounded down
age: i32,
}
#
# fn main() {}
```
Objects and fields without doc comments can instead set a `description`
via the `graphql` attribute. The following example is equivalent to the above:
!FILENAME GraphQL descriptions via attribute
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
#[graphql(description = "Information about a person")]
struct Person {
#[graphql(description = "The person's full name, including both first and last names")]
name: String,
#[graphql(description = "The person's age in years, rounded down")]
age: i32,
}
#
# fn main() {}
```
Descriptions set via the `graphql` attribute take precedence over Rust
doc comments. This enables internal Rust documentation and external GraphQL
documentation to differ:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
#[graphql(description = "This description shows up in GraphQL")]
/// This description shows up in RustDoc
struct Person {
#[graphql(description = "This description shows up in GraphQL")]
/// This description shows up in RustDoc
name: String,
/// This description shows up in both RustDoc and GraphQL
age: i32,
}
#
# fn main() {}
```
## Relationships
You can only use the custom derive attribute under these circumstances:
- The annotated type is a `struct`,
- Every struct field is either
- A primitive type (`i32`, `f64`, `bool`, `String`, `juniper::ID`), or
- A valid custom GraphQL type, e.g. another struct marked with this attribute,
or
- A container/reference containing any of the above, e.g. `Vec<T>`, `Box<T>`,
`Option<T>`
Let's see what that means for building relationships between objects:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
#[derive(GraphQLObject)]
struct House {
address: Option<String>, // Converted into String (nullable)
inhabitants: Vec<Person>, // Converted into [Person!]!
}
#
# fn main() {}
```
Because `Person` is a valid GraphQL type, you can have a `Vec<Person>` in a
struct and it'll be automatically converted into a list of non-nullable `Person`
objects.
## Renaming fields
By default, struct fields are converted from Rust's standard `snake_case` naming
convention into GraphQL's `camelCase` convention:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
first_name: String, // Would be exposed as firstName in the GraphQL schema
last_name: String, // Exposed as lastName
}
#
# fn main() {}
```
You can override the name by using the `graphql` attribute on individual struct
fields:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
#[graphql(name = "websiteURL")]
website_url: Option<String>, // now exposed as `websiteURL` in the schema
}
#
# fn main() {}
```
Or provide a different renaming policy on a struct for all its fields:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
#[graphql(rename_all = "none")] // disables any renaming
struct Person {
name: String,
age: i32,
website_url: Option<String>, // now exposed as `website_url` in the schema
}
#
# fn main() {}
```
## Deprecating fields
To deprecate a field, you specify a deprecation reason using the `graphql`
attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
#[graphql(deprecated = "Please use the name field instead")]
first_name: String,
}
#
# fn main() {}
```
The `name`, `description`, and `deprecation` arguments can of course be
combined. Some restrictions from the GraphQL spec still applies though; you can
only deprecate object fields and enum values.
## Ignoring fields
By default, all fields in a `GraphQLObject` are included in the generated GraphQL type. To prevent including a specific field, annotate the field with `#[graphql(ignore)]`:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
#[graphql(ignore)]
# #[allow(dead_code)]
password_hash: String, // cannot be queried or modified from GraphQL
}
#
# fn main() {}
```

View file

@ -1,183 +0,0 @@
Field errors
============
[Rust] provides [two ways of dealing with errors][11]:
- [`Result<T, E>`][12] for recoverable errors;
- [`panic!`][13] for unrecoverable errors.
[Juniper] does not do anything about panicking, it naturally bubbles up to the surrounding code/framework and can be dealt with there.
For recoverable errors, [Juniper] works well with the [built-in `Result` type][12]. You can use the [`?` operator][14] and things will work as you expect them to:
```rust
# extern crate juniper;
# use std::{fs::File, io::Read, path::PathBuf, str};
# use juniper::{graphql_object, FieldResult};
#
struct Example {
filename: PathBuf,
}
#[graphql_object]
impl Example {
fn contents(&self) -> FieldResult<String> {
let mut file = File::open(&self.filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn foo() -> FieldResult<Option<String>> {
// Some invalid bytes.
let invalid = vec![128, 223];
Ok(Some(str::from_utf8(&invalid)?.to_string()))
}
}
#
# fn main() {}
```
[`FieldResult<T>`][21] is an alias for [`Result<T, FieldError>`][22], which is the [error type][1] all fallible [fields][6] must return. By using the [`?` operator][14], any type that implements the [`Display` trait][15] (which most of the error types out there do) can be automatically converted into a [`FieldError`][22].
> **TIP**: If a custom conversion into a [`FieldError`][22] is needed (to [fill up `extensions`][2], for example), the [`IntoFieldError` trait][23] should be implemented.
> **NOTE**: [`FieldError`][22]s are [GraphQL field errors][1] and are [not visible][9] in a [GraphQL schema][8] in any way.
## Error payloads, `null`, and partial errors
[Juniper]'s error behavior conforms to the [GraphQL specification][0].
When a [field][6] returns an [error][11], the [field][6]'s result is replaced by `null`, and an additional `errors` object is created at the top level of the [response][7], and the [execution][5] is resumed.
Let's run the following query against the previous example:
```graphql
{
example {
contents
foo
}
}
```
If `str::from_utf8` results in a `std::str::Utf8Error`, then the following will be returned:
```json
{
"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}]
}]
}
```
> Since [`Non-Null` type][4] [fields][5] cannot be **null**, [field errors][1] are propagated to be handled by the parent [field][5]. If the parent [field][5] may be **null** then it resolves to **null**, otherwise if it is a [`Non-Null` type][4], the [field error][1] is further propagated to its parent [field][5].
For example, with the following query:
```graphql
{
example {
contents
}
}
```
If the `File::open()` above results in a `std::io::ErrorKind::PermissionDenied`, the following ill be returned:
```json
{
"data": null,
"errors": [{
"message": "Permission denied (os error 13)",
"locations": [{"line": 2, "column": 4}]
}]
}
```
## Additional information
Sometimes it's desirable to return additional structured error information to clients. This can be accomplished by implementing the [`IntoFieldError` trait][23]:
```rust
# #[macro_use] extern crate juniper;
# use juniper::{graphql_object, FieldError, IntoFieldError, ScalarValue};
#
enum CustomError {
WhateverNotSet,
}
impl<S: ScalarValue> IntoFieldError<S> for CustomError {
fn into_field_error(self) -> FieldError<S> {
match self {
Self::WhateverNotSet => FieldError::new(
"Whatever does not exist",
graphql_value!({
"type": "NO_WHATEVER"
}),
),
}
}
}
struct Example {
whatever: Option<bool>,
}
#[graphql_object]
impl Example {
fn whatever(&self) -> Result<bool, CustomError> {
if let Some(value) = self.whatever {
return Ok(value);
}
Err(CustomError::WhateverNotSet)
}
}
#
# fn main() {}
```
And the specified structured error information will be included into the [error's `extensions`][2]:
```json
{
"errors": [{
"message": "Whatever does not exist",
"locations": [{"line": 2, "column": 4}],
"extensions": {
"type": "NO_WHATEVER"
}
}]
}
```
> **NOTE**: This pattern is particularly useful when it comes to instrumentation of returned [field errors][1] with custom error codes or additional diagnostics (like stack traces or tracing IDs).
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Handling-Field-Errors
[1]: https://spec.graphql.org/October2021#sec-Errors.Field-errors
[2]: https://spec.graphql.org/October2021#sel-GAPHRPZCAACCC_7Q
[4]: https://spec.graphql.org/October2021#sec-Non-Null
[5]: https://spec.graphql.org/October2021#sec-Execution
[6]: https://spec.graphql.org/October2021#sec-Language.Fields
[7]: https://spec.graphql.org/October2021#sec-Response
[8]: https://graphql.org/learn/schema
[9]: https://spec.graphql.org/October2021#sec-Introspection
[11]: https://doc.rust-lang.org/book/ch09-00-error-handling.html
[12]: https://doc.rust-lang.org/stable/std/result/enum.Result.html
[13]: https://doc.rust-lang.org/stable/std/macro.panic.html
[14]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
[15]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
[21]: https://docs.rs/juniper/0.16.1/juniper/executor/type.FieldResult.html
[22]: https://docs.rs/juniper/0.16.1/juniper/executor/struct.FieldError.html
[23]: https://docs.rs/juniper/0.16.1/juniper/executor/trait.IntoFieldError.html

View file

@ -1,26 +0,0 @@
Error handling
==============
Error handling in [GraphQL] can be done in multiple ways. We will cover the two different error handling models mostly used:
1. [Implicit field results](field.md).
2. [Explicit errors backend by GraphQL schema](schema.md).
Choosing the right error handling method depends on the requirements of the application and the concrete error happening. Investigating both approaches is beneficial.
## Comparison
The [first approach](field.md) (where every error is a [field error][1]) is easier to implement. However, clients won't know what errors may occur and instead will have to infer what happens from the [error message][2]. This is brittle and could change over time due to either clients or server changing. Therefore, extensive integration testing between clients and server is required to maintain the implicit contract between the two.
[Encoding non-critical errors in a GraphQL schema](schema.md) makes the contract between clients and the server explicit. This allows clients to understand and handle these errors correctly and the server to know when changes are potentially breaking clients. However, encoding this error information into a [GraphQL schema][8] requires additional code and up-front definition of non-critical errors.
[GraphQL]: https://graphql.org
[1]: https://spec.graphql.org/October2021#sec-Errors.Field-errors
[2]: https://spec.graphql.org/October2021/#sel-GAPHRPDCAACCyD57Z
[8]: https://graphql.org/learn/schema

View file

@ -1,336 +0,0 @@
Schema errors
=============
[Rust]'s model of errors can be adapted for [GraphQL]. [Rust]'s panic is similar to a [field error][1] - the whole query is aborted and nothing can be extracted (except for error related information).
Not all errors require this strict handling. Recoverable or partial errors can be put into a [GraphQL schema][8], so the client can intelligently handle them.
To implement this approach, all errors must be partitioned into two classes:
- _Critical_ errors that cannot be fixed by clients (e.g. a database error).
- _Recoverable_ errors that can be fixed by clients (e.g. invalid input data).
Critical errors are returned from resolvers as [field errors][1] (from the [previous chapter](field.md)). Recoverable errors are part of a [GraphQL schema][8] and can be handled gracefully by clients. Similar to [Rust], [GraphQL] allows similar error models with [unions][9] (see ["Unions" chapter](../../unions.md)).
### Example: Simple
In this example, basic input validation is implemented with [GraphQL types][7]. [Strings][5] are used to identify the problematic [field][6] name. Errors for a particular [field][6] are also returned as a [string][5].
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationError {
field: String,
message: String,
}
#[derive(GraphQLObject)]
pub struct ValidationErrors {
errors: Vec<ValidationError>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrors),
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn add_item(&self, name: String, quantity: i32) -> GraphQLResult {
let mut errors = Vec::new();
if !(10 <= name.len() && name.len() <= 100) {
errors.push(ValidationError {
field: "name".into(),
message: "between 10 and 100".into(),
});
}
if !(1 <= quantity && quantity <= 10) {
errors.push(ValidationError {
field: "quantity".into(),
message: "between 1 and 10".into(),
});
}
if errors.is_empty() {
GraphQLResult::Ok(Item { name, quantity })
} else {
GraphQLResult::Err(ValidationErrors { errors })
}
}
}
#
# fn main() {}
```
Each function may have a different return type and depending on the input parameters a new result type may be required. For example, adding a `User` would require a new result type containing the variant `Ok(User)`instead of `Ok(Item)`.
> **NOTE**: In this example the returned [string][5] 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 its users.
The client can send a mutation request and handle the resulting errors in the following manner:
```graphql
{
mutation {
addItem(name: "", quantity: 0) {
... on Item {
name
}
... on ValidationErrors {
errors {
field
message
}
}
}
}
}
```
> **NOTE**: 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.
### Example: Complex
Instead of using [strings][5] to propagate errors, it is possible to use [GraphQL type system][7] to describe the errors more precisely.
For each fallible [input argument][4] we create a [field][6] in a [GraphQL object][10]. The [field][6] is set if the validation for that particular [argument][4] fails.
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationError {
name: Option<String>,
quantity: Option<String>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationError),
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn add_item(&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".into());
}
if !(1 <= quantity && quantity <= 10) {
error.quantity = Some("between 1 and 10".into());
}
if error.name.is_none() && error.quantity.is_none() {
GraphQLResult::Ok(Item { name, quantity })
} else {
GraphQLResult::Err(error)
}
}
}
#
# fn main() {}
```
> **NOTE**: We 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 `ValidationResult` which contains only [fields][6] provided by the function.
So, all the expected errors are handled directly inside the query. Additionally, all non-critical errors are known in advance by both the server and the client:
```graphql
{
mutation {
addItem {
... on Item {
name
}
... on ValidationErrorsItem {
name
quantity
}
}
}
}
```
### Example: Complex with critical errors
Our examples so far have only included non-critical errors. Providing errors inside a [GraphQL schema][8] still allows us to return unexpected critical errors when they occur.
In the following example, a theoretical database could fail and would generate errors. Since it is not common for a database to fail, the corresponding error is returned as a [critical error][1]:
```rust
# extern crate juniper;
# use juniper::{graphql_object, graphql_value, FieldError, GraphQLObject, GraphQLUnion, ScalarValue};
#
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationErrorItem {
name: Option<String>,
quantity: Option<String>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrorItem),
}
pub enum ApiError {
Database,
}
impl<S: ScalarValue> juniper::IntoFieldError<S> for ApiError {
fn into_field_error(self) -> FieldError<S> {
match self {
Self::Database => FieldError::new(
"Internal database error",
graphql_value!({"type": "DATABASE"}),
),
}
}
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn add_item(&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".into());
}
if !(1 <= quantity && quantity <= 10) {
error.quantity = Some("between 1 and 10".into());
}
if error.name.is_none() && error.quantity.is_none() {
Ok(GraphQLResult::Ok(Item { name, quantity }))
} else {
Ok(GraphQLResult::Err(error))
}
}
}
#
# fn main() {}
```
### Example: Shopify API
The [Shopify API] implements a similar approach. Their API is a good reference to explore this approach in a real world application.
### Example: Non-struct [objects][10]
Up until now, we've only looked at mapping [structs][20] to [GraphQL objects][10]. However, any [Rust] type can be exposed a [GraphQL object][10].
Using `Result`-like [enums][1] can be a useful way of reporting validation errors from a mutation:
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
#
#[derive(GraphQLObject)]
struct User {
name: String,
}
#[derive(GraphQLObject)]
struct ValidationError {
field: String,
message: String,
}
enum SignUpResult {
Ok(User),
Error(Vec<ValidationError>),
}
#[graphql_object]
impl SignUpResult {
fn user(&self) -> Option<&User> {
match self {
Self::Ok(user) => Some(user),
Self::Error(_) => None,
}
}
fn error(&self) -> Option<&[ValidationError]> {
match self {
Self::Ok(_) => None,
Self::Error(errs) => Some(errs.as_slice())
}
}
}
#
# fn main() {}
```
Here, we use an [enum][21] to decide whether a client's input data is valid or not, and it could be used as the result of e.g. a `signUp` mutation:
```graphql
{
mutation {
signUp(name: "wrong") {
user {
name
}
error {
field
message
}
}
}
}
```
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[Shopify API]: https://shopify.dev/docs/admin-api/graphql/reference
[1]: https://spec.graphql.org/October2021#sec-Errors.Field-errors
[4]: https://spec.graphql.org/October2021#sec-Language.Arguments
[5]: https://spec.graphql.org/October2021#sec-String
[6]: https://spec.graphql.org/October2021#sec-Language.Fields
[7]: https://spec.graphql.org/October2021#sec-Types
[8]: https://graphql.org/learn/schema
[9]: https://spec.graphql.org/October2021#sec-Unions
[10]: https://spec.graphql.org/October2021#sec-Objects
[20]: https://doc.rust-lang.org/reference/items/structs.html
[21]: https://doc.rust-lang.org/reference/items/enumerations.html

View file

@ -0,0 +1,467 @@
# Error handling
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.
## Field Results
Rust
[provides](https://doc.rust-lang.org/book/second-edition/ch09-00-error-handling.html)
two ways of dealing with errors: `Result<T, E>` for recoverable errors and
`panic!` for unrecoverable errors. Juniper does not do anything about panicking;
it will bubble up to the surrounding framework and hopefully be dealt with
there.
For recoverable errors, Juniper works well with the built-in `Result` type, you
can use the `?` operator and things will generally just work as you expect them to:
```rust
# extern crate juniper;
use std::{
str,
path::PathBuf,
fs::{File},
io::{Read},
};
use juniper::{graphql_object, FieldResult};
struct Example {
filename: PathBuf,
}
#[graphql_object]
impl Example {
fn contents(&self) -> FieldResult<String> {
let mut file = File::open(&self.filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn foo() -> FieldResult<Option<String>> {
// Some invalid bytes.
let invalid = vec![128, 223];
Ok(Some(str::from_utf8(&invalid)?.to_string()))
}
}
#
# fn main() {}
```
`FieldResult<T>` is an alias for `Result<T, FieldError>`, which is the error
type all fields must return. By using the `?` operator or `try!` macro, any type
that implements the `Display` trait - which are most of the error types out
there - those errors are automatically converted into `FieldError`.
## Error payloads, `null`, and partial errors
Juniper's error behavior conforms to the [GraphQL specification](https://spec.graphql.org/October2021#sec-Handling-Field-Errors).
When a field returns an error, the field's result is replaced by `null`, an
additional `errors` 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:
```graphql
{
example {
contents
foo
}
}
```
If `str::from_utf8` resulted in a `std::str::Utf8Error`, the following would be
returned:
!FILENAME Response for nullable field with error
```json
{
"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 }])
]
}
```
If an error is returned from a non-null field, such as the
example above, the `null` value is propagated up to the first nullable parent
field, or the root `data` object if there are no nullable fields.
For example, with the following query:
```graphql
{
example {
contents
}
}
```
If `File::open()` above resulted in `std::io::ErrorKind::PermissionDenied`, the
following would be returned:
!FILENAME Response for non-null field with error and no nullable parent
```json
{
"errors": [
"message": "Permission denied (os error 13)",
"locations": [{ "line": 2, "column": 4 }])
]
}
```
### Structured errors
Sometimes it is desirable to return additional structured error information
to clients. This can be accomplished by implementing [`IntoFieldError`](https://docs.rs/juniper/latest/juniper/trait.IntoFieldError.html):
```rust
# #[macro_use] extern crate juniper;
# use juniper::{graphql_object, FieldError, IntoFieldError, ScalarValue};
#
enum CustomError {
WhateverNotSet,
}
impl<S: ScalarValue> IntoFieldError<S> for CustomError {
fn into_field_error(self) -> FieldError<S> {
match self {
CustomError::WhateverNotSet => FieldError::new(
"Whatever does not exist",
graphql_value!({
"type": "NO_WHATEVER"
}),
),
}
}
}
struct Example {
whatever: Option<bool>,
}
#[graphql_object]
impl Example {
fn whatever(&self) -> Result<bool, CustomError> {
if let Some(value) = self.whatever {
return Ok(value);
}
Err(CustomError::WhateverNotSet)
}
}
#
# fn main() {}
```
The specified structured error information is included in the [`extensions`](https://spec.graphql.org/October2021#sec-Errors) key:
```json
{
"errors": [{
"message": "Whatever does not exist",
"locations": [{"line": 2, "column": 4}],
"extensions": {
"type": "NO_WHATEVER"
}
}]
}
```
## Errors Backed by GraphQL's Schema
Rust's model of errors can be adapted for GraphQL. Rust's panic is
similar to a `FieldError`--the whole query is aborted and nothing can
be extracted (except for error related information).
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.
To implement this approach, all errors must be partitioned into two error classes:
* Critical errors that cannot be fixed by the user (e.g. a database error).
* Recoverable errors that can be fixed by the user (e.g. invalid input data).
Critical errors are returned from resolvers as `FieldErrors` (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).
### Example Input Validation (simple)
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.
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationError {
field: String,
message: String,
}
#[derive(GraphQLObject)]
pub struct ValidationErrors {
errors: Vec<ValidationError>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrors),
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn addItem(&self, name: String, quantity: i32) -> GraphQLResult {
let mut errors = Vec::new();
if !(10 <= name.len() && name.len() <= 100) {
errors.push(ValidationError {
field: "name".into(),
message: "between 10 and 100".into(),
});
}
if !(1 <= quantity && quantity <= 10) {
errors.push(ValidationError {
field: "quantity".into(),
message: "between 1 and 10".into(),
});
}
if errors.is_empty() {
GraphQLResult::Ok(Item { name, quantity })
} else {
GraphQLResult::Err(ValidationErrors { errors })
}
}
}
#
# fn main() {}
```
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 `Ok(User)`
instead of `Ok(Item)`.
The client can send a mutation request and handle the
resulting errors as shown in the following example:
```graphql
{
mutation {
addItem(name: "", quantity: 0) {
... on Item {
name
}
... on ValidationErrors {
errors {
field
message
}
}
}
}
}
```
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.
### Example Input Validation (complex)
Instead of using strings to propagate errors, it is possible to use
GraphQL's type system to describe the errors more precisely.
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 `ValidationResult` which
contains only fields provided by the function.
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject, GraphQLUnion};
#
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationError {
name: Option<String>,
quantity: Option<String>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationError),
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn addItem(&self, name: String, quantity: i32) -> GraphQLResult {
let mut error = ValidationError {
name: None,
quantity: None,
};
if !(10 <= name.len() && name.len() <= 100) {
error.name = Some("between 10 and 100".into());
}
if !(1 <= quantity && quantity <= 10) {
error.quantity = Some("between 1 and 10".into());
}
if error.name.is_none() && error.quantity.is_none() {
GraphQLResult::Ok(Item { name, quantity })
} else {
GraphQLResult::Err(error)
}
}
}
#
# fn main() {}
```
```graphql
{
mutation {
addItem {
... on Item {
name
}
... on ValidationErrorsItem {
name
quantity
}
}
}
}
```
Expected errors are handled directly inside the query. Additionally, all
non-critical errors are known in advance by both the server and the client.
### Example Input Validation (complex with critical error)
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.
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:
```rust
# extern crate juniper;
#
use juniper::{graphql_object, graphql_value, FieldError, GraphQLObject, GraphQLUnion, ScalarValue};
#[derive(GraphQLObject)]
pub struct Item {
name: String,
quantity: i32,
}
#[derive(GraphQLObject)]
pub struct ValidationErrorItem {
name: Option<String>,
quantity: Option<String>,
}
#[derive(GraphQLUnion)]
pub enum GraphQLResult {
Ok(Item),
Err(ValidationErrorItem),
}
pub enum ApiError {
Database,
}
impl<S: ScalarValue> juniper::IntoFieldError<S> for ApiError {
fn into_field_error(self) -> FieldError<S> {
match self {
ApiError::Database => FieldError::new(
"Internal database error",
graphql_value!({
"type": "DATABASE"
}),
),
}
}
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn addItem(&self, name: String, quantity: i32) -> Result<GraphQLResult, ApiError> {
let mut error = ValidationErrorItem {
name: None,
quantity: None,
};
if !(10 <= name.len() && name.len() <= 100) {
error.name = Some("between 10 and 100".into());
}
if !(1 <= quantity && quantity <= 10) {
error.quantity = Some("between 1 and 10".into());
}
if error.name.is_none() && error.quantity.is_none() {
Ok(GraphQLResult::Ok(Item { name, quantity }))
} else {
Ok(GraphQLResult::Err(error))
}
}
}
#
# fn main() {}
```
## Additional Material
The [Shopify API](https://shopify.dev/docs/admin-api/graphql/reference)
implements a similar approach. Their API is a good reference to
explore this approach in a real world application.
# Comparison
The first approach discussed above--where every error is a critical error defined by `FieldResult` --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.
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.

View file

@ -1,78 +0,0 @@
Generics
========
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][1] and [non-`null`ables][2].
This poses a restriction on what we can expose in [GraphQL] from [Rust]: no generic structs can be exposed - all type parameters must be bound. For example, we cannot expose `Result<T, E>` as a [GraphQL type][0], but we _can_ expose `Result<User, String>` as a [GraphQL type][0].
Let's make a slightly more compact but generic implementation of [the last schema error example](error/schema.md#example-non-struct-objects):
```rust
# extern crate juniper;
# use juniper::{graphql_object, GraphQLObject};
#
#[derive(GraphQLObject)]
struct User {
name: String,
}
#[derive(GraphQLObject)]
struct ForumPost {
title: String,
}
#[derive(GraphQLObject)]
struct ValidationError {
field: String,
message: String,
}
struct MutationResult<T>(Result<T, Vec<ValidationError>>);
#[graphql_object]
#[graphql(name = "UserResult")]
impl MutationResult<User> {
fn user(&self) -> Option<&User> {
self.0.as_ref().ok()
}
fn error(&self) -> Option<&[ValidationError]> {
self.0.as_ref().err().map(Vec::as_slice)
}
}
#[graphql_object]
#[graphql(name = "ForumPostResult")]
impl MutationResult<ForumPost> {
fn forum_post(&self) -> Option<&ForumPost> {
self.0.as_ref().ok()
}
fn error(&self) -> Option<&[ValidationError]> {
self.0.as_ref().err().map(Vec::as_slice)
}
}
#
# fn main() {}
```
Here, we've made a wrapper around a `Result` and exposed some concrete instantiations of `Result<T, E>` as distinct [GraphQL objects][3].
> **NOTE**: The reason we needed the wrapper is of [Rust]'s [orphan rules][10] (both the `Result` and [Juniper]'s internal traits are from third-party sources).
> **NOTE**: Because we're using generics, we also need to specify a `name` for our instantiated [GraphQL types][0]. Even if [Juniper] _could_ figure out the name, `MutationResult<User>` wouldn't be a [valid GraphQL type name][4]. And, also, two different [GraphQL types][0] cannot have the same `MutationResult` name, inferred by default.
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[0]: https://spec.graphql.org/October2021#sec-Types
[1]: https://spec.graphql.org/October2021#sec-List
[2]: https://spec.graphql.org/October2021#sec-Non-Null
[3]: https://spec.graphql.org/October2021#sec-Objects
[4]: https://spec.graphql.org/October2021#sec-Names
[10]: https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence

View file

@ -1,228 +0,0 @@
Objects
=======
> [GraphQL objects][0] represent a list of named fields, each of which yield a value of a specific type.
When declaring a [GraphQL schema][schema], most of the time we deal with [GraphQL objects][0], because they are the only place where we actually define the behavior once [schema] gets [executed][1].
There are two ways to define a [GraphQL object][0] in [Juniper]:
1. The easiest way, suitable for trivial cases, is to use the [`#[derive(GraphQLObject)]` attribute][2] on a [struct], as described below.
2. The other way, using the [`#[graphql_object]` attribute][3], is described in the ["Complex fields" chapter](complex_fields.md).
## Trivial
While any type in [Rust] can be exposed as a [GraphQL object][0], the most common one is a [struct]:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
#
# fn main() {}
```
This creates a [GraphQL object][0] type called `Person`, with two fields: `name` of type `String!`, and `age` of type `Int!`. Because of [Rust]'s type system, everything is exported as [non-`null`][4] by default.
> **TIP**: If a `null`able field is required, the most obvious way is to use `Option`. Or [`Nullable`] for distinguishing between [explicit and implicit `null`s][14].
### Documentation
We should take advantage of the fact that [GraphQL] is [self-documenting][5] and add descriptions to the defined [GraphQL object][0] type and its fields. [Juniper] will automatically use associated [Rust doc comments][6] as [GraphQL descriptions][7]:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
/// Information about a person.
#[derive(GraphQLObject)]
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,
}
#
# fn main() {}
```
If using [Rust doc comments][6] is not desired (for example, when we want to keep [Rust] API docs and GraphQL schema descriptions different), the `#[graphql(description = "...")]` attribute can be used instead, which takes precedence over [Rust doc comments][6]:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
/// This doc comment is visible only in Rust API docs.
#[derive(GraphQLObject)]
#[graphql(description = "This description is visible only in GraphQL schema.")]
struct Person {
/// This doc comment is visible only in Rust API docs.
#[graphql(desc = "This description is visible only in GraphQL schema.")]
// ^^^^ shortcut for a `description` argument
name: String,
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
age: i32,
}
#
# fn main() {}
```
### Renaming
By default, [struct] fields are converted from [Rust]'s standard `snake_case` naming convention into [GraphQL]'s `camelCase` convention:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
struct Person {
first_name: String, // exposed as `firstName` in GraphQL schema
last_name: String, // exposed as `lastName` in GraphQL schema
}
#
# fn main() {}
```
We can override the name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
#[graphql(name = "WebPerson")] // now exposed as `WebPerson` in GraphQL schema
struct Person {
name: String,
age: i32,
#[graphql(name = "websiteURL")]
website_url: Option<String>, // now exposed as `websiteURL` in GraphQL schema
}
#
# fn main() {}
```
Or provide a different renaming policy for all the [struct] fields:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
#[graphql(rename_all = "none")] // disables any renaming
struct Person {
name: String,
age: i32,
website_url: Option<String>, // exposed as `website_url` in GraphQL schema
}
#
# fn main() {}
```
> **TIP**: Supported policies are: `SCREAMING_SNAKE_CASE`, `camelCase` and `none` (disables any renaming).
### Deprecation
To [deprecate][9] a [GraphQL object][0] field, either the `#[graphql(deprecated = "...")]` attribute, or [Rust's `#[deprecated]` attribute][13], should be used:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
#[graphql(deprecated = "Please use the `name` field instead.")]
first_name: String,
#[deprecated(note = "Please use the `name` field instead.")]
last_name: String,
}
#
# fn main() {}
```
> **NOTE**: Only [GraphQL object][0]/[interface][11] fields and [GraphQL enum][10] values can be [deprecated][9].
### Ignoring
By default, all [struct] fields are included into the generated [GraphQL object][0] type. To prevent inclusion of a specific field annotate it with the `#[graphql(ignore)]` attribute:
```rust
# #![allow(dead_code)]
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
#[graphql(ignore)]
password_hash: String, // cannot be queried from GraphQL
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
is_banned: bool, // cannot be queried from GraphQL
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[derive(GraphQLObject)]`][2] attribute.
## Relationships
[GraphQL object][0] fields can be of any [GraphQL] type, except [input objects][8].
Let's see what it means to build relationships between [objects][0]:
```rust
# extern crate juniper;
# use juniper::GraphQLObject;
#
#[derive(GraphQLObject)]
struct Person {
name: String,
age: i32,
}
#[derive(GraphQLObject)]
struct House {
address: Option<String>, // converted into `String` (`null`able)
inhabitants: Vec<Person>, // converted into `[Person!]!`
}
#
# fn main() {}
```
Because `Person` is a valid [GraphQL] type, we can have a `Vec<Person>` in a [struct], and it'll be automatically converted into a [list][12] of [non-`null`able][4] `Person` [objects][0].
[`Nullable`]: https://docs.rs/juniper/0.16.1/juniper/enum.Nullable.html
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
[schema]: https://graphql.org/learn/schema
[struct]: https://doc.rust-lang.org/reference/items/structs.html
[0]: https://spec.graphql.org/October2021#sec-Objects
[1]: https://spec.graphql.org/October2021#sec-Execution
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLObject.html
[3]: https://docs.rs/juniper/0.16.1/juniper/attr.graphql_object.html
[4]: https://spec.graphql.org/October2021#sec-Non-Null
[5]: https://spec.graphql.org/October2021#sec-Introspection
[6]: https://doc.rust-lang.org/reference/comments.html#doc-comments
[7]: https://spec.graphql.org/October2021#sec-Descriptions
[8]: https://spec.graphql.org/October2021#sec-Input-Objects
[9]: https://spec.graphql.org/October2021#sec--deprecated
[10]: https://spec.graphql.org/October2021#sec-Enums
[11]: https://spec.graphql.org/October2021#sec-Interfaces
[12]: https://spec.graphql.org/October2021#sec-List
[13]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
[14]: https://spec.graphql.org/October2021#sel-EAFdRDHAAEJDAoBxzT

View file

@ -0,0 +1,150 @@
# Using contexts
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 _context factory_. How this is defined is
specific to the framework integration you're using, so check out the
documentation for [Rocket](../../servers/rocket.md) integration.
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 `HashMap`:
```rust
# #![allow(dead_code)]
# use std::collections::HashMap;
#
struct Database {
users: HashMap<i32, User>,
}
struct User {
id: i32,
name: String,
friend_ids: Vec<i32>,
}
#
# fn main() { }
```
We would like a `friends` field on `User` that returns a list of `User` objects.
In order to write such a field though, the database must be queried.
To solve this, we mark the `Database` as a valid context type and assign it to
the user object.
To gain access to the context, we need to specify an argument with the same
type as the specified `Context` for the type:
```rust
# extern crate juniper;
# use std::collections::HashMap;
# use juniper::graphql_object;
#
// This struct represents our context.
struct Database {
users: HashMap<i32, User>,
}
// Mark the Database as a valid context type for Juniper
impl juniper::Context for Database {}
struct User {
id: i32,
name: String,
friend_ids: Vec<i32>,
}
// Assign Database as the context type for User
#[graphql_object(context = Database)]
impl User {
// 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<'db>(&self, context: &'db Database) -> Vec<&'db User> {
// Use the database to lookup users
self.friend_ids.iter()
.map(|id| context.users.get(id).expect("Could not find user with ID"))
.collect()
}
fn name(&self) -> &str {
self.name.as_str()
}
fn id(&self) -> i32 {
self.id
}
}
#
# fn main() { }
```
You only get an immutable reference to the context, so if you want to affect
change to the execution, you'll need to use [interior
mutability](https://doc.rust-lang.org/book/first-edition/mutability.html#interior-vs-exterior-mutability)
using e.g. `RwLock` or `RefCell`.
## Dealing with mutable references
Context cannot be specified by a mutable reference, because concurrent fields resolving may be performed. If you have something in your context that requires access by mutable reference, then you need to leverage the [interior mutability][1] for that.
For example, when using async runtime with [work stealing][2] (like `tokio`), which obviously requires thread safety in addition, you will need to use a corresponding async version of `RwLock`:
```rust
# extern crate juniper;
# extern crate tokio;
# use std::collections::HashMap;
# use juniper::graphql_object;
use tokio::sync::RwLock;
struct Database {
requested_count: HashMap<i32, i32>,
}
// Since we cannot directly implement juniper::Context
// for RwLock we use the newtype idiom
struct DatabaseContext(RwLock<Database>);
impl juniper::Context for DatabaseContext {}
struct User {
id: i32,
name: String
}
#[graphql_object(context=DatabaseContext)]
impl User {
async fn times_requested<'db>(&self, context: &'db DatabaseContext) -> i32 {
// Acquire a mutable reference and await if async RwLock is used,
// which is necessary if context consists async operations like
// querying remote databases.
// Obtain base type
let DatabaseContext(context) = context;
// If context is immutable use .read() on RwLock.
let mut context = context.write().await;
// Perform a mutable operation.
context.requested_count.entry(self.id).and_modify(|e| { *e += 1 }).or_insert(1).clone()
}
fn name(&self) -> &str {
self.name.as_str()
}
fn id(&self) -> i32 {
self.id
}
}
#
# fn main() { }
```
Replace `tokio::sync::RwLock` with `std::sync::RwLock` (or similar) if you don't intend to use async resolving.
[1]: https://doc.rust-lang.org/book/ch15-05-interior-mutability.html
[2]: https://en.wikipedia.org/wiki/Work_stealing

View file

@ -0,0 +1,11 @@
# Other Types
The GraphQL type system provides several types in addition to objects.
Find out more about each type below:
- [Enums](enums.md)
- [Interfaces](interfaces.md)
- [Input objects](input_objects.md)
- [Scalars](scalars.md)
- [Unions](unions.md)

View file

@ -1,88 +1,118 @@
Scalars
=======
# Scalars
[GraphQL scalars][0] represent primitive leaf values in a GraphQL type system: numbers, strings, and booleans.
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.
Since any value going over the wire is eventually transformed into JSON, you're
also limited in the data types you can use.
There are two ways to define custom scalars.
* For simple scalars that just wrap a primitive type, you can use the newtype pattern with
a custom derive.
* For more advanced use cases with custom validation, you can use
the `graphql_scalar` proc macro.
## Built-in scalars
Juniper has built-in support for:
* `i32` as `Int`
* `f64` as `Float`
* `String` and `&str` as `String`
* `bool` as `Boolean`
* `juniper::ID` as `ID`. This type is defined [in the
spec](https://spec.graphql.org/October2021#sec-ID) as a type that is serialized
as a string but can be parsed from both a string and an integer.
Note that there is no built-in support for `i64`/`u64`, as the GraphQL spec [doesn't define any built-in scalars for `i64`/`u64` by default](https://spec.graphql.org/October2021#sec-Int). You may wish to leverage a [custom GraphQL scalar](#custom-scalars) in your schema to support them.
**Third party types**:
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.
* uuid::Uuid
* chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime}
* chrono_tz::Tz;
* time::{Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset}
* url::Url
* bson::oid::ObjectId
## Built-in
## Custom scalars
[Juniper] provides support for all the [built-in scalars][5].
### `#[graphql(transparent)]` attribute
| [Rust] types | [GraphQL] scalar |
|------------------|------------------|
| `bool` | `Boolean` |
| `i32` | `Int` |
| `f64` | `Float` |
| `String`, `&str` | `String` |
| `juniper::ID` | [`ID`] |
Often, you might need a custom scalar that just wraps an existing type.
> **NOTE**: [`ID`] scalar is [defined in the GraphQL spec][`ID`] as a type that is serialized as a string, but can be parsed from both a string and an integer.
This can be done with the newtype pattern and a custom derive, similar to how
serde supports this pattern with `#[serde(transparent)]`.
> **TIP**: There is no built-in support for `i64`, `u64`, or other [Rust] integer types, as the [GraphQL spec doesn't define any built-in scalars for them][1] by default. Instead, to be supported, they should be defined as [custom scalars](#custom) in a [GraphQL schema][schema].
## Custom
We can create [custom scalars][2] for other primitive values, but they are still [limited in the data types for representation][1], and only introduce additional semantic meaning. This, also, often requires coordination with the client library, intended to consume the API we're building.
[Custom scalars][2] can be defined in [Juniper] by using either [`#[derive(GraphQLScalar)]`][8] or [`#[graphql_scalar]`][9] attributes, which do work pretty much the same way (except, [`#[derive(GraphQLScalar)]`][8] cannot be used on [type aliases][4]).
### Transparent delegation
Quite often, we want to create a [custom GraphQL scalar][2] type by just wrapping an existing one, inheriting all its behavior. In [Rust], this is often called as ["newtype pattern"][3]. This may be achieved by providing a `#[graphql(transparent)]` attribute to the definition:
```rust
# extern crate juniper;
# use juniper::{graphql_scalar, GraphQLScalar};
#
#[derive(GraphQLScalar)]
#[derive(juniper::GraphQLScalar)]
#[graphql(transparent)]
pub struct UserId(i32);
// Using `#[graphql_scalar]` attribute here makes no difference, and is fully
// interchangeable with `#[derive(GraphQLScalar)]`. It's only up to the
// personal preference - which one to use.
#[graphql_scalar]
#[graphql(transparent)]
pub struct MessageId {
value: i32,
#[derive(juniper::GraphQLObject)]
struct User {
id: UserId,
}
#
# fn main() {}
```
That's it, now the `UserId` and `MessageId` [scalars][0] can be used in [GraphQL schema][schema].
We may also customize the definition, to provide more information about our [custom scalar][2] in [GraphQL schema][schema]:
`#[derive(GraphQLScalar)]` is mostly interchangeable with `#[graphql_scalar]`
attribute:
```rust
# extern crate juniper;
# use juniper::GraphQLScalar;
# use juniper::graphql_scalar;
#
/// You can use a Rust doc comment to specify a description in GraphQL schema.
#[derive(GraphQLScalar)]
#[graphql(
transparent,
// Overwrite the name of this type in the GraphQL schema.
name = "MyUserId",
// Specifying a type description via attribute takes precedence over the
// Rust doc comment, which allows to separate Rust API docs from GraphQL
// schema descriptions, if required.
description = "Actual description.",
// Optional specification URL.
specified_by_url = "https://tools.ietf.org/html/rfc4122",
)]
pub struct UserId(String);
#[graphql_scalar(transparent)]
pub struct UserId {
value: i32,
}
#[derive(juniper::GraphQLObject)]
struct User {
id: UserId,
}
#
# fn main() {}
```
That's it, you can now use `UserId` in your schema.
### Resolving
The macro also allows for more customization:
```rust
# extern crate juniper;
/// You can use a doc comment to specify a description.
#[derive(juniper::GraphQLScalar)]
#[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);
#
# fn main() {}
```
All the methods used from newtype's field can be replaced with attributes:
### `#[graphql(to_output_with = <fn>)]` attribute
In case we need to customize [resolving][7] of a [custom GraphQL scalar][2] value (change the way it gets executed), the `#[graphql(to_output_with = <fn path>)]` attribute is the way to do so:
```rust
# extern crate juniper;
# use juniper::{GraphQLScalar, ScalarValue, Value};
@ -93,17 +123,14 @@ struct Incremented(i32);
/// Increments [`Incremented`] before converting into a [`Value`].
fn to_output<S: ScalarValue>(v: &Incremented) -> Value<S> {
let inc = v.0 + 1;
Value::from(inc)
Value::from(v.0 + 1)
}
#
#
# fn main() {}
```
### `#[graphql(from_input_with = <fn>)]` attribute
### Input value parsing
Customization of a [custom GraphQL scalar][2] value parsing is possible via `#[graphql(from_input_with = <fn path>)]` attribute:
```rust
# extern crate juniper;
# use juniper::{GraphQLScalar, InputValue, ScalarValue};
@ -113,8 +140,8 @@ Customization of a [custom GraphQL scalar][2] value parsing is possible via `#[g
struct UserId(String);
impl UserId {
/// Checks whether the [`InputValue`] is a [`String`] beginning with `id: `
/// and strips it.
/// Checks whether [`InputValue`] is `String` beginning with `id: ` and
/// strips it.
fn from_input<S>(input: &InputValue<S>) -> Result<Self, String>
where
S: ScalarValue
@ -137,15 +164,13 @@ impl UserId {
# fn main() {}
```
### `#[graphql(parse_token_with = <fn>]` or `#[graphql(parse_token(<types>)]` attributes
### Token parsing
Customization of which tokens a [custom GraphQL scalar][0] type should be parsed from, is possible via `#[graphql(parse_token_with = <fn path>)]` or `#[graphql(parse_token(<types>)]` attributes:
```rust
# extern crate juniper;
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value,
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value
# };
#
#[derive(GraphQLScalar)]
@ -153,26 +178,32 @@ Customization of which tokens a [custom GraphQL scalar][0] type should be parsed
to_output_with = to_output,
from_input_with = from_input,
parse_token_with = parse_token,
// ^^^^^^^^^^^^^^^^ Can be replaced with `parse_token(String, i32)`
// which tries to parse as `String` and then as `i32`
// if prior fails.
)]
// ^^^^^^^^^^^^^^^^ Can be replaced with `parse_token(String, i32)`, which
// tries to parse as `String` first, and then as `i32` if
// prior fails.
enum StringOrInt {
String(String),
Int(i32),
}
fn to_output<S: ScalarValue>(v: &StringOrInt) -> Value<S> {
fn to_output<S>(v: &StringOrInt) -> Value<S>
where
S: ScalarValue
{
match v {
StringOrInt::String(s) => Value::scalar(s.to_owned()),
StringOrInt::Int(i) => Value::scalar(*i),
}
}
fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<StringOrInt, String> {
fn from_input<S>(v: &InputValue<S>) -> Result<StringOrInt, String>
where
S: ScalarValue
{
v.as_string_value()
.map(|s| StringOrInt::String(s.into()))
.or_else(|| v.as_int_value().map(StringOrInt::Int))
.or_else(|| v.as_int_value().map(|i| StringOrInt::Int(i)))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {v}"))
}
@ -183,62 +214,27 @@ fn parse_token<S: ScalarValue>(value: ScalarToken<'_>) -> ParseScalarResult<S> {
#
# fn main() {}
```
> **NOTE**: Once we provide all 3 custom functions, there is no sense to follow [newtype pattern][3] anymore, as nothing left to inherit.
> __NOTE:__ As you can see, once you provide all 3 custom resolvers, there
> is no need to follow `newtype` pattern.
### Full behavior
### `#[graphql(with = <path>)]` attribute
Instead of providing all custom resolvers, you can provide path to the `to_output`,
`from_input`, `parse_token` functions.
Path can be simply `with = Self` (default path where macro expects resolvers to be),
in case there is an impl block with custom resolvers:
Instead of providing all custom functions separately, it's possible to provide a module holding the appropriate `to_output()`, `from_input()` and `parse_token()` functions via `#[graphql(with = <module path>)]` attribute:
```rust
# extern crate juniper;
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value,
# ScalarValue, ScalarToken, Value
# };
#
#[derive(GraphQLScalar)]
#[graphql(with = string_or_int)]
enum StringOrInt {
String(String),
Int(i32),
}
mod string_or_int {
use super::*;
pub(super) fn to_output<S: ScalarValue>(v: &StringOrInt) -> Value<S> {
match v {
StringOrInt::String(s) => Value::scalar(s.to_owned()),
StringOrInt::Int(i) => Value::scalar(*i),
}
}
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<StringOrInt, String> {
v.as_string_value()
.map(|s| StringOrInt::String(s.into()))
.or_else(|| v.as_int_value().map(StringOrInt::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {v}"))
}
pub(super) fn parse_token<S: ScalarValue>(t: ScalarToken<'_>) -> ParseScalarResult<S> {
<String as ParseScalarValue<S>>::from_str(t)
.or_else(|_| <i32 as ParseScalarValue<S>>::from_str(t))
}
}
#
# fn main() {}
```
A regular `impl` block is also suitable for that:
```rust
# extern crate juniper;
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value,
# };
#
#[derive(GraphQLScalar)]
// #[graphql(with = Self)] <- default behaviour, so can be omitted
// #[graphql(with = Self)] <- default behaviour
enum StringOrInt {
String(String),
Int(i32),
@ -251,20 +247,20 @@ impl StringOrInt {
Self::Int(i) => Value::scalar(*i),
}
}
fn from_input<S>(v: &InputValue<S>) -> Result<Self, String>
where
S: ScalarValue
S: ScalarValue,
{
v.as_string_value()
.map(|s| Self::String(s.into()))
.or_else(|| v.as_int_value().map(Self::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {v}"))
}
fn parse_token<S>(value: ScalarToken<'_>) -> ParseScalarResult<S>
where
S: ScalarValue
S: ScalarValue,
{
<String as ParseScalarValue<S>>::from_str(value)
.or_else(|_| <i32 as ParseScalarValue<S>>::from_str(value))
@ -274,19 +270,17 @@ impl StringOrInt {
# fn main() {}
```
At the same time, any custom function still may be specified separately, if required:
Or it can be path to a module, where custom resolvers are located.
```rust
# extern crate juniper;
# use juniper::{
# GraphQLScalar, InputValue, ParseScalarResult, ScalarValue,
# ScalarToken, Value
# GraphQLScalar, InputValue, ParseScalarResult, ParseScalarValue,
# ScalarValue, ScalarToken, Value
# };
#
#[derive(GraphQLScalar)]
#[graphql(
with = string_or_int,
parse_token(String, i32)
)]
#[graphql(with = string_or_int)]
enum StringOrInt {
String(String),
Int(i32),
@ -304,7 +298,7 @@ mod string_or_int {
StringOrInt::Int(i) => Value::scalar(*i),
}
}
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<StringOrInt, String>
where
S: ScalarValue,
@ -314,23 +308,62 @@ mod string_or_int {
.or_else(|| v.as_int_value().map(StringOrInt::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {v}"))
}
// No need in `parse_token()` function.
pub(super) fn parse_token<S>(value: ScalarToken<'_>) -> ParseScalarResult<S>
where
S: ScalarValue,
{
<String as ParseScalarValue<S>>::from_str(value)
.or_else(|_| <i32 as ParseScalarValue<S>>::from_str(value))
}
}
#
# fn main() {}
```
> **TIP**: See more available features in the API docs of the [`#[derive(GraphQLScalar)]`][8] and [`#[graphql_scalar]`][9] attributes.
Also, you can partially override `#[graphql(with)]` attribute with other custom scalars.
```rust
# extern crate juniper;
# use juniper::{GraphQLScalar, InputValue, ParseScalarResult, ScalarValue, ScalarToken, Value};
#
#[derive(GraphQLScalar)]
#[graphql(parse_token(String, i32))]
enum StringOrInt {
String(String),
Int(i32),
}
impl StringOrInt {
fn to_output<S>(&self) -> Value<S>
where
S: ScalarValue,
{
match self {
Self::String(s) => Value::scalar(s.to_owned()),
Self::Int(i) => Value::scalar(*i),
}
}
fn from_input<S>(v: &InputValue<S>) -> Result<Self, String>
where
S: ScalarValue,
{
v.as_string_value()
.map(|s| Self::String(s.into()))
.or_else(|| v.as_int_value().map(Self::Int))
.ok_or_else(|| format!("Expected `String` or `Int`, found: {v}"))
}
}
#
# fn main() {}
```
### Using foreign types as scalars
## Foreign
For implementing custom scalars on foreign types there is `#[graphql_scalar]` attribute macro.
For implementing [custom scalars][2] on foreign types there is [`#[graphql_scalar]`][9] attribute.
> **NOTE**: To satisfy [orphan rules], we should provide a local [`ScalarValue`] implementation.
> __NOTE:__ To satisfy [orphan rules] you should provide local [`ScalarValue`] implementation.
```rust
# extern crate juniper;
@ -358,10 +391,10 @@ use juniper::{graphql_scalar, InputValue, ScalarValue, Value};
with = date_scalar,
parse_token(String),
scalar = CustomScalarValue,
// ^^^^^^^^^^^^^^^^^ Local `ScalarValue` implementation.
)]
// ^^^^^^^^^^^^^^^^^ local `ScalarValue` implementation
type Date = date::Date;
// ^^^^^^^^^^ type from another crate
// ^^^^^^^^^^ Type from another crate.
mod date_scalar {
use super::*;
@ -380,103 +413,5 @@ mod date_scalar {
# fn main() {}
```
### Supported out-of-the-box
[Juniper] provides out-of-the-box [GraphQL scalar][0] implementations for some very common [Rust] crates. The types from these crates will be usable in your schemas automatically after enabling the correspondent self-titled [Cargo feature].
| [Rust] type | [GraphQL] scalar | [Cargo feature] |
|-----------------------------|-----------------------|------------------|
| [`bigdecimal::BigDecimal`] | `BigDecimal` | [`bigdecimal`] |
| [`bson::oid::ObjectId`] | [`ObjectID`] | [`bson`] |
| [`bson::DateTime`] | [`DateTime`] | [`bson`] |
| [`chrono::NaiveDate`] | [`LocalDate`] | [`chrono`] |
| [`chrono::NaiveTime`] | [`LocalTime`] | [`chrono`] |
| [`chrono::NaiveDateTime`] | [`LocalDateTime`] | [`chrono`] |
| [`chrono::DateTime`] | [`DateTime`] | [`chrono`] |
| [`chrono_tz::Tz`] | [`TimeZone`] | [`chrono-tz`] |
| [`rust_decimal::Decimal`] | `Decimal` | [`rust_decimal`] |
| [`jiff::civil::Date`] | [`LocalDate`] | [`jiff`] |
| [`jiff::civil::Time`] | [`LocalTime`] | [`jiff`] |
| [`jiff::civil::DateTime`] | [`LocalDateTime`] | [`jiff`] |
| [`jiff::Timestamp`] | [`DateTime`] | [`jiff`] |
| [`jiff::Zoned`] | `ZonedDateTime` | [`jiff`] |
| [`jiff::tz::TimeZone`] | `TimeZoneOrUtcOffset` | [`jiff`] |
| [`jiff::tz::TimeZone`] via [`juniper::integrations::jiff::TimeZone`] | [`TimeZone`] | [`jiff`] |
| [`jiff::tz::Offset`] | [`UtcOffset`] | [`jiff`] |
| [`jiff::Span`] | [`Duration`] | [`jiff`] |
| [`time::Date`] | [`LocalDate`] | [`time`] |
| [`time::Time`] | [`LocalTime`] | [`time`] |
| [`time::PrimitiveDateTime`] | [`LocalDateTime`] | [`time`] |
| [`time::OffsetDateTime`] | [`DateTime`] | [`time`] |
| [`time::UtcOffset`] | [`UtcOffset`] | [`time`] |
| [`url::Url`] | [`URL`] | [`url`] |
| [`uuid::Uuid`] | [`UUID`] | [`uuid`] |
[`bigdecimal`]: https://docs.rs/bigdecimal
[`bigdecimal::BigDecimal`]: https://docs.rs/bigdecimal/latest/bigdecimal/struct.BigDecimal.html
[`bson`]: https://docs.rs/bson
[`bson::DateTime`]: https://docs.rs/bson/latest/bson/struct.DateTime.html
[`bson::oid::ObjectId`]: https://docs.rs/bson/latest/bson/oid/struct.ObjectId.html
[`chrono`]: https://docs.rs/chrono
[`chrono::DateTime`]: https://docs.rs/chrono/latest/chrono/struct.DateTime.html
[`chrono::NaiveDate`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
[`chrono::NaiveDateTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
[`chrono::NaiveTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
[`chrono-tz`]: https://docs.rs/chrono-tz
[`chrono_tz::Tz`]: https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html
[`DateTime`]: https://graphql-scalars.dev/docs/scalars/date-time
[`Duration`]: https://graphql-scalars.dev/docs/scalars/duration
[`ID`]: https://spec.graphql.org/October2021#sec-ID
[`jiff`]: https://docs.rs/jiff
[`jiff::civil::Date`]: https://docs.rs/jiff/latest/jiff/civil/struct.Date.html
[`jiff::civil::DateTime`]: https://docs.rs/jiff/latest/jiff/civil/struct.DateTime.html
[`jiff::civil::Time`]: https://docs.rs/jiff/latest/jiff/civil/struct.Time.html
[`jiff::Span`]: https://docs.rs/jiff/latest/jiff/struct.Span.html
[`jiff::Timestamp`]: https://docs.rs/jiff/latest/jiff/struct.Timestamp.html
[`jiff::tz::Offset`]: https://docs.rs/jiff/latest/jiff/tz/struct.Offset.html
[`jiff::tz::TimeZone`]: https://docs.rs/jiff/latest/jiff/tz/struct.TimeZone.html
[`jiff::Zoned`]: https://docs.rs/jiff/latest/jiff/struct.Zoned.html
[`juniper::integrations::jiff::TimeZone`]: https://docs.rs/juniper/0.16.1/juniper/integrations/jiff/struct.TimeZone.html
[`LocalDate`]: https://graphql-scalars.dev/docs/scalars/local-date
[`LocalDateTime`]: https://graphql-scalars.dev/docs/scalars/local-date-time
[`LocalTime`]: https://graphql-scalars.dev/docs/scalars/local-time
[`ObjectID`]: https://the-guild.dev/graphql/scalars/docs/scalars/object-id
[`rust_decimal`]: https://docs.rs/rust_decimal
[`rust_decimal::Decimal`]: https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html
[`ScalarValue`]: https://docs.rs/juniper/0.16.1/juniper/trait.ScalarValue.html
[`serde`]: https://docs.rs/serde
[`time`]: https://docs.rs/time
[`time::Date`]: https://docs.rs/time/latest/time/struct.Date.html
[`time::PrimitiveDateTime`]: https://docs.rs/time/latest/time/struct.PrimitiveDateTime.html
[`time::Time`]: https://docs.rs/time/latest/time/struct.Time.html
[`time::UtcOffset`]: https://docs.rs/time/latest/time/struct.UtcOffset.html
[`time::OffsetDateTime`]: https://docs.rs/time/latest/time/struct.OffsetDateTime.html
[`TimeZone`]: https://graphql-scalars.dev/docs/scalars/time-zone
[`url`]: https://docs.rs/url
[`url::Url`]: https://docs.rs/url/latest/url/struct.Url.html
[`URL`]: https://graphql-scalars.dev/docs/scalars/url
[`UtcOffset`]: https://graphql-scalars.dev/docs/scalars/utc-offset
[`uuid`]: https://docs.rs/uuid
[`uuid::Uuid`]: https://docs.rs/uuid/latest/uuid/struct.Uuid.html
[`UUID`]: https://graphql-scalars.dev/docs/scalars/uuid
[Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[orphan rules]: https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
[Rust]: https://www.rust-lang.org
[schema]: https://graphql.org/learn/schema
[0]: https://spec.graphql.org/October2021#sec-Scalars
[1]: https://spec.graphql.org/October2021#sel-FAHXJDCAACKB1qb
[2]: https://spec.graphql.org/October2021#sec-Scalars.Custom-Scalars
[3]: https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html
[4]: https://doc.rust-lang.org/reference/items/type-aliases.html
[5]: https://spec.graphql.org/October2021/#sec-Scalars.Built-in-Scalars
[6]: https://serde.rs/container-attrs.html#transparent
[7]: https://spec.graphql.org/October2021#sec-Value-Resolution
[8]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLScalar.html
[9]: https://docs.rs/juniper/0.16.1/juniper/attr.graphql_scalar.html
[`ScalarValue`]: https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html

View file

@ -1,17 +1,25 @@
Unions
======
> [GraphQL unions][0] represent an object that could be one of a list of [GraphQL object][10] types, but provides for no guaranteed fields between those types. They also differ from [interfaces][12] in that [object][10] types declare what [interfaces][12] they implement, but are not aware of what [unions][0] contain them.
From the server's point of view, [GraphQL unions][1] are somewhat similar to [interfaces][5] - the main difference is that they don't contain fields on their own.
From the server's point of view, [GraphQL unions][0] are somewhat similar to [interfaces][12]: the main difference is that they don't contain fields on their own, and so, we only need to represent a value, _dispatchable_ into concrete [objects][10].
The most obvious and straightforward way to represent a [GraphQL union][1] in Rust is enum. However, we also can do so either with trait or a regular struct. That's why, for implementing [GraphQL unions][1] Juniper provides:
- `#[derive(GraphQLUnion)]` macro for enums and structs.
- `#[graphql_union]` for traits.
## Enums
Most of the time, we just need a trivial and straightforward Rust enum to represent a [GraphQL union][1].
Obviously, the most straightforward approach to express [GraphQL unions][0] in [Rust] is to use [enums][22]. In [Juniper] this may be done by using [`#[derive(GraphQLInterface)]`][2] attribute on them:
```rust
# extern crate derive_more;
# extern crate juniper;
# use derive_more::From;
# use juniper::{GraphQLObject, GraphQLUnion};
#
# extern crate derive_more;
use derive_more::From;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
struct Human {
id: String,
@ -25,7 +33,6 @@ struct Droid {
}
#[derive(From, GraphQLUnion)]
// ^^^^ only for convenience, and may be omitted
enum Character {
Human(Human),
Droid(Droid),
@ -35,93 +42,22 @@ enum Character {
```
### Renaming
### Ignoring enum variants
Just as with [renaming GraphQL objects](objects/index.md#renaming), we can override the default [union][0] name by using the `#[graphql(name = "...")]` attribute:
```rust
# extern crate juniper;
# use juniper::{GraphQLObject, GraphQLUnion};
#
# #[derive(GraphQLObject)]
# struct Human {
# id: String,
# home_planet: String,
# }
#
# #[derive(GraphQLObject)]
# struct Droid {
# id: String,
# primary_function: String,
# }
#
#[derive(GraphQLUnion)]
#[graphql(name = "CharacterUnion")]
enum Character { // exposed as `CharacterUnion` in GraphQL schema
Human(Human),
Droid(Droid),
}
#
# fn main() {}
```
> **NOTE**: Unlike [Rust enum variants][22], [GraphQL union members][0] don't have any special names aside from the ones provided by [objects][10] themselves, and so, obviously, **cannot be renamed**.
### Documentation
Similarly to [documenting GraphQL objects](objects/index.md#documentation), we can [document][7] a [GraphQL union][0] via `#[graphql(description = "...")]` attribute or [Rust doc comments][6]:
```rust
# extern crate juniper;
# use juniper::{GraphQLObject, GraphQLUnion};
#
# #[derive(GraphQLObject)]
# struct Human {
# id: String,
# home_planet: String,
# }
#
# #[derive(GraphQLObject)]
# struct Droid {
# id: String,
# primary_function: String,
# }
#
/// This doc comment is visible in both Rust API docs and GraphQL schema
/// descriptions.
#[derive(GraphQLUnion)]
enum Character {
/// This doc comment is visible only in Rust API docs.
Human(Human),
/// This doc comment is visible only in Rust API docs.
Droid(Droid),
}
/// This doc comment is visible only in Rust API docs.
#[derive(GraphQLUnion)]
#[graphql(description = "This description overwrites the one from doc comment.")]
// ^^^^^^^^^^^ or `desc` shortcut, up to your preference
enum Person {
/// This doc comment is visible only in Rust API docs.
Human(Human),
}
#
# fn main() {}
```
> **NOTE**: Unlike [Rust enum variants][22], [GraphQL union members][0] don't have any special constructors aside from the provided [objects][10] directly, and so, **cannot be [documented][7]**, but rather reuse [object descriptions][7] "as is".
### Ignoring
In some rare situations we may want to omit exposing an [enum][22] variant in a [GraphQL schema][1]. [Similarly to GraphQL enums](enums.md#ignoring), we can just annotate the variant with the `#[graphql(ignore)]` attribute.
In some rare situations we may want to omit exposing an enum variant in the GraphQL schema.
As an example, let's consider the situation where we need to bind some type parameter `T` for doing interesting type-level stuff in our resolvers. To achieve this we need to have `PhantomData<T>`, but we don't want it exposed in the GraphQL schema.
> __WARNING__:
> It's the _library user's responsibility_ to ensure that ignored enum variant is _never_ returned from resolvers, otherwise resolving the GraphQL query will __panic at runtime__.
```rust
# extern crate derive_more;
# extern crate juniper;
# extern crate derive_more;
# use std::marker::PhantomData;
# use derive_more::From;
# use juniper::{GraphQLObject, GraphQLUnion};
#
use derive_more::From;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
struct Human {
id: String,
@ -139,30 +75,417 @@ enum Character<S> {
Human(Human),
Droid(Droid),
#[from(ignore)]
#[graphql(ignore)]
// ^^^^^^ or `skip`, up to your preference
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
_State(PhantomData<S>),
}
#
# fn main() {}
```
> **WARNING**: It's the _library user's responsibility_ to ensure that ignored [enum][22] variant is **never** returned from resolvers, otherwise resolving the [GraphQL] query will **panic in runtime**.
> **TIP**: See more available features in the API docs of the [`#[derive(GraphQLUnion)]`][2] attribute.
### External resolver functions
If some custom logic is needed to resolve a [GraphQL union][1] variant, you may specify an external function to do so:
```rust
# #![allow(dead_code)]
# extern crate juniper;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
#[graphql(Context = CustomContext)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = CustomContext)]
struct Droid {
id: String,
primary_function: String,
}
pub struct CustomContext {
droid: Droid,
}
impl juniper::Context for CustomContext {}
#[derive(GraphQLUnion)]
#[graphql(Context = CustomContext)]
enum Character {
Human(Human),
#[graphql(with = Character::droid_from_context)]
Droid(Droid),
}
impl Character {
// NOTICE: The function signature must contain `&self` and `&Context`,
// and return `Option<&VariantType>`.
fn droid_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Droid> {
Some(&ctx.droid)
}
}
#
# fn main() {}
```
With an external resolver function we can even declare a new [GraphQL union][1] variant where the Rust type is absent in the initial enum definition. The attribute syntax `#[graphql(on VariantType = resolver_fn)]` follows the [GraphQL syntax for dispatching union variants](https://spec.graphql.org/October2021#example-f8163).
```rust
# #![allow(dead_code)]
# extern crate juniper;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
#[graphql(Context = CustomContext)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = CustomContext)]
struct Droid {
id: String,
primary_function: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = CustomContext)]
struct Ewok {
id: String,
is_funny: bool,
}
pub struct CustomContext {
ewok: Ewok,
}
impl juniper::Context for CustomContext {}
#[derive(GraphQLUnion)]
#[graphql(Context = CustomContext)]
#[graphql(on Ewok = Character::ewok_from_context)]
enum Character {
Human(Human),
Droid(Droid),
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
Ewok,
}
impl Character {
fn ewok_from_context<'c>(&self, ctx: &'c CustomContext) -> Option<&'c Ewok> {
if let Self::Ewok = self {
Some(&ctx.ewok)
} else {
None
}
}
}
#
# fn main() {}
```
[GraphQL]: https://graphql.org
[Juniper]: https://docs.rs/juniper
[Rust]: https://www.rust-lang.org
## Structs
[0]: https://spec.graphql.org/October2021#sec-Unions
[1]: https://graphql.org/learn/schema
[2]: https://docs.rs/juniper/0.16.1/juniper/derive.GraphQLUnion.html
[6]: https://doc.rust-lang.org/reference/comments.html#doc-comments
[7]: https://spec.graphql.org/October2021#sec-Descriptions
[10]: https://spec.graphql.org/October2021#sec-Objects
[11]: https://spec.graphql.org/October2021#sec-Enums
[12]: https://spec.graphql.org/October2021#sec-Interfaces
[22]: https://doc.rust-lang.org/reference/items/enumerations.html#enumerations
Using Rust structs as [GraphQL unions][1] is very similar to using enums, with the nuance that specifying an external resolver function is the only way to declare a [GraphQL union][1] variant.
```rust
# extern crate juniper;
# use std::collections::HashMap;
use juniper::{GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Droid {
id: String,
primary_function: String,
}
struct Database {
humans: HashMap<String, Human>,
droids: HashMap<String, Droid>,
}
impl juniper::Context for Database {}
#[derive(GraphQLUnion)]
#[graphql(
Context = Database,
on Human = Character::get_human,
on Droid = Character::get_droid,
)]
struct Character {
id: String,
}
impl Character {
fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human>{
ctx.humans.get(&self.id)
}
fn get_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid>{
ctx.droids.get(&self.id)
}
}
#
# fn main() {}
```
## Traits
To use a Rust trait definition as a [GraphQL union][1] you need to use the `#[graphql_union]` macro. [Rust doesn't allow derive macros on traits](https://doc.rust-lang.org/stable/reference/procedural-macros.html#derive-macros), so using `#[derive(GraphQLUnion)]` on traits doesn't work.
> __NOTICE__:
> A __trait has to be [object safe](https://doc.rust-lang.org/stable/reference/items/traits.html#object-safety)__, because schema resolvers will need to return a [trait object](https://doc.rust-lang.org/stable/reference/types/trait-object.html) to specify a [GraphQL union][1] behind it.
```rust
# extern crate juniper;
use juniper::{graphql_union, GraphQLObject};
#[derive(GraphQLObject)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
}
#[graphql_union]
trait Character {
// NOTICE: The method signature must contain `&self` and return `Option<&VariantType>`.
fn as_human(&self) -> Option<&Human> { None }
fn as_droid(&self) -> Option<&Droid> { None }
}
impl Character for Human {
fn as_human(&self) -> Option<&Human> { Some(&self) }
}
impl Character for Droid {
fn as_droid(&self) -> Option<&Droid> { Some(&self) }
}
#
# fn main() {}
```
### Custom context
If a [`Context`][6] is required in a trait method to resolve a [GraphQL union][1] variant, specify it as an argument.
```rust
# #![allow(unused_variables)]
# extern crate juniper;
# use std::collections::HashMap;
use juniper::{graphql_union, GraphQLObject};
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Droid {
id: String,
primary_function: String,
}
struct Database {
humans: HashMap<String, Human>,
droids: HashMap<String, Droid>,
}
impl juniper::Context for Database {}
#[graphql_union(context = Database)]
trait Character {
// NOTICE: The method signature may optionally contain `&Context`.
fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> { None }
fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> { None }
}
impl Character for Human {
fn as_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> {
ctx.humans.get(&self.id)
}
}
impl Character for Droid {
fn as_droid<'db>(&self, ctx: &'db Database) -> Option<&'db Droid> {
ctx.droids.get(&self.id)
}
}
#
# fn main() {}
```
### Ignoring trait methods
As with enums, we may want to omit some trait methods to be assumed as [GraphQL union][1] variants and ignore them.
```rust
# extern crate juniper;
use juniper::{graphql_union, GraphQLObject};
#[derive(GraphQLObject)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
}
#[graphql_union]
trait Character {
fn as_human(&self) -> Option<&Human> { None }
fn as_droid(&self) -> Option<&Droid> { None }
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
fn id(&self) -> &str;
}
impl Character for Human {
fn as_human(&self) -> Option<&Human> { Some(&self) }
fn id(&self) -> &str { self.id.as_str() }
}
impl Character for Droid {
fn as_droid(&self) -> Option<&Droid> { Some(&self) }
fn id(&self) -> &str { self.id.as_str() }
}
#
# fn main() {}
```
### External resolver functions
Similarly to enums and structs, it's not mandatory to use trait methods as [GraphQL union][1] variant resolvers. Instead, custom functions may be specified:
```rust
# extern crate juniper;
# use std::collections::HashMap;
use juniper::{graphql_union, GraphQLObject};
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
#[graphql(Context = Database)]
struct Droid {
id: String,
primary_function: String,
}
struct Database {
humans: HashMap<String, Human>,
droids: HashMap<String, Droid>,
}
impl juniper::Context for Database {}
#[graphql_union(context = Database)]
#[graphql_union(
on Human = DynCharacter::get_human,
on Droid = get_droid,
)]
trait Character {
#[graphql(ignore)] // or `#[graphql(skip)]`, your choice
fn id(&self) -> &str;
}
impl Character for Human {
fn id(&self) -> &str { self.id.as_str() }
}
impl Character for Droid {
fn id(&self) -> &str { self.id.as_str() }
}
// The trait object is always `Send` and `Sync`.
type DynCharacter<'a> = dyn Character + Send + Sync + 'a;
impl<'a> DynCharacter<'a> {
fn get_human<'db>(&self, ctx: &'db Database) -> Option<&'db Human> {
ctx.humans.get(self.id())
}
}
// External resolver function doesn't have to be a method of a type.
// It's only a matter of the function signature to match the requirements.
fn get_droid<'db>(ch: &DynCharacter<'_>, ctx: &'db Database) -> Option<&'db Droid> {
ctx.droids.get(ch.id())
}
#
# fn main() {}
```
## `ScalarValue` considerations
By default, `#[derive(GraphQLUnion)]` and `#[graphql_union]` macros generate code, which is generic over a [`ScalarValue`][2] type. This may introduce a problem when at least one of [GraphQL union][1] variants is restricted to a concrete [`ScalarValue`][2] type in its implementation. To resolve such problem, a concrete [`ScalarValue`][2] type should be specified:
```rust
# #![allow(dead_code)]
# extern crate juniper;
use juniper::{DefaultScalarValue, GraphQLObject, GraphQLUnion};
#[derive(GraphQLObject)]
#[graphql(Scalar = DefaultScalarValue)]
struct Human {
id: String,
home_planet: String,
}
#[derive(GraphQLObject)]
struct Droid {
id: String,
primary_function: String,
}
#[derive(GraphQLUnion)]
#[graphql(Scalar = DefaultScalarValue)] // removing this line will fail compilation
enum Character {
Human(Human),
Droid(Droid),
}
#
# fn main() {}
```
[1]: https://spec.graphql.org/October2021#sec-Unions
[2]: https://docs.rs/juniper/latest/juniper/trait.ScalarValue.html
[5]: https://spec.graphql.org/October2021#sec-Interfaces
[6]: https://docs.rs/juniper/0.14.2/juniper/trait.Context.html

View file

@ -8,78 +8,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
## master
[Diff](/../../compare/juniper-v0.16.1...master) | [Milestone](/../../milestone/7)
### BC Breaks
- Upgraded [`chrono-tz` crate] integration to [0.10 version](https://github.com/chronotope/chrono-tz/releases/tag/v0.10.0). ([#1252], [#1284])
- Bumped up [MSRV] to 1.75. ([#1272])
- Corrected compliance with newer [graphql-scalars.dev] specs: ([#1275], [#1277])
- Switched `LocalDateTime` scalars to `yyyy-MM-ddTHH:mm:ss` format in types:
- `chrono::NaiveDateTime`.
- `time::PrimitiveDateTime`.
- Switched from `Date` scalar to `LocalDate` scalar in types:
- `chrono::NaiveDate`.
- `time::Date`.
- Switched from `UtcDateTime` scalar to `DateTime` scalar in types:
- `bson::DateTime`.
- Corrected `TimeZone` scalar in types:
- `chrono_tz::Tz`.
- Renamed `Url` scalar to `URL` in types:
- `url::Url`.
- Renamed `Uuid` scalar to `UUID` in types:
- `uuid::Uuid`.
- Renamed `ObjectId` scalar to `ObjectID` in types: ([#1277])
- `bson::oid::ObjectId`.
### Added
- [`jiff` crate] integration behind `jiff` [Cargo feature]: ([#1271], [#1278], [#1270])
- `jiff::civil::Date` as `LocalDate` scalar.
- `jiff::civil::Time` as `LocalTime` scalar.
- `jiff::civil::DateTime` as `LocalDateTime` scalar. ([#1275])
- `jiff::Timestamp` as `DateTime` scalar.
- `jiff::Zoned` as `ZonedDateTime` scalar.
- `jiff::tz::TimeZone` as `TimeZoneOrUtcOffset` and `TimeZone` scalars.
- `jiff::tz::Offset` as `UtcOffset` scalar.
- `jiff::Span` as `Duration` scalar.
### Changed
- Updated [GraphiQL] to [3.8.3 version](https://github.com/graphql/graphiql/blob/graphiql%403.8.3/packages/graphiql/CHANGELOG.md#383). ([#1300])
[#1252]: /../../pull/1252
[#1270]: /../../issues/1270
[#1271]: /../../pull/1271
[#1272]: /../../pull/1272
[#1275]: /../../pull/1275
[#1277]: /../../pull/1277
[#1278]: /../../pull/1278
[#1281]: /../../pull/1281
[#1284]: /../../pull/1284
[#1300]: /../../pull/1300
## [0.16.1] · 2024-04-04
[0.16.1]: /../../tree/juniper-v0.16.1/juniper
[Diff](/../../compare/juniper-v0.16.0...juniper-v0.16.1) | [Milestone](/../../milestone/6)
### Changed
- Updated [GraphiQL] to 3.1.2 version. ([#1251])
[#1251]: /../../pull/1251
## [0.16.0] · 2024-03-20
[0.16.0]: /../../tree/juniper-v0.16.0/juniper
[Diff](/../../compare/juniper-v0.15.12...juniper-v0.16.0) | [Milestone](/../../milestone/4)
[Diff](/../../compare/juniper-v0.15.9...master)
### BC Breaks
@ -122,24 +51,10 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- Disabled `chrono` [Cargo feature] by default.
- Removed `scalar-naivetime` [Cargo feature].
- Removed lifetime parameter from `ParseError`, `GraphlQLError`, `GraphQLBatchRequest` and `GraphQLRequest`. ([#1081], [#528])
- Upgraded [GraphiQL] to 3.1.1 version (requires new [`graphql-transport-ws` GraphQL over WebSocket Protocol] integration on server, see `juniper_warp/examples/subscription.rs`). ([#1188], [#1193], [#1246])
- Upgraded [GraphiQL] to 3.0.10 version (requires new [`graphql-transport-ws` GraphQL over WebSocket Protocol] integration on server, see `juniper_warp/examples/subscription.rs`). ([#1188], [#1193], [#1221])
- Made `LookAheadMethods::children()` method to return slice instead of `Vec`. ([#1200])
- Abstracted `Spanning::start` and `Spanning::end` fields into separate struct `Span`. ([#1207], [#1208])
- Removed `graphql-parser-integration` and `graphql-parser` [Cargo feature]s by merging them into `schema-language` [Cargo feature]. ([#1237])
- Renamed `RootNode::as_schema_language()` method as `RootNode::as_sdl()`. ([#1237])
- Renamed `RootNode::as_parser_document()` method as `RootNode::as_document()`. ([#1237])
- Reworked look-ahead machinery: ([#1212])
- Turned from eagerly-evaluated into lazy-evaluated:
- Made `LookAheadValue::List` to contain new iterable `LookAheadList` type.
- Made `LookAheadValue::Object` to contain new iterable `LookAheadObject` type.
- Removed `LookAheadMethods` trait and redundant `ConcreteLookAheadSelection` type, making all APIs accessible as inherent methods on `LookAheadSelection` and `LookAheadChildren` decoupled types:
- Moved `LookAheadMethods::child_names()` to `LookAheadChildren::names()`.
- Moved `LookAheadMethods::has_children()` to `LookAheadChildren::is_empty()`.
- Moved `LookAheadMethods::select_child()` to `LookAheadChildren::select()`.
- Moved `LookAheadSelection::for_explicit_type()` to `LookAheadSelection::children_for_explicit_type()`.
- Made `LookAheadSelection::arguments()` returning iterator over `LookAheadArgument`.
- Made `LookAheadSelection::children()` returning `LookAheadChildren`.
- Added `Span` to `Arguments` and `LookAheadArguments`. ([#1206], [#1209])
- Disabled `bson`, `url`, `uuid` and `schema-language` [Cargo feature]s by default. ([#1230])
- Added `Span` to `Arguments` and `LookAheadArguments`. ([#1206], [#1209])
### Added
@ -155,15 +70,12 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- `LookAheadMethods::applies_for()` method. ([#1138], [#1145])
- `LookAheadMethods::field_original_name()` and `LookAheadMethods::field_alias()` methods. ([#1199])
- [`anyhow` crate] integration behind `anyhow` and `backtrace` [Cargo feature]s. ([#1215], [#988])
- `RootNode::disable_introspection()` applying additional `validation::rules::disable_introspection`, and `RootNode::enable_introspection()` reverting it. ([#1227], [#456])
- `Clone` and `PartialEq` implementations for `GraphQLResponse`. ([#1228], [#103])
### Changed
- Made `GraphQLRequest` fields public. ([#750])
- Relaxed [object safety] requirement for `GraphQLValue` and `GraphQLValueAsync` traits. ([ba1ed85b])
- Updated [GraphQL Playground] to 1.7.28 version. ([#1190])
- Improve validation errors for input values. ([#811], [#693])
## Fixed
@ -174,18 +86,12 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- Incorrect input value coercion with defaults. ([#1080], [#1073])
- Incorrect error when explicit `null` provided for `null`able list input parameter. ([#1086], [#1085])
- Stack overflow on nested GraphQL fragments. ([CVE-2022-31173])
- Unstable definitions order in schema generated by `RootNode::as_sdl()`. ([#1237], [#1134])
- Unstable definitions order in schema generated by `introspect()` or other introspection queries. ([#1239], [#1134])
[#103]: /../../issues/103
[#113]: /../../issues/113
[#456]: /../../issues/456
[#503]: /../../issues/503
[#528]: /../../issues/528
[#693]: /../../issues/693
[#750]: /../../issues/750
[#798]: /../../issues/798
[#811]: /../../pull/811
[#918]: /../../issues/918
[#965]: /../../pull/965
[#966]: /../../pull/966
@ -219,7 +125,6 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
[#1086]: /../../pull/1086
[#1118]: /../../issues/1118
[#1119]: /../../pull/1119
[#1134]: /../../issues/1134
[#1138]: /../../issues/1138
[#1145]: /../../pull/1145
[#1147]: /../../pull/1147
@ -228,18 +133,13 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
[#1190]: /../../pull/1190
[#1193]: /../../pull/1193
[#1199]: /../../pull/1199
[#1200]: /../../pull/1200
[#1206]: /../../pull/1206
[#1207]: /../../pull/1207
[#1208]: /../../pull/1208
[#1209]: /../../pull/1209
[#1212]: /../../pull/1212
[#1215]: /../../pull/1215
[#1227]: /../../pull/1227
[#1228]: /../../pull/1228
[#1230]: /../../pull/1230
[#1237]: /../../pull/1237
[#1239]: /../../pull/1239
[#1246]: /../../pull/1246
[#1221]: /../../pull/1221
[ba1ed85b]: /../../commit/ba1ed85b3c3dd77fbae7baf6bc4e693321a94083
[CVE-2022-31173]: /../../security/advisories/GHSA-4rx6-g5vg-5f3j
@ -248,7 +148,7 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
## Previous releases
See [old CHANGELOG](/../../blob/juniper-v0.15.12/juniper/CHANGELOG.md).
See [old CHANGELOG](/../../blob/juniper-v0.15.9/juniper/CHANGELOG.md).
@ -258,14 +158,12 @@ See [old CHANGELOG](/../../blob/juniper-v0.15.12/juniper/CHANGELOG.md).
[`bson` crate]: https://docs.rs/bson
[`chrono` crate]: https://docs.rs/chrono
[`chrono-tz` crate]: https://docs.rs/chrono-tz
[`jiff` crate]: https://docs.rs/jiff
[`time` crate]: https://docs.rs/time
[Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
[`graphql-transport-ws` GraphQL over WebSocket Protocol]: https://github.com/enisdenjo/graphql-ws/v5.14.0/PROTOCOL.md
[GraphiQL]: https://github.com/graphql/graphiql
[GraphQL Playground]: https://github.com/prisma/graphql-playground
[graphql-scalars.dev]: https://graphql-scalars.dev
[MSRV]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field
[October 2021]: https://spec.graphql.org/October2021
[object safety]: https://doc.rust-lang.org/reference/items/traits.html#object-safety
[orphan rules]: https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules

View file

@ -1,8 +1,8 @@
[package]
name = "juniper"
version = "0.16.1"
version = "0.16.0-dev"
edition = "2021"
rust-version = "1.75"
rust-version = "1.73"
description = "GraphQL server library."
license = "BSD-2-Clause"
authors = [
@ -13,7 +13,7 @@ authors = [
"Kai Ren <tyranron@gmail.com>",
]
documentation = "https://docs.rs/juniper"
homepage = "https://graphql-rust.github.io/juniper"
homepage = "https://graphql-rust.github.io"
repository = "https://github.com/graphql-rust/juniper"
readme = "README.md"
categories = ["asynchronous", "web-programming", "web-programming::http-server"]
@ -25,18 +25,24 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[features]
default = [
"bson",
"schema-language",
"url",
"uuid",
]
anyhow = ["dep:anyhow"]
backtrace = ["anyhow?/backtrace"]
bigdecimal = ["dep:bigdecimal", "dep:num-bigint", "dep:ryu"]
bson = ["dep:bson", "dep:tap"]
bson = ["dep:bson"]
chrono = ["dep:chrono"]
chrono-clock = ["chrono", "chrono/clock"]
chrono-tz = ["dep:chrono-tz", "dep:regex"]
expose-test-schema = ["dep:anyhow", "dep:serde_json"]
jiff = ["dep:jiff"]
graphql-parser = ["dep:graphql-parser", "dep:void"]
js = ["chrono?/wasmbind", "time?/wasm-bindgen", "uuid?/js"]
rust_decimal = ["dep:rust_decimal"]
schema-language = ["dep:graphql-parser", "dep:void"]
schema-language = ["graphql-parser"]
time = ["dep:time"]
url = ["dep:url"]
uuid = ["dep:uuid"]
@ -44,24 +50,23 @@ uuid = ["dep:uuid"]
[dependencies]
anyhow = { version = "1.0.47", optional = true }
async-trait = "0.1.39"
auto_enums = "0.8"
bigdecimal = { version = "0.4", optional = true }
bson = { version = "2.4", optional = true }
bson = { version = "2.4", features = ["chrono-0_4"], optional = true }
chrono = { version = "0.4.30", features = ["alloc"], default-features = false, optional = true }
chrono-tz = { version = "0.10", default-features = false, optional = true }
fnv = "1.0.5"
chrono-tz = { version = "0.8", default-features = false, optional = true }
fnv = "1.0.3"
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
futures-enum = { version = "0.1.12", default-features = false }
graphql-parser = { version = "0.4", optional = true }
indexmap = { version = "2.0", features = ["serde"] }
jiff = { version = "0.1.16", features = ["std"], default-features = false, optional = true }
juniper_codegen = { version = "0.16.0", path = "../juniper_codegen" }
juniper_codegen = { version = "0.16.0-dev", path = "../juniper_codegen" }
rust_decimal = { version = "1.20", default-features = false, optional = true }
ryu = { version = "1.0", optional = true }
serde = { version = "1.0.122", features = ["derive"] }
serde_json = { version = "1.0.18", features = ["std"], default-features = false, optional = true }
smartstring = "1.0"
static_assertions = "1.1"
time = { version = "0.3.37", features = ["formatting", "macros", "parsing"], optional = true }
time = { version = "0.3", features = ["formatting", "macros", "parsing"], optional = true }
url = { version = "2.0", optional = true }
uuid = { version = "1.3", default-features = false, optional = true }
@ -70,24 +75,18 @@ uuid = { version = "1.3", default-features = false, optional = true }
num-bigint = { version = "0.4.2", optional = true }
# TODO: Try remove on upgrade of `chrono-tz` crate.
regex = { version = "1.6", features = ["std"], default-features = false, optional = true }
# TODO: Try remove on upgrade of `bson` crate.
tap = { version = "1.0.1", optional = true }
# TODO: Remove on upgrade to 0.4.1 version of `graphql-parser`.
void = { version = "1.0.2", optional = true }
[dev-dependencies]
bencher = "0.1.2"
chrono = { version = "0.4.30", features = ["alloc"], default-features = false }
jiff = { version = "0.1.16", features = ["tzdb-bundle-always"], default-features = false }
pretty_assertions = "1.0.0"
serde_json = "1.0.18"
serial_test = "3.0"
serial_test = "2.0"
tokio = { version = "1.0", features = ["macros", "time", "rt-multi-thread"] }
[[bench]]
name = "bench"
harness = false
path = "benches/bench.rs"
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(nightly)'] }

View file

@ -1,10 +1,6 @@
BSD 2-Clause License
Copyright (c) 2016-2025 Magnus Hallin <mhallin@fastmail.com>,
Christoph Herzog <chris@theduke.at>,
Christian Legnitto <christian@legnitto.com>,
Ilya Solovyiov <ilya.solovyiov@gmail.com>,
Kai Ren <tyranron@gmail.com>
Copyright (c) 2016-2022, Magnus Hallin
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -4,15 +4,15 @@ Juniper (GraphQL server library for Rust)
[![Crates.io](https://img.shields.io/crates/v/juniper.svg?maxAge=2592000)](https://crates.io/crates/juniper)
[![Documentation](https://docs.rs/juniper/badge.svg)](https://docs.rs/juniper)
[![CI](https://github.com/graphql-rust/juniper/workflows/CI/badge.svg?branch=master "CI")](https://github.com/graphql-rust/juniper/actions?query=workflow%3ACI+branch%3Amaster)
[![Rust 1.75+](https://img.shields.io/badge/rustc-1.75+-lightgray.svg "Rust 1.75+")](https://blog.rust-lang.org/2023/12/28/Rust-1.75.0.html)
[![Rust 1.73+](https://img.shields.io/badge/rustc-1.73+-lightgray.svg "Rust 1.73+")](https://blog.rust-lang.org/2023/10/05/Rust-1.73.0.html)
- [Juniper Book] ([current][Juniper Book] | [edge][Juniper Book edge])
- [Changelog](https://github.com/graphql-rust/juniper/blob/juniper-v0.16.1/juniper/CHANGELOG.md)
- [Changelog](https://github.com/graphql-rust/juniper/blob/master/juniper/CHANGELOG.md)
[GraphQL] is a data query language developed by [Facebook] and intended to serve mobile and web application frontends.
*[Juniper]* 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 [Rust] will allow.
*[Juniper]* 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.
[Juniper] doesn't include a web server - instead it provides building blocks to make integration with existing servers straightforward, including embedded [GraphiQL] and/or [GraphQL Playground] for easy debugging.
@ -42,20 +42,20 @@ As an exception to other [GraphQL] libraries for other languages, [Juniper] buil
## Integrations
### Types
### Data types
[Juniper] provides out-of-the-box integration for some very common [Rust] crates to make building schemas a breeze. The types from these crates will be usable in your schemas automatically after enabling the correspondent self-titled [Cargo feature]:
- [`bigdecimal`]
[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:
- [`bigdecimal`] (feature gated)
- [`bson`]
- [`chrono`], [`chrono-tz`]
- [`jiff`]
- [`rust_decimal`]
- [`time`]
- [`chrono`] (feature gated)
- [`chrono-tz`] (feature gated)
- [`rust_decimal`] (feature gated)
- [`time`] (feature gated)
- [`url`]
- [`uuid`]
### Web server frameworks
### Web servers
- [`actix-web`] ([`juniper_actix`] crate)
- [`axum`] ([`juniper_axum`] crate)
@ -66,7 +66,7 @@ As an exception to other [GraphQL] libraries for other languages, [Juniper] buil
## API stability
## API Stability
[Juniper] has not reached 1.0 yet, thus some API instability should be expected.
@ -75,7 +75,7 @@ As an exception to other [GraphQL] libraries for other languages, [Juniper] buil
## License
This project is licensed under [BSD 2-Clause License](https://github.com/graphql-rust/juniper/blob/juniper-v0.16.1/juniper/LICENSE).
This project is licensed under [BSD 2-Clause License](https://github.com/graphql-rust/juniper/blob/master/juniper/LICENSE).
@ -86,7 +86,6 @@ This project is licensed under [BSD 2-Clause License](https://github.com/graphql
[`bson`]: https://docs.rs/bson
[`chrono`]: https://docs.rs/chrono
[`chrono-tz`]: https://docs.rs/chrono-tz
[`jiff`]: https://docs.rs/jiff
[`juniper_actix`]: https://docs.rs/juniper_actix
[`juniper_axum`]: https://docs.rs/juniper_axum
[`juniper_hyper`]: https://docs.rs/juniper_hyper
@ -99,14 +98,13 @@ This project is licensed under [BSD 2-Clause License](https://github.com/graphql
[`url`]: https://docs.rs/url
[`uuid`]: https://docs.rs/uuid
[`warp`]: https://docs.rs/warp
[Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
[Facebook]: https://facebook.com
[GraphiQL]: https://github.com/graphql/graphiql
[GraphQL]: http://graphql.org
[GraphQL Playground]: https://github.com/graphql/graphql-playground
[Juniper]: https://docs.rs/juniper
[Juniper Book]: https://graphql-rust.github.io/juniper
[Juniper Book]: https://graphql-rust.github.io
[Juniper Book edge]: https://graphql-rust.github.io/juniper/master
[Rust]: https://www.rust-lang.org
[1]: https://graphql-rust.github.io/juniper/quickstart.html
[1]: https://graphql-rust.github.io/quickstart.html

View file

@ -4,7 +4,7 @@
"postinstall": "make graphiql graphql-playground"
},
"dependencies": {
"graphiql": "3.8.3",
"graphiql": "3.0.10",
"graphql-playground-react": "1.7.28"
}
}

View file

@ -1,3 +1,34 @@
[[pre-release-replacements]]
file = "../book/src/advanced/dataloaders.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/advanced/subscriptions.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/quickstart.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/servers/hyper.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/servers/rocket.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/servers/warp.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../juniper_actix/Cargo.toml"
exactly = 2
@ -58,79 +89,3 @@ file = "README.md"
exactly = 2
search = "graphql-rust/juniper/blob/[^/]+/"
replace = "graphql-rust/juniper/blob/{{crate_name}}-v{{version}}/"
[[pre-release-replacements]]
file = "../book/src/quickstart.md"
exactly = 1
search = "juniper = \"[^\"]+\""
replace = "juniper = \"{{version}}\""
[[pre-release-replacements]]
file = "../book/src/quickstart.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/advanced/implicit_and_explicit_null.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/advanced/lookahead.md"
exactly = 6
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/schema/index.md"
exactly = 5
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/schema/introspection.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/schema/subscriptions.md"
exactly = 3
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/enums.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/input_objects.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/interfaces.md"
exactly = 2
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/scalars.md"
exactly = 4
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/unions.md"
exactly = 1
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/objects/complex_fields.md"
exactly = 2
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/objects/index.md"
exactly = 3
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"
[[pre-release-replacements]]
file = "../book/src/types/objects/error/field.md"
exactly = 3
search = "docs.rs/juniper/[^/]+/"
replace = "docs.rs/juniper/{{version}}/"

View file

@ -194,7 +194,7 @@ pub trait ToInputValue<S = DefaultScalarValue>: Sized {
fn to_input_value(&self) -> InputValue<S>;
}
impl Type<'_> {
impl<'a> Type<'a> {
/// Get the name of a named type.
///
/// Only applies to named types; lists will return `None`.
@ -221,7 +221,7 @@ impl Type<'_> {
}
}
impl fmt::Display for Type<'_> {
impl<'a> fmt::Display for Type<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Named(n) => write!(f, "{n}"),

File diff suppressed because it is too large Load diff

View file

@ -37,7 +37,7 @@ use crate::{
pub use self::{
look_ahead::{
Applies, LookAheadArgument, LookAheadChildren, LookAheadList, LookAheadObject,
Applies, ConcreteLookAheadSelection, LookAheadArgument, LookAheadMethods,
LookAheadSelection, LookAheadValue,
},
owned_executor::OwnedExecutor,
@ -87,7 +87,7 @@ where
///
/// All execution errors contain the source position in the query of the field
/// that failed to resolve. It also contains the field stack.
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, PartialEq)]
pub struct ExecutionError<S> {
location: SourcePosition,
path: Vec<String>,
@ -273,7 +273,7 @@ impl<S> IntoFieldError<S> for std::convert::Infallible {
}
}
impl<S> IntoFieldError<S> for &str {
impl<'a, S> IntoFieldError<S> for &'a str {
fn into_field_error(self) -> FieldError<S> {
FieldError::<S>::from(self)
}
@ -285,7 +285,7 @@ impl<S> IntoFieldError<S> for String {
}
}
impl<S> IntoFieldError<S> for Cow<'_, str> {
impl<'a, S> IntoFieldError<S> for Cow<'a, str> {
fn into_field_error(self) -> FieldError<S> {
FieldError::<S>::from(self)
}
@ -403,7 +403,7 @@ pub trait FromContext<T> {
/// Marker trait for types that can act as context objects for `GraphQL` types.
pub trait Context {}
impl<C: Context> Context for &C {}
impl<'a, C: Context> Context for &'a C {}
static NULL_CONTEXT: () = ();
@ -698,38 +698,48 @@ where
};
self.parent_selection_set
.and_then(|p| {
// Search the parent's fields to find this field within the selection set.
p.iter().find_map(|x| {
match x {
// Search the parent's fields to find this field within the set
let found_field = p.iter().find(|&x| {
match *x {
Selection::Field(ref field) => {
let field = &field.item;
// TODO: support excludes.
let name = field.name.item;
let alias = field.alias.as_ref().map(|a| a.item);
(alias.unwrap_or(name) == field_name).then(|| {
LookAheadSelection::new(
look_ahead::SelectionSource::Field(field),
self.variables,
self.fragments,
)
})
alias.unwrap_or(name) == field_name
}
Selection::FragmentSpread(_) | Selection::InlineFragment(_) => None,
_ => false,
}
})
});
if let Some(p) = found_field {
LookAheadSelection::build_from_selection(p, self.variables, self.fragments)
} else {
None
}
})
.unwrap_or_else(|| {
// We didn't find this field in the parent's selection matching it, which means
// we're inside a `FragmentSpread`.
LookAheadSelection::new(
look_ahead::SelectionSource::Spread {
field_name,
set: self.current_selection_set,
},
self.variables,
self.fragments,
)
// We didn't find a field in the parent's selection matching
// this field, which means we're inside a FragmentSpread
let mut ret = LookAheadSelection {
name: field_name,
alias: None,
arguments: Vec::new(),
children: Vec::new(),
applies_for: Applies::All,
};
// Add in all the children - this will mutate `ret`
if let Some(selection_set) = self.current_selection_set {
for c in selection_set {
LookAheadSelection::build_from_selection_with_parent(
c,
Some(&mut ret),
self.variables,
self.fragments,
);
}
}
ret
})
}
@ -755,7 +765,7 @@ where
}
}
impl FieldPath<'_> {
impl<'a> FieldPath<'a> {
fn construct_path(&self, acc: &mut Vec<String>) {
match self {
FieldPath::Root(_) => (),

View file

@ -25,7 +25,7 @@ pub struct OwnedExecutor<'a, CtxT, S> {
pub(super) field_path: Arc<FieldPath<'a>>,
}
impl<CtxT, S> Clone for OwnedExecutor<'_, CtxT, S>
impl<'a, CtxT, S> Clone for OwnedExecutor<'a, CtxT, S>
where
S: Clone,
{

View file

@ -100,7 +100,7 @@ async fn does_not_accept_string_literals() {
assert_eq!(
error,
ValidationError(vec![RuleError::new(
r#"Invalid value for argument "color", reason: Invalid value ""RED"" for enum "Color""#,
r#"Invalid value for argument "color", expected type "Color!""#,
&[SourcePosition::new(18, 0, 18)],
)])
);

View file

@ -6,7 +6,6 @@ mod interface {
GraphQLObject,
};
#[allow(dead_code)] // TODO: Consider this for the GraphQL interfaces in the expansion.
#[graphql_interface(for = [Cat, Dog])]
trait Pet {
fn name(&self) -> &str;

View file

@ -24,7 +24,6 @@ enum Sample {
struct Scalar(i32);
/// A sample interface
#[allow(dead_code)] // TODO: Consider this for the GraphQL interfaces in the expansion.
#[graphql_interface(name = "SampleInterface", for = Root)]
trait Interface {
/// A sample field in the interface

View file

@ -916,8 +916,7 @@ async fn does_not_allow_missing_required_field() {
assert_eq!(
error,
ValidationError(vec![RuleError::new(
"Invalid value for argument \"arg\", \
reason: \"ExampleInputObject\" is missing fields: \"b\"",
r#"Invalid value for argument "arg", expected type "ExampleInputObject!""#,
&[SourcePosition::new(20, 0, 20)],
)]),
);
@ -941,9 +940,7 @@ async fn does_not_allow_null_in_required_field() {
assert_eq!(
error,
ValidationError(vec![RuleError::new(
"Invalid value for argument \"arg\", \
reason: Error on \"ExampleInputObject\" field \"b\": \
\"null\" specified for not nullable type \"Int!\"",
r#"Invalid value for argument "arg", expected type "ExampleInputObject!""#,
&[SourcePosition::new(20, 0, 20)],
)]),
);

View file

@ -30,11 +30,11 @@
-->
<script
crossorigin
src="https://unpkg.com/react@18/umd/react.production.min.js"
src="https://unpkg.com/react@18/umd/react.development.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
></script>
<!--
These two files can be found in the npm module, however you may wish to
@ -42,10 +42,10 @@
favored resource bundler.
-->
<script
src="https://unpkg.com/graphiql@3.8.3/graphiql.min.js"
src="https://unpkg.com/graphiql@3.0.10/graphiql.min.js"
type="application/javascript"
></script>
<link rel="stylesheet" href="https://unpkg.com/graphiql@3.8.3/graphiql.min.css" />
<link rel="stylesheet" href="https://unpkg.com/graphiql@3.0.10/graphiql.min.css" />
<!--
These are imports for the GraphIQL Explorer plugin.
-->

View file

@ -12,19 +12,25 @@ pub fn graphiql_source(
graphql_endpoint_url: &str,
subscriptions_endpoint_url: Option<&str>,
) -> String {
let subscriptions_endpoint = if let Some(sub_url) = subscriptions_endpoint_url {
sub_url
} else {
""
};
include_str!("graphiql.html").replace(
"<!-- inject -->",
&format!(
// language=JavaScript
"
var JUNIPER_URL = '{juniper_url}';
var JUNIPER_SUBSCRIPTIONS_URL = '{juniper_subscriptions_url}';
var JUNIPER_URL = '{graphql_url}';
var JUNIPER_SUBSCRIPTIONS_URL = '{graphql_subscriptions_url}';
{grahiql_js}
",
juniper_url = graphql_endpoint_url,
juniper_subscriptions_url = subscriptions_endpoint_url.unwrap_or_default(),
graphql_url = graphql_endpoint_url,
graphql_subscriptions_url = subscriptions_endpoint,
grahiql_js = include_str!("graphiql.js"),
),
)

View file

@ -163,7 +163,7 @@ where
/// This struct implements Serialize, so you can simply serialize this
/// to JSON and send it over the wire. Use the `is_ok` method to determine
/// whether to send a 200 or 400 HTTP status code.
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug)]
pub struct GraphQLResponse<S = DefaultScalarValue>(
Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError>,
);

View file

@ -1,35 +1,8 @@
//! GraphQL support for [`bson`] crate types.
//!
//! # Supported types
//!
//! | Rust type | Format | GraphQL scalar |
//! |-------------------|-------------------|------------------|
//! | [`oid::ObjectId`] | HEX string | [`ObjectID`][s1] |
//! | [`DateTime`] | [RFC 3339] string | [`DateTime`][s4] |
//!
//! [`DateTime`]: bson::DateTime
//! [`oid::ObjectId`]: bson::oid::ObjectId
//! [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
//! [s1]: https://graphql-scalars.dev/docs/scalars/object-id
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
//! GraphQL support for [bson](https://github.com/mongodb/bson-rust) types.
use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// [BSON ObjectId][0] represented as a HEX string.
///
/// [`ObjectID` scalar][1] compliant.
///
/// See also [`bson::oid::ObjectId`][2] for details.
///
/// [0]: https://www.mongodb.com/docs/manual/reference/bson-types#objectid
/// [1]: https://graphql-scalars.dev/docs/scalars/object-id
/// [2]: https://docs.rs/bson/*/bson/oid/struct.ObjectId.html
#[graphql_scalar(
name = "ObjectID",
with = object_id,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/object-id",
)]
#[graphql_scalar(with = object_id, parse_token(String))]
type ObjectId = bson::oid::ObjectId;
mod object_id {
@ -43,54 +16,37 @@ mod object_id {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| {
ObjectId::parse_str(s).map_err(|e| format!("Failed to parse `ObjectID`: {e}"))
ObjectId::parse_str(s).map_err(|e| format!("Failed to parse `ObjectId`: {e}"))
})
}
}
/// [BSON date][3] in [RFC 3339][0] format.
///
/// [BSON datetimes][3] have millisecond precision and are always in UTC (inputs with other
/// timezones are coerced).
///
/// [`DateTime` scalar][1] compliant.
///
/// See also [`bson::DateTime`][2] for details.
///
/// [0]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
/// [1]: https://graphql-scalars.dev/docs/scalars/date-time
/// [2]: https://docs.rs/bson/*/bson/struct.DateTime.html
/// [3]: https://www.mongodb.com/docs/manual/reference/bson-types#date
#[graphql_scalar(
with = date_time,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date-time",
)]
type DateTime = bson::DateTime;
#[graphql_scalar(with = utc_date_time, parse_token(String))]
type UtcDateTime = bson::DateTime;
mod date_time {
mod utc_date_time {
use super::*;
pub(super) fn to_output<S: ScalarValue>(v: &DateTime) -> Value<S> {
pub(super) fn to_output<S: ScalarValue>(v: &UtcDateTime) -> Value<S> {
Value::scalar(
(*v).try_to_rfc3339_string()
.unwrap_or_else(|e| panic!("failed to format `DateTime` as RFC 3339: {e}")),
.unwrap_or_else(|e| panic!("failed to format `UtcDateTime` as RFC3339: {e}")),
)
}
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<DateTime, String> {
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<UtcDateTime, String> {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| {
DateTime::parse_rfc3339_str(s)
.map_err(|e| format!("Failed to parse `DateTime`: {e}"))
UtcDateTime::parse_rfc3339_str(s)
.map_err(|e| format!("Failed to parse `UtcDateTime`: {e}"))
})
}
}
#[cfg(test)]
mod test {
use bson::oid::ObjectId;
use bson::{oid::ObjectId, DateTime as UtcDateTime};
use crate::{graphql_input_value, FromInputValue, InputValue};
@ -104,161 +60,21 @@ mod test {
assert_eq!(parsed, id);
}
}
#[cfg(test)]
mod date_time_test {
use crate::{graphql_input_value, FromInputValue as _, InputValue, ToInputValue as _};
use super::DateTime;
#[test]
fn parses_correct_input() {
for (raw, expected) in [
(
"2014-11-28T21:00:09+09:00",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(12)
.second(9)
.build()
.unwrap(),
),
(
"2014-11-28T21:00:09Z",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(21)
.second(9)
.build()
.unwrap(),
),
(
"2014-11-28 21:00:09z",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(21)
.second(9)
.build()
.unwrap(),
),
(
"2014-11-28T21:00:09+00:00",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(21)
.second(9)
.build()
.unwrap(),
),
(
"2014-11-28T21:00:09.05+09:00",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(12)
.second(9)
.millisecond(50)
.build()
.unwrap(),
),
(
"2014-11-28 21:00:09.05+09:00",
DateTime::builder()
.year(2014)
.month(11)
.day(28)
.hour(12)
.second(9)
.millisecond(50)
.build()
.unwrap(),
),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = DateTime::from_input_value(&input);
fn utcdatetime_from_input() {
use chrono::{DateTime, Utc};
assert!(
parsed.is_ok(),
"failed to parse `{raw}`: {:?}",
parsed.unwrap_err(),
);
assert_eq!(parsed.unwrap(), expected, "input: {raw}");
}
}
let raw = "2020-03-23T17:38:32.446+00:00";
let input: InputValue = graphql_input_value!((raw));
#[test]
fn fails_on_invalid_input() {
for input in [
graphql_input_value!("12"),
graphql_input_value!("12:"),
graphql_input_value!("56:34:22"),
graphql_input_value!("56:34:22.000"),
graphql_input_value!("1996-12-1914:23:43"),
graphql_input_value!("1996-12-19T14:23:43"),
graphql_input_value!("1996-12-19T14:23:43ZZ"),
graphql_input_value!("1996-12-19T14:23:43.543"),
graphql_input_value!("1996-12-19T14:23"),
graphql_input_value!("1996-12-19T14:23:1"),
graphql_input_value!("1996-12-19T14:23:"),
graphql_input_value!("1996-12-19T23:78:43Z"),
graphql_input_value!("1996-12-19T23:18:99Z"),
graphql_input_value!("1996-12-19T24:00:00Z"),
graphql_input_value!("1996-12-19T99:02:13Z"),
graphql_input_value!("1996-12-19T99:02:13Z"),
graphql_input_value!("1996-12-19T12:02:13+4444444"),
graphql_input_value!("i'm not even a datetime"),
graphql_input_value!(2.32),
graphql_input_value!(1),
graphql_input_value!(null),
graphql_input_value!(false),
] {
let input: InputValue = input;
let parsed = DateTime::from_input_value(&input);
let parsed: UtcDateTime = FromInputValue::from_input_value(&input).unwrap();
let date_time = UtcDateTime::from_chrono(
DateTime::parse_from_rfc3339(raw)
.unwrap()
.with_timezone(&Utc),
);
assert!(parsed.is_err(), "allows input: {input:?}");
}
}
#[test]
fn formats_correctly() {
for (val, expected) in [
(
DateTime::builder()
.year(1996)
.month(12)
.day(19)
.hour(12)
.build()
.unwrap(),
graphql_input_value!("1996-12-19T12:00:00Z"),
),
(
DateTime::builder()
.year(1564)
.month(1)
.day(30)
.hour(5)
.minute(3)
.second(3)
.millisecond(1)
.build()
.unwrap(),
graphql_input_value!("1564-01-30T05:03:03.001Z"),
),
] {
let actual: InputValue = val.to_input_value();
assert_eq!(actual, expected, "on value: {val}");
}
assert_eq!(parsed, date_time);
}
}

View file

@ -2,21 +2,20 @@
//!
//! # Supported types
//!
//! | Rust type | Format | GraphQL scalar |
//! |-------------------|-----------------------|-----------------------|
//! | [`NaiveDate`] | `yyyy-MM-dd` | [`LocalDate`][s1] |
//! | [`NaiveTime`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`NaiveDateTime`] | `yyyy-MM-ddTHH:mm:ss` | [`LocalDateTime`][s3] |
//! | [`DateTime`] | [RFC 3339] string | [`DateTime`][s4] |
//! | Rust type | Format | GraphQL scalar |
//! |-------------------|-----------------------|-------------------|
//! | [`NaiveDate`] | `yyyy-MM-dd` | [`Date`][s1] |
//! | [`NaiveTime`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`NaiveDateTime`] | `yyyy-MM-dd HH:mm:ss` | `LocalDateTime` |
//! | [`DateTime`] | [RFC 3339] string | [`DateTime`][s4] |
//!
//! [`DateTime`]: chrono::DateTime
//! [`NaiveDate`]: chrono::naive::NaiveDate
//! [`NaiveDateTime`]: chrono::naive::NaiveDateTime
//! [`NaiveTime`]: chrono::naive::NaiveTime
//! [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
//! [s1]: https://graphql-scalars.dev/docs/scalars/local-date
//! [s1]: https://graphql-scalars.dev/docs/scalars/date
//! [s2]: https://graphql-scalars.dev/docs/scalars/local-time
//! [s3]: https://graphql-scalars.dev/docs/scalars/local-date-time
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
use std::fmt;
@ -30,43 +29,42 @@ use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// Represents a description of the date (as used for birthdays, for example).
/// It cannot represent an instant on the time-line.
///
/// [`LocalDate` scalar][1] compliant.
/// [`Date` scalar][1] compliant.
///
/// See also [`chrono::NaiveDate`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
#[graphql_scalar(
with = local_date,
with = date,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date",
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date",
)]
pub type LocalDate = chrono::NaiveDate;
pub type Date = chrono::NaiveDate;
mod local_date {
mod date {
use super::*;
/// Format of a [`LocalDate` scalar][1].
/// Format of a [`Date` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
/// [1]: https://graphql-scalars.dev/docs/scalars/date
const FORMAT: &str = "%Y-%m-%d";
pub(super) fn to_output<S>(v: &LocalDate) -> Value<S>
pub(super) fn to_output<S>(v: &Date) -> Value<S>
where
S: ScalarValue,
{
Value::scalar(v.format(FORMAT).to_string())
}
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDate, String>
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<Date, String>
where
S: ScalarValue,
{
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| {
LocalDate::parse_from_str(s, FORMAT)
.map_err(|e| format!("Invalid `LocalDate`: {e}"))
Date::parse_from_str(s, FORMAT).map_err(|e| format!("Invalid `Date`: {e}"))
})
}
}
@ -142,28 +140,19 @@ mod local_time {
}
}
/// Combined date and time (without time zone) in `yyyy-MM-ddTHH:mm:ss` format.
/// Combined date and time (without time zone) in `yyyy-MM-dd HH:mm:ss` format.
///
/// [`LocalDateTime` scalar][1] compliant.
/// See also [`chrono::NaiveDateTime`][1] for details.
///
/// See also [`chrono::NaiveDateTime`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
#[graphql_scalar(
with = local_date_time,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date-time",
)]
/// [1]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
#[graphql_scalar(with = local_date_time, parse_token(String))]
pub type LocalDateTime = chrono::NaiveDateTime;
mod local_date_time {
use super::*;
/// Format of a [`LocalDateTime` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
const FORMAT: &str = "%Y-%m-%dT%H:%M:%S";
/// Format of a `LocalDateTime` scalar.
const FORMAT: &str = "%Y-%m-%d %H:%M:%S";
pub(super) fn to_output<S>(v: &LocalDateTime) -> Value<S>
where
@ -200,7 +189,6 @@ mod local_date_time {
#[graphql_scalar(
with = date_time,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date-time",
where(
Tz: TimeZone + FromFixedOffset,
Tz::Offset: fmt::Display,
@ -341,19 +329,19 @@ impl FromFixedOffset for chrono_tz::Tz {
}
#[cfg(test)]
mod local_date_test {
mod date_test {
use crate::{graphql_input_value, FromInputValue as _, InputValue, ToInputValue as _};
use super::LocalDate;
use super::Date;
#[test]
fn parses_correct_input() {
for (raw, expected) in [
("1996-12-19", LocalDate::from_ymd_opt(1996, 12, 19)),
("1564-01-30", LocalDate::from_ymd_opt(1564, 01, 30)),
("1996-12-19", Date::from_ymd_opt(1996, 12, 19)),
("1564-01-30", Date::from_ymd_opt(1564, 01, 30)),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = LocalDate::from_input_value(&input);
let parsed = Date::from_input_value(&input);
assert!(
parsed.is_ok(),
@ -381,7 +369,7 @@ mod local_date_test {
graphql_input_value!(false),
] {
let input: InputValue = input;
let parsed = LocalDate::from_input_value(&input);
let parsed = Date::from_input_value(&input);
assert!(parsed.is_err(), "allows input: {input:?}");
}
@ -391,15 +379,15 @@ mod local_date_test {
fn formats_correctly() {
for (val, expected) in [
(
LocalDate::from_ymd_opt(1996, 12, 19),
Date::from_ymd_opt(1996, 12, 19),
graphql_input_value!("1996-12-19"),
),
(
LocalDate::from_ymd_opt(1564, 01, 30),
Date::from_ymd_opt(1564, 01, 30),
graphql_input_value!("1564-01-30"),
),
(
LocalDate::from_ymd_opt(2020, 01, 01),
Date::from_ymd_opt(2020, 01, 01),
graphql_input_value!("2020-01-01"),
),
] {
@ -509,14 +497,14 @@ mod local_date_time_test {
fn parses_correct_input() {
for (raw, expected) in [
(
"1996-12-19T14:23:43",
"1996-12-19 14:23:43",
LocalDateTime::new(
NaiveDate::from_ymd_opt(1996, 12, 19).unwrap(),
NaiveTime::from_hms_opt(14, 23, 43).unwrap(),
),
),
(
"1564-01-30T14:00:00",
"1564-01-30 14:00:00",
LocalDateTime::new(
NaiveDate::from_ymd_opt(1564, 1, 30).unwrap(),
NaiveTime::from_hms_opt(14, 00, 00).unwrap(),
@ -542,17 +530,15 @@ mod local_date_time_test {
graphql_input_value!("12:"),
graphql_input_value!("56:34:22"),
graphql_input_value!("56:34:22.000"),
graphql_input_value!("1996-12-1914:23:43"),
graphql_input_value!("1996-12-19 14:23:43"),
graphql_input_value!("1996-12-19Q14:23:43"),
graphql_input_value!("1996-12-19T14:23:43Z"),
graphql_input_value!("1996-12-19T14:23:43.543"),
graphql_input_value!("1996-12-19T14:23"),
graphql_input_value!("1996-12-19T14:23:"),
graphql_input_value!("1996-12-19T23:78:43"),
graphql_input_value!("1996-12-19T23:18:99"),
graphql_input_value!("1996-12-19T24:00:00"),
graphql_input_value!("1996-12-19T99:02:13"),
graphql_input_value!("1996-12-19T14:23:43"),
graphql_input_value!("1996-12-19 14:23:43Z"),
graphql_input_value!("1996-12-19 14:23:43.543"),
graphql_input_value!("1996-12-19 14:23"),
graphql_input_value!("1996-12-19 14:23:"),
graphql_input_value!("1996-12-19 23:78:43"),
graphql_input_value!("1996-12-19 23:18:99"),
graphql_input_value!("1996-12-19 24:00:00"),
graphql_input_value!("1996-12-19 99:02:13"),
graphql_input_value!("i'm not even a datetime"),
graphql_input_value!(2.32),
graphql_input_value!(1),
@ -574,14 +560,14 @@ mod local_date_time_test {
NaiveDate::from_ymd_opt(1996, 12, 19).unwrap(),
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
),
graphql_input_value!("1996-12-19T00:00:00"),
graphql_input_value!("1996-12-19 00:00:00"),
),
(
LocalDateTime::new(
NaiveDate::from_ymd_opt(1564, 1, 30).unwrap(),
NaiveTime::from_hms_opt(14, 0, 0).unwrap(),
),
graphql_input_value!("1564-01-30T14:00:00"),
graphql_input_value!("1564-01-30 14:00:00"),
),
] {
let actual: InputValue = val.to_input_value();
@ -751,9 +737,7 @@ mod integration_test {
types::scalars::{EmptyMutation, EmptySubscription},
};
use super::{
DateTime, FixedOffset, FromFixedOffset, LocalDate, LocalDateTime, LocalTime, TimeZone,
};
use super::{Date, DateTime, FixedOffset, FromFixedOffset, LocalDateTime, LocalTime, TimeZone};
#[tokio::test]
async fn serializes() {
@ -800,8 +784,8 @@ mod integration_test {
#[graphql_object]
impl Root {
fn local_date() -> LocalDate {
LocalDate::from_ymd_opt(2015, 3, 14).unwrap()
fn date() -> Date {
Date::from_ymd_opt(2015, 3, 14).unwrap()
}
fn local_time() -> LocalTime {
@ -810,7 +794,7 @@ mod integration_test {
fn local_date_time() -> LocalDateTime {
LocalDateTime::new(
LocalDate::from_ymd_opt(2016, 7, 8).unwrap(),
Date::from_ymd_opt(2016, 7, 8).unwrap(),
LocalTime::from_hms_opt(9, 10, 11).unwrap(),
)
}
@ -818,7 +802,7 @@ mod integration_test {
fn date_time() -> DateTime<chrono::Utc> {
DateTime::from_naive_utc_and_offset(
LocalDateTime::new(
LocalDate::from_ymd_opt(1996, 12, 20).unwrap(),
Date::from_ymd_opt(1996, 12, 20).unwrap(),
LocalTime::from_hms_opt(0, 39, 57).unwrap(),
),
chrono::Utc,
@ -835,7 +819,7 @@ mod integration_test {
}
const DOC: &str = r#"{
localDate
date
localTime
localDateTime
dateTime,
@ -853,9 +837,9 @@ mod integration_test {
execute(DOC, None, &schema, &graphql_vars! {}, &()).await,
Ok((
graphql_value!({
"localDate": "2015-03-14",
"date": "2015-03-14",
"localTime": "16:07:08",
"localDateTime": "2016-07-08T09:10:11",
"localDateTime": "2016-07-08 09:10:11",
"dateTime": "1996-12-20T00:39:57Z",
"passDateTime": "2014-11-28T12:00:09Z",
"transformDateTime": "2014-11-28T12:00:09Z",

View file

@ -2,35 +2,27 @@
//!
//! # Supported types
//!
//! | Rust type | Format | GraphQL scalar |
//! |-----------|--------------------|------------------|
//! | [`Tz`] | [IANA database][1] | [`TimeZone`][s1] |
//! | Rust type | Format | GraphQL scalar |
//! |-----------|--------------------|----------------|
//! | [`Tz`] | [IANA database][1] | `TimeZone` |
//!
//! [`chrono-tz`]: chrono_tz
//! [`Tz`]: chrono_tz::Tz
//! [1]: http://www.iana.org/time-zones
//! [s1]: https://graphql-scalars.dev/docs/scalars/time-zone
use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// Timezone based on [`IANA` database][0].
/// Timezone based on [`IANA` database][1].
///
/// See ["List of tz database time zones"][3] `TZ database name` column for
/// See ["List of tz database time zones"][2] `TZ database name` column for
/// available names.
///
/// [`TimeZone` scalar][1] compliant.
/// See also [`chrono_tz::Tz`][3] for detals.
///
/// See also [`chrono_tz::Tz`][2] for details.
///
/// [0]: https://www.iana.org/time-zones
/// [1]: https://graphql-scalars.dev/docs/scalars/time-zone
/// [2]: https://docs.rs/chrono-tz/*/chrono_tz/enum.Tz.html
/// [3]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
#[graphql_scalar(
with = tz,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/time-zone",
)]
/// [1]: https://www.iana.org/time-zones
/// [2]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
/// [3]: https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html
#[graphql_scalar(with = tz, parse_token(String))]
pub type TimeZone = chrono_tz::Tz;
mod tz {
@ -86,7 +78,7 @@ mod test {
fn forward_slash() {
tz_input_test(
"Abc/Xyz",
Err("Failed to parse `TimeZone`: failed to parse timezone"),
Err("Failed to parse `TimeZone`: received invalid timezone"),
);
}
@ -94,7 +86,7 @@ mod test {
fn number() {
tz_input_test(
"8086",
Err("Failed to parse `TimeZone`: failed to parse timezone"),
Err("Failed to parse `TimeZone`: received invalid timezone"),
);
}
@ -102,7 +94,7 @@ mod test {
fn no_forward_slash() {
tz_input_test(
"AbcXyz",
Err("Failed to parse `TimeZone`: failed to parse timezone"),
Err("Failed to parse `TimeZone`: received invalid timezone"),
);
}
}

File diff suppressed because it is too large Load diff

View file

@ -10,8 +10,6 @@ pub mod bson;
pub mod chrono;
#[cfg(feature = "chrono-tz")]
pub mod chrono_tz;
#[cfg(feature = "jiff")]
pub mod jiff;
#[cfg(feature = "rust_decimal")]
pub mod rust_decimal;
#[doc(hidden)]

View file

@ -289,7 +289,7 @@ impl<'de> Deserialize<'de> for DefaultScalarValue {
fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
struct Visitor;
impl de::Visitor<'_> for Visitor {
impl<'de> de::Visitor<'de> for Visitor {
type Value = DefaultScalarValue;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {

View file

@ -2,13 +2,13 @@
//!
//! # Supported types
//!
//! | Rust type | Format | GraphQL scalar |
//! |-----------------------|-----------------------|-----------------------|
//! | [`Date`] | `yyyy-MM-dd` | [`LocalDate`][s1] |
//! | [`Time`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`PrimitiveDateTime`] | `yyyy-MM-ddTHH:mm:ss` | [`LocalDateTime`][s3] |
//! | [`OffsetDateTime`] | [RFC 3339] string | [`DateTime`][s4] |
//! | [`UtcOffset`] | `±hh:mm` | [`UtcOffset`][s5] |
//! | Rust type | Format | GraphQL scalar |
//! |-----------------------|-----------------------|---------------------|
//! | [`Date`] | `yyyy-MM-dd` | [`Date`][s1] |
//! | [`Time`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`PrimitiveDateTime`] | `yyyy-MM-dd HH:mm:ss` | `LocalDateTime` |
//! | [`OffsetDateTime`] | [RFC 3339] string | [`DateTime`][s4] |
//! | [`UtcOffset`] | `±hh:mm` | [`UtcOffset`][s5] |
//!
//! [`Date`]: time::Date
//! [`OffsetDateTime`]: time::OffsetDateTime
@ -16,14 +16,13 @@
//! [`Time`]: time::Time
//! [`UtcOffset`]: time::UtcOffset
//! [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
//! [s1]: https://graphql-scalars.dev/docs/scalars/local-date
//! [s1]: https://graphql-scalars.dev/docs/scalars/date
//! [s2]: https://graphql-scalars.dev/docs/scalars/local-time
//! [s3]: https://graphql-scalars.dev/docs/scalars/local-date-time
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
//! [s5]: https://graphql-scalars.dev/docs/scalars/utc-offset
use time::{
format_description::{well_known::Rfc3339, BorrowedFormatItem},
format_description::{well_known::Rfc3339, FormatItem},
macros::format_description,
};
@ -34,40 +33,38 @@ use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// Represents a description of the date (as used for birthdays, for example).
/// It cannot represent an instant on the time-line.
///
/// [`LocalDate` scalar][1] compliant.
/// [`Date` scalar][1] compliant.
///
/// See also [`time::Date`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [2]: https://docs.rs/time/*/time/struct.Date.html
#[graphql_scalar(
with = local_date,
with = date,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date",
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date",
)]
pub type LocalDate = time::Date;
pub type Date = time::Date;
mod local_date {
mod date {
use super::*;
/// Format of a [`LocalDate` scalar][1].
/// Format of a [`Date` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
const FORMAT: &[BorrowedFormatItem<'_>] = format_description!("[year]-[month]-[day]");
/// [1]: https://graphql-scalars.dev/docs/scalars/date
const FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
pub(super) fn to_output<S: ScalarValue>(v: &LocalDate) -> Value<S> {
pub(super) fn to_output<S: ScalarValue>(v: &Date) -> Value<S> {
Value::scalar(
v.format(FORMAT)
.unwrap_or_else(|e| panic!("failed to format `LocalDate`: {e}")),
.unwrap_or_else(|e| panic!("Failed to format `Date`: {e}")),
)
}
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<LocalDate, String> {
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Date, String> {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| {
LocalDate::parse(s, FORMAT).map_err(|e| format!("Invalid `LocalDate`: {e}"))
})
.and_then(|s| Date::parse(s, FORMAT).map_err(|e| format!("Invalid `Date`: {e}")))
}
}
@ -83,11 +80,7 @@ mod local_date {
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
/// [2]: https://docs.rs/time/*/time/struct.Time.html
#[graphql_scalar(
with = local_time,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-time",
)]
#[graphql_scalar(with = local_time, parse_token(String))]
pub type LocalTime = time::Time;
mod local_time {
@ -96,19 +89,18 @@ mod local_time {
/// Full format of a [`LocalTime` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
const FORMAT: &[BorrowedFormatItem<'_>] =
const FORMAT: &[FormatItem<'_>] =
format_description!("[hour]:[minute]:[second].[subsecond digits:3]");
/// Format of a [`LocalTime` scalar][1] without milliseconds.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
const FORMAT_NO_MILLIS: &[BorrowedFormatItem<'_>] =
format_description!("[hour]:[minute]:[second]");
const FORMAT_NO_MILLIS: &[FormatItem<'_>] = format_description!("[hour]:[minute]:[second]");
/// Format of a [`LocalTime` scalar][1] without seconds.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
const FORMAT_NO_SECS: &[BorrowedFormatItem<'_>] = format_description!("[hour]:[minute]");
const FORMAT_NO_SECS: &[FormatItem<'_>] = format_description!("[hour]:[minute]");
pub(super) fn to_output<S: ScalarValue>(v: &LocalTime) -> Value<S> {
Value::scalar(
@ -117,7 +109,7 @@ mod local_time {
} else {
v.format(FORMAT)
}
.unwrap_or_else(|e| panic!("failed to format `LocalTime`: {e}")),
.unwrap_or_else(|e| panic!("Failed to format `LocalTime`: {e}")),
)
}
@ -136,34 +128,25 @@ mod local_time {
}
}
/// Combined date and time (without time zone) in `yyyy-MM-ddTHH:mm:ss` format.
///
/// [`LocalDateTime` scalar][1] compliant.
/// Combined date and time (without time zone) in `yyyy-MM-dd HH:mm:ss` format.
///
/// See also [`time::PrimitiveDateTime`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
/// [2]: https://docs.rs/time/*/time/struct.PrimitiveDateTime.html
#[graphql_scalar(
with = local_date_time,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date-time",
)]
#[graphql_scalar(with = local_date_time, parse_token(String))]
pub type LocalDateTime = time::PrimitiveDateTime;
mod local_date_time {
use super::*;
/// Format of a [`LocalDateTime` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
const FORMAT: &[BorrowedFormatItem<'_>] =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
/// Format of a [`LocalDateTime`] scalar.
const FORMAT: &[FormatItem<'_>] =
format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
pub(super) fn to_output<S: ScalarValue>(v: &LocalDateTime) -> Value<S> {
Value::scalar(
v.format(FORMAT)
.unwrap_or_else(|e| panic!("failed to format `LocalDateTime`: {e}")),
.unwrap_or_else(|e| panic!("Failed to format `LocalDateTime`: {e}")),
)
}
@ -202,7 +185,7 @@ mod date_time {
Value::scalar(
v.to_offset(UtcOffset::UTC)
.format(&Rfc3339)
.unwrap_or_else(|e| panic!("failed to format `DateTime`: {e}")),
.unwrap_or_else(|e| panic!("Failed to format `DateTime`: {e}")),
)
}
@ -219,7 +202,7 @@ mod date_time {
/// Format of a [`UtcOffset` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/utc-offset
const UTC_OFFSET_FORMAT: &[BorrowedFormatItem<'_>] =
const UTC_OFFSET_FORMAT: &[FormatItem<'_>] =
format_description!("[offset_hour sign:mandatory]:[offset_minute]");
/// Offset from UTC in `±hh:mm` format. See [list of database time zones][0].
@ -244,7 +227,7 @@ mod utc_offset {
pub(super) fn to_output<S: ScalarValue>(v: &UtcOffset) -> Value<S> {
Value::scalar(
v.format(UTC_OFFSET_FORMAT)
.unwrap_or_else(|e| panic!("failed to format `UtcOffset`: {e}")),
.unwrap_or_else(|e| panic!("Failed to format `UtcOffset`: {e}")),
)
}
@ -259,12 +242,12 @@ mod utc_offset {
}
#[cfg(test)]
mod local_date_test {
mod date_test {
use time::macros::date;
use crate::{graphql_input_value, FromInputValue as _, InputValue, ToInputValue as _};
use super::LocalDate;
use super::Date;
#[test]
fn parses_correct_input() {
@ -273,7 +256,7 @@ mod local_date_test {
("1564-01-30", date!(1564 - 01 - 30)),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = LocalDate::from_input_value(&input);
let parsed = Date::from_input_value(&input);
assert!(
parsed.is_ok(),
@ -301,7 +284,7 @@ mod local_date_test {
graphql_input_value!(false),
] {
let input: InputValue = input;
let parsed = LocalDate::from_input_value(&input);
let parsed = Date::from_input_value(&input);
assert!(parsed.is_err(), "allows input: {input:?}");
}
@ -408,8 +391,8 @@ mod local_date_time_test {
#[test]
fn parses_correct_input() {
for (raw, expected) in [
("1996-12-19T14:23:43", datetime!(1996-12-19 14:23:43)),
("1564-01-30T14:00:00", datetime!(1564-01-30 14:00)),
("1996-12-19 14:23:43", datetime!(1996-12-19 14:23:43)),
("1564-01-30 14:00:00", datetime!(1564-01-30 14:00)),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = LocalDateTime::from_input_value(&input);
@ -431,17 +414,16 @@ mod local_date_time_test {
graphql_input_value!("56:34:22"),
graphql_input_value!("56:34:22.000"),
graphql_input_value!("1996-12-1914:23:43"),
graphql_input_value!("1996-12-19 14:23:43"),
graphql_input_value!("1996-12-19Q14:23:43"),
graphql_input_value!("1996-12-19T14:23:43Z"),
graphql_input_value!("1996-12-19T14:23:43.543"),
graphql_input_value!("1996-12-19T14:23"),
graphql_input_value!("1996-12-19T14:23:1"),
graphql_input_value!("1996-12-19T14:23:"),
graphql_input_value!("1996-12-19T23:78:43"),
graphql_input_value!("1996-12-19T23:18:99"),
graphql_input_value!("1996-12-19T24:00:00"),
graphql_input_value!("1996-12-19T99:02:13"),
graphql_input_value!("1996-12-19T14:23:43"),
graphql_input_value!("1996-12-19 14:23:43Z"),
graphql_input_value!("1996-12-19 14:23:43.543"),
graphql_input_value!("1996-12-19 14:23"),
graphql_input_value!("1996-12-19 14:23:1"),
graphql_input_value!("1996-12-19 14:23:"),
graphql_input_value!("1996-12-19 23:78:43"),
graphql_input_value!("1996-12-19 23:18:99"),
graphql_input_value!("1996-12-19 24:00:00"),
graphql_input_value!("1996-12-19 99:02:13"),
graphql_input_value!("i'm not even a datetime"),
graphql_input_value!(2.32),
graphql_input_value!(1),
@ -460,11 +442,11 @@ mod local_date_time_test {
for (val, expected) in [
(
datetime!(1996-12-19 12:00 am),
graphql_input_value!("1996-12-19T00:00:00"),
graphql_input_value!("1996-12-19 00:00:00"),
),
(
datetime!(1564-01-30 14:00),
graphql_input_value!("1564-01-30T14:00:00"),
graphql_input_value!("1564-01-30 14:00:00"),
),
] {
let actual: InputValue = val.to_input_value();
@ -490,7 +472,6 @@ mod date_time_test {
datetime!(2014-11-28 21:00:09 +9),
),
("2014-11-28T21:00:09Z", datetime!(2014-11-28 21:00:09 +0)),
("2014-11-28 21:00:09z", datetime!(2014-11-28 21:00:09 +0)),
(
"2014-11-28T21:00:09+00:00",
datetime!(2014-11-28 21:00:09 +0),
@ -499,10 +480,6 @@ mod date_time_test {
"2014-11-28T21:00:09.05+09:00",
datetime!(2014-11-28 12:00:09.05 +0),
),
(
"2014-11-28 21:00:09.05+09:00",
datetime!(2014-11-28 12:00:09.05 +0),
),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = DateTime::from_input_value(&input);
@ -524,6 +501,7 @@ mod date_time_test {
graphql_input_value!("56:34:22"),
graphql_input_value!("56:34:22.000"),
graphql_input_value!("1996-12-1914:23:43"),
graphql_input_value!("1996-12-19 14:23:43Z"),
graphql_input_value!("1996-12-19T14:23:43"),
graphql_input_value!("1996-12-19T14:23:43ZZ"),
graphql_input_value!("1996-12-19T14:23:43.543"),
@ -651,7 +629,7 @@ mod integration_test {
types::scalars::{EmptyMutation, EmptySubscription},
};
use super::{DateTime, LocalDate, LocalDateTime, LocalTime, UtcOffset};
use super::{Date, DateTime, LocalDateTime, LocalTime, UtcOffset};
#[tokio::test]
async fn serializes() {
@ -659,7 +637,7 @@ mod integration_test {
#[graphql_object]
impl Root {
fn local_date() -> LocalDate {
fn date() -> Date {
date!(2015 - 03 - 14)
}
@ -681,7 +659,7 @@ mod integration_test {
}
const DOC: &str = r#"{
localDate
date
localTime
localDateTime
dateTime,
@ -698,9 +676,9 @@ mod integration_test {
execute(DOC, None, &schema, &graphql_vars! {}, &()).await,
Ok((
graphql_value!({
"localDate": "2015-03-14",
"date": "2015-03-14",
"localTime": "16:07:08",
"localDateTime": "2016-07-08T09:10:11",
"localDateTime": "2016-07-08 09:10:11",
"dateTime": "1996-12-20T00:39:57Z",
"utcOffset": "+11:30",
}),

View file

@ -1,32 +1,8 @@
//! GraphQL support for [`url`] crate types.
//!
//! # Supported types
//!
//! | Rust type | GraphQL scalar |
//! |-----------|----------------|
//! | [`Url`] | [`URL`][s1] |
//!
//! [`Url`]: url::Url
//! [s1]: https://graphql-scalars.dev/docs/scalars/url
//! GraphQL support for [url](https://github.com/servo/rust-url) types.
use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// [Standard URL][0] format as specified in [RFC 3986].
///
/// [`URL` scalar][1] compliant.
///
/// See also [`url::Url`][2] for details.
///
/// [0]: http://url.spec.whatwg.org
/// [1]: https://graphql-scalars.dev/docs/scalars/url
/// [2]: https://docs.rs/url/*/url/struct.Url.html
/// [RFC 3986]: https://datatracker.ietf.org/doc/html/rfc3986
#[graphql_scalar(
name = "URL",
with = url_scalar,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/url",
)]
#[graphql_scalar(with = url_scalar, parse_token(String))]
type Url = url::Url;
mod url_scalar {
@ -39,7 +15,7 @@ mod url_scalar {
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Url, String> {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| Url::parse(s).map_err(|e| format!("Failed to parse `URL`: {e}")))
.and_then(|s| Url::parse(s).map_err(|e| format!("Failed to parse `Url`: {e}")))
}
}

View file

@ -1,31 +1,10 @@
//! GraphQL support for [`uuid`] crate types.
//!
//! # Supported types
//!
//! | Rust type | GraphQL scalar |
//! |-----------|----------------|
//! | [`Uuid`] | [`UUID`][s1] |
//!
//! [`Uuid`]: uuid::Uuid
//! [s1]: https://graphql-scalars.dev/docs/scalars/uuid
//! GraphQL support for [uuid](https://doc.rust-lang.org/uuid/uuid/struct.Uuid.html) types.
#![allow(clippy::needless_lifetimes)]
use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// [Universally Unique Identifier][0] (UUID).
///
/// [`UUID` scalar][1] compliant.
///
/// See also [`uuid::Uuid`][2] for details.
///
/// [0]: https://en.wikipedia.org/wiki/Universally_unique_identifier
/// [1]: https://graphql-scalars.dev/docs/scalars/uuid
/// [2]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
#[graphql_scalar(
name = "UUID",
with = uuid_scalar,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/uuid",
)]
#[graphql_scalar(with = uuid_scalar, parse_token(String))]
type Uuid = uuid::Uuid;
mod uuid_scalar {
@ -38,7 +17,7 @@ mod uuid_scalar {
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Uuid, String> {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| Uuid::parse_str(s).map_err(|e| format!("Failed to parse `UUID`: {e}")))
.and_then(|s| Uuid::parse_str(s).map_err(|e| format!("Failed to parse `Uuid`: {e}")))
}
}

View file

@ -58,10 +58,7 @@ use crate::{
executor::{execute_validated_query, get_operation},
introspection::{INTROSPECTION_QUERY, INTROSPECTION_QUERY_WITHOUT_DESCRIPTIONS},
parser::parse_document_source,
validation::{
rules, validate_input_values, visit as visit_rule, visit_all_rules, MultiVisitorNil,
ValidatorContext,
},
validation::{validate_input_values, visit_all_rules, ValidatorContext},
};
pub use crate::{
@ -71,9 +68,8 @@ pub use crate::{
},
executor::{
Applies, Context, ExecutionError, ExecutionResult, Executor, FieldError, FieldResult,
FromContext, IntoFieldError, IntoResolvable, LookAheadArgument, LookAheadChildren,
LookAheadList, LookAheadObject, LookAheadSelection, LookAheadValue, OwnedExecutor,
Registry, ValuesStream, Variables,
FromContext, IntoFieldError, IntoResolvable, LookAheadArgument, LookAheadMethods,
LookAheadSelection, LookAheadValue, OwnedExecutor, Registry, ValuesStream, Variables,
},
introspection::IntrospectionFormat,
macros::helper::subscription::{ExtractTypeFromStream, IntoFieldResult},
@ -99,7 +95,7 @@ pub use crate::{
/// An error that prevented query execution
#[allow(missing_docs)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub enum GraphQLError {
ParseError(Spanning<ParseError>),
ValidationError(Vec<RuleError>),
@ -162,13 +158,6 @@ where
{
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);
if root_node.introspection_disabled {
visit_rule(
&mut MultiVisitorNil.with(rules::disable_introspection::factory()),
&mut ctx,
&document,
);
}
let errors = ctx.into_errors();
if !errors.is_empty() {
@ -212,13 +201,6 @@ where
{
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);
if root_node.introspection_disabled {
visit_rule(
&mut MultiVisitorNil.with(rules::disable_introspection::factory()),
&mut ctx,
&document,
);
}
let errors = ctx.into_errors();
if !errors.is_empty() {
@ -264,13 +246,6 @@ where
{
let mut ctx = ValidatorContext::new(&root_node.schema, &document);
visit_all_rules(&mut ctx, &document);
if root_node.introspection_disabled {
visit_rule(
&mut MultiVisitorNil.with(rules::disable_introspection::factory()),
&mut ctx,
&document,
);
}
let errors = ctx.into_errors();
if !errors.is_empty() {

View file

@ -44,11 +44,11 @@ pub trait BaseType<S> {
const NAME: Type;
}
impl<S, T: BaseType<S> + ?Sized> BaseType<S> for &T {
impl<'a, S, T: BaseType<S> + ?Sized> BaseType<S> for &'a T {
const NAME: Type = T::NAME;
}
impl<S, T> BaseType<S> for (&T::Context, T)
impl<'ctx, S, T> BaseType<S> for (&'ctx T::Context, T)
where
S: ScalarValue,
T: BaseType<S> + GraphQLValue<S>,
@ -105,11 +105,11 @@ pub trait BaseSubTypes<S> {
const NAMES: Types;
}
impl<S, T: BaseSubTypes<S> + ?Sized> BaseSubTypes<S> for &T {
impl<'a, S, T: BaseSubTypes<S> + ?Sized> BaseSubTypes<S> for &'a T {
const NAMES: Types = T::NAMES;
}
impl<S, T> BaseSubTypes<S> for (&T::Context, T)
impl<'ctx, S, T> BaseSubTypes<S> for (&'ctx T::Context, T)
where
S: ScalarValue,
T: BaseSubTypes<S> + GraphQLValue<S>,
@ -205,7 +205,7 @@ pub trait WrappedType<S> {
const VALUE: WrappedValue;
}
impl<S, T: WrappedType<S>> WrappedType<S> for (&T::Context, T)
impl<'ctx, S, T: WrappedType<S>> WrappedType<S> for (&'ctx T::Context, T)
where
S: ScalarValue,
T: GraphQLValue<S>,
@ -237,7 +237,7 @@ impl<S, T: WrappedType<S>, const N: usize> WrappedType<S> for [T; N] {
const VALUE: u128 = T::VALUE * 10 + 3;
}
impl<S, T: WrappedType<S> + ?Sized> WrappedType<S> for &T {
impl<'a, S, T: WrappedType<S> + ?Sized> WrappedType<S> for &'a T {
const VALUE: u128 = T::VALUE;
}

View file

@ -1,4 +1,9 @@
use std::{char, fmt, iter::Peekable, str::CharIndices};
use std::{
char, fmt,
iter::{Iterator, Peekable},
result::Result,
str::CharIndices,
};
use crate::parser::{SourcePosition, Spanning};
@ -46,7 +51,7 @@ pub enum Token<'a> {
}
/// Error when tokenizing the input source
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub enum LexerError {
/// An unknown character was found
///
@ -477,7 +482,7 @@ impl<'a> Iterator for Lexer<'a> {
}
}
impl fmt::Display for Token<'_> {
impl<'a> fmt::Display for Token<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Token::Name(name) => write!(f, "{name}"),

View file

@ -1,11 +1,11 @@
use std::{error::Error, fmt};
use std::{error::Error, fmt, result::Result};
use smartstring::alias::String;
use crate::parser::{Lexer, LexerError, Spanning, Token};
/// Error while parsing a GraphQL query
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub enum ParseError {
/// An unexpected token occurred in the source
// TODO: Previously was `Token<'a>`.

View file

@ -52,7 +52,7 @@ impl Query {
}
}
fn scalar_meta<T>(name: &'static str) -> MetaType<'static>
fn scalar_meta<T>(name: &'static str) -> MetaType
where
T: FromInputValue<DefaultScalarValue> + ParseScalarValue<DefaultScalarValue>,
T::Error: IntoFieldError,

View file

@ -1,7 +1,10 @@
//! Types used to describe a `GraphQL` schema
use juniper::IntoFieldError;
use std::{borrow::Cow, fmt};
use std::{
borrow::{Cow, ToOwned},
fmt,
};
use crate::{
ast::{FromInputValue, InputValue, Type},
@ -181,7 +184,7 @@ pub struct Field<'a, S> {
pub deprecation_status: DeprecationStatus,
}
impl<S> Field<'_, S> {
impl<'a, S> Field<'a, S> {
/// Returns true if the type is built-in to GraphQL.
pub fn is_builtin(&self) -> bool {
// "used exclusively by GraphQLs introspection system"
@ -202,7 +205,7 @@ pub struct Argument<'a, S> {
pub default_value: Option<InputValue<S>>,
}
impl<S> Argument<'_, S> {
impl<'a, S> Argument<'a, S> {
/// Returns true if the type is built-in to GraphQL.
pub fn is_builtin(&self) -> bool {
// "used exclusively by GraphQLs introspection system"
@ -770,7 +773,7 @@ impl EnumValue {
}
}
impl<S: fmt::Debug> fmt::Debug for ScalarMeta<'_, S> {
impl<'a, S: fmt::Debug> fmt::Debug for ScalarMeta<'a, S> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ScalarMeta")
.field("name", &self.name)
@ -779,7 +782,7 @@ impl<S: fmt::Debug> fmt::Debug for ScalarMeta<'_, S> {
}
}
impl<S: fmt::Debug> fmt::Debug for EnumMeta<'_, S> {
impl<'a, S: fmt::Debug> fmt::Debug for EnumMeta<'a, S> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("EnumMeta")
.field("name", &self.name)
@ -789,7 +792,7 @@ impl<S: fmt::Debug> fmt::Debug for EnumMeta<'_, S> {
}
}
impl<S: fmt::Debug> fmt::Debug for InputObjectMeta<'_, S> {
impl<'a, S: fmt::Debug> fmt::Debug for InputObjectMeta<'a, S> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("InputObjectMeta")
.field("name", &self.name)

View file

@ -1,7 +1,7 @@
use std::{borrow::Cow, fmt};
use fnv::FnvHashMap;
#[cfg(feature = "schema-language")]
#[cfg(feature = "graphql-parser")]
use graphql_parser::schema::Document;
use crate::{
@ -13,6 +13,9 @@ use crate::{
GraphQLEnum,
};
#[cfg(feature = "graphql-parser")]
use crate::schema::translate::{graphql_parser::GraphQLParserTranslator, SchemaTranslator};
/// Root query node of a schema
///
/// This brings the mutation, subscription and query types together,
@ -41,8 +44,6 @@ pub struct RootNode<
pub subscription_info: SubscriptionT::TypeInfo,
#[doc(hidden)]
pub schema: SchemaType<'a, S>,
#[doc(hidden)]
pub introspection_disabled: bool,
}
/// Metadata for a schema
@ -56,7 +57,7 @@ pub struct SchemaType<'a, S> {
directives: FnvHashMap<String, DirectiveType<'a, S>>,
}
impl<S> Context for SchemaType<'_, S> {}
impl<'a, S> Context for SchemaType<'a, S> {}
#[derive(Clone)]
pub enum TypeType<'a, S: 'a> {
@ -96,8 +97,8 @@ pub enum DirectiveLocation {
EnumValue,
}
impl<QueryT, MutationT, SubscriptionT>
RootNode<'_, QueryT, MutationT, SubscriptionT, DefaultScalarValue>
impl<'a, QueryT, MutationT, SubscriptionT>
RootNode<'a, QueryT, MutationT, SubscriptionT, DefaultScalarValue>
where
QueryT: GraphQLType<DefaultScalarValue, TypeInfo = ()>,
MutationT: GraphQLType<DefaultScalarValue, TypeInfo = ()>,
@ -146,7 +147,7 @@ where
mutation_info: MutationT::TypeInfo,
subscription_info: SubscriptionT::TypeInfo,
) -> Self {
Self {
RootNode {
query_type: query_obj,
mutation_type: mutation_obj,
subscription_type: subscription_obj,
@ -158,100 +159,21 @@ where
query_info,
mutation_info,
subscription_info,
introspection_disabled: false,
}
}
/// Disables introspection for this [`RootNode`], making it to return a [`FieldError`] whenever
/// its `__schema` or `__type` field is resolved.
///
/// By default, all introspection queries are allowed.
///
/// # Example
///
/// ```rust
/// # use juniper::{
/// # graphql_object, graphql_vars, EmptyMutation, EmptySubscription, GraphQLError,
/// # RootNode,
/// # };
/// #
/// pub struct Query;
///
/// #[graphql_object]
/// impl Query {
/// fn some() -> bool {
/// true
/// }
/// }
///
/// type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
///
/// let schema = Schema::new(Query, EmptyMutation::new(), EmptySubscription::new())
/// .disable_introspection();
///
/// # // language=GraphQL
/// let query = "query { __schema { queryType { name } } }";
///
/// match juniper::execute_sync(query, None, &schema, &graphql_vars! {}, &()) {
/// Err(GraphQLError::ValidationError(errs)) => {
/// assert_eq!(
/// errs.first().unwrap().message(),
/// "GraphQL introspection is not allowed, but the operation contained `__schema`",
/// );
/// }
/// res => panic!("expected `ValidationError`, returned: {res:#?}"),
/// }
/// ```
pub fn disable_introspection(mut self) -> Self {
self.introspection_disabled = true;
self
}
/// Enables introspection for this [`RootNode`], if it was previously [disabled][1].
///
/// By default, all introspection queries are allowed.
///
/// [1]: RootNode::disable_introspection
pub fn enable_introspection(mut self) -> Self {
self.introspection_disabled = false;
self
}
#[cfg(feature = "schema-language")]
/// Returns this [`RootNode`] as a [`String`] containing the schema in [SDL (schema definition language)].
///
/// # Sorted
///
/// The order of the generated definitions is stable and is sorted in the "type-then-name" manner.
///
/// If another sorting order is required, then the [`as_document()`] method should be used, which allows to sort the
/// returned [`Document`] in the desired manner and then to convert it [`to_string()`].
///
/// [`as_document()`]: RootNode::as_document
/// [`to_string()`]: ToString::to_string
/// [0]: https://graphql.org/learn/schema#type-language
#[must_use]
pub fn as_sdl(&self) -> String {
use crate::schema::translate::graphql_parser::sort_schema_document;
let mut doc = self.as_document();
sort_schema_document(&mut doc);
doc.to_string()
/// The schema definition as a `String` in the
/// [GraphQL Schema Language](https://graphql.org/learn/schema/#type-language)
/// format.
pub fn as_schema_language(&self) -> String {
self.as_parser_document().to_string()
}
#[cfg(feature = "schema-language")]
/// Returns this [`RootNode`] as a [`graphql_parser`]'s [`Document`].
///
/// # Unsorted
///
/// The order of the generated definitions in the returned [`Document`] is NOT stable and may change without any
/// real schema changes.
#[must_use]
pub fn as_document(&'a self) -> Document<'a, &'a str> {
use crate::schema::translate::{
graphql_parser::GraphQLParserTranslator, SchemaTranslator as _,
};
#[cfg(feature = "graphql-parser")]
/// The schema definition as a [`graphql_parser`](https://crates.io/crates/graphql-parser)
/// [`Document`](https://docs.rs/graphql-parser/latest/graphql_parser/schema/struct.Document.html).
pub fn as_parser_document(&'a self) -> Document<'a, &'a str> {
GraphQLParserTranslator::translate_schema(&self.schema)
}
}
@ -417,13 +339,7 @@ impl<'a, S> SchemaType<'a, S> {
/// Get a list of types.
pub fn type_list(&self) -> Vec<TypeType<S>> {
let mut types = self
.types
.values()
.map(|t| TypeType::Concrete(t))
.collect::<Vec<_>>();
sort_concrete_types(&mut types);
types
self.types.values().map(|t| TypeType::Concrete(t)).collect()
}
/// Get a list of concrete types.
@ -449,9 +365,7 @@ impl<'a, S> SchemaType<'a, S> {
/// Get a list of directives.
pub fn directive_list(&self) -> Vec<&DirectiveType<S>> {
let mut directives = self.directives.values().collect::<Vec<_>>();
sort_directives(&mut directives);
directives
self.directives.values().collect()
}
/// Get directive by name.
@ -683,7 +597,7 @@ impl fmt::Display for DirectiveLocation {
}
}
impl<S> fmt::Display for TypeType<'_, S> {
impl<'a, S> fmt::Display for TypeType<'a, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Concrete(t) => f.write_str(t.name().unwrap()),
@ -693,202 +607,120 @@ impl<S> fmt::Display for TypeType<'_, S> {
}
}
/// Sorts the provided [`TypeType`]s in the "type-then-name" manner.
fn sort_concrete_types<S>(types: &mut [TypeType<S>]) {
types.sort_by(|a, b| {
concrete_type_sort::by_type(a)
.cmp(&concrete_type_sort::by_type(b))
.then_with(|| concrete_type_sort::by_name(a).cmp(&concrete_type_sort::by_name(b)))
});
}
/// Sorts the provided [`DirectiveType`]s by name.
fn sort_directives<S>(directives: &mut [&DirectiveType<S>]) {
directives.sort_by(|a, b| a.name.cmp(&b.name));
}
/// Evaluation of a [`TypeType`] weights for sorting (for concrete types only).
///
/// Used for deterministic introspection output.
mod concrete_type_sort {
use crate::meta::MetaType;
use super::TypeType;
/// Returns a [`TypeType`] sorting weight by its type.
pub fn by_type<S>(t: &TypeType<S>) -> u8 {
match t {
TypeType::Concrete(MetaType::Enum(_)) => 0,
TypeType::Concrete(MetaType::InputObject(_)) => 1,
TypeType::Concrete(MetaType::Interface(_)) => 2,
TypeType::Concrete(MetaType::Scalar(_)) => 3,
TypeType::Concrete(MetaType::Object(_)) => 4,
TypeType::Concrete(MetaType::Union(_)) => 5,
// NOTE: The following types are not part of the introspected types.
TypeType::Concrete(
MetaType::List(_) | MetaType::Nullable(_) | MetaType::Placeholder(_),
) => 6,
// NOTE: Other variants will not appear since we're only sorting concrete types.
TypeType::List(..) | TypeType::NonNull(_) => 7,
}
}
/// Returns a [`TypeType`] sorting weight by its name.
pub fn by_name<'a, S>(t: &'a TypeType<'a, S>) -> Option<&'a str> {
match t {
TypeType::Concrete(MetaType::Enum(meta)) => Some(&meta.name),
TypeType::Concrete(MetaType::InputObject(meta)) => Some(&meta.name),
TypeType::Concrete(MetaType::Interface(meta)) => Some(&meta.name),
TypeType::Concrete(MetaType::Scalar(meta)) => Some(&meta.name),
TypeType::Concrete(MetaType::Object(meta)) => Some(&meta.name),
TypeType::Concrete(MetaType::Union(meta)) => Some(&meta.name),
TypeType::Concrete(
// NOTE: The following types are not part of the introspected types.
MetaType::List(_) | MetaType::Nullable(_) | MetaType::Placeholder(_),
)
// NOTE: Other variants will not appear since we're only sorting concrete types.
| TypeType::List(..)
| TypeType::NonNull(_) => None,
}
}
}
#[cfg(test)]
mod root_node_test {
#[cfg(feature = "schema-language")]
mod as_document {
mod test {
#[cfg(feature = "graphql-parser")]
mod graphql_parser_integration {
use crate::{graphql_object, EmptyMutation, EmptySubscription, RootNode};
struct Query;
#[graphql_object]
impl Query {
fn blah() -> bool {
true
}
}
#[test]
fn generates_correct_document() {
fn graphql_parser_doc() {
struct Query;
#[graphql_object]
impl Query {
fn blah() -> bool {
true
}
}
let schema = RootNode::new(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
let ast = graphql_parser::parse_schema::<&str>(
//language=GraphQL
r#"
type Query {
blah: Boolean!
blah: Boolean!
}
schema {
query: Query
}
"#,
"#,
)
.unwrap();
assert_eq!(ast.to_string(), schema.as_document().to_string());
assert_eq!(ast.to_string(), schema.as_parser_document().to_string());
}
}
#[cfg(feature = "schema-language")]
mod as_sdl {
mod schema_language {
use crate::{
graphql_object, EmptyMutation, EmptySubscription, GraphQLEnum, GraphQLInputObject,
GraphQLObject, GraphQLUnion, RootNode,
};
#[derive(GraphQLObject, Default)]
struct Cake {
fresh: bool,
}
#[derive(GraphQLObject, Default)]
struct IceCream {
cold: bool,
}
#[derive(GraphQLUnion)]
enum GlutenFree {
Cake(Cake),
IceCream(IceCream),
}
#[derive(GraphQLEnum)]
enum Fruit {
Apple,
Orange,
}
#[derive(GraphQLInputObject)]
struct Coordinate {
latitude: f64,
longitude: f64,
}
struct Query;
#[graphql_object]
impl Query {
fn blah() -> bool {
true
#[test]
fn schema_language() {
#[derive(GraphQLObject, Default)]
struct Cake {
fresh: bool,
}
/// This is whatever's description.
fn whatever() -> String {
"foo".into()
#[derive(GraphQLObject, Default)]
struct IceCream {
cold: bool,
}
fn arr(stuff: Vec<Coordinate>) -> Option<&'static str> {
(!stuff.is_empty()).then_some("stuff")
#[derive(GraphQLUnion)]
enum GlutenFree {
Cake(Cake),
IceCream(IceCream),
}
fn fruit() -> Fruit {
Fruit::Apple
#[derive(GraphQLEnum)]
enum Fruit {
Apple,
Orange,
}
fn gluten_free(flavor: String) -> GlutenFree {
if flavor == "savory" {
GlutenFree::Cake(Cake::default())
} else {
GlutenFree::IceCream(IceCream::default())
#[derive(GraphQLInputObject)]
struct Coordinate {
latitude: f64,
longitude: f64,
}
struct Query;
#[graphql_object]
impl Query {
fn blah() -> bool {
true
}
/// This is whatever's description.
fn whatever() -> String {
"foo".into()
}
fn arr(stuff: Vec<Coordinate>) -> Option<&'static str> {
(!stuff.is_empty()).then_some("stuff")
}
fn fruit() -> Fruit {
Fruit::Apple
}
fn gluten_free(flavor: String) -> GlutenFree {
if flavor == "savory" {
GlutenFree::Cake(Cake::default())
} else {
GlutenFree::IceCream(IceCream::default())
}
}
#[deprecated]
fn old() -> i32 {
42
}
#[deprecated(note = "This field is deprecated, use another.")]
fn really_old() -> f64 {
42.0
}
}
#[deprecated]
fn old() -> i32 {
42
}
#[deprecated(note = "This field is deprecated, use another.")]
fn really_old() -> f64 {
42.0
}
}
#[test]
fn generates_correct_sdl() {
let actual = RootNode::new(
let schema = RootNode::new(
Query,
EmptyMutation::<()>::new(),
EmptySubscription::<()>::new(),
);
let expected = graphql_parser::parse_schema::<&str>(
//language=GraphQL
let ast = graphql_parser::parse_schema::<&str>(
r#"
schema {
query: Query
}
union GlutenFree = Cake | IceCream
enum Fruit {
APPLE
ORANGE
}
input Coordinate {
latitude: Float!
longitude: Float!
}
type Cake {
fresh: Boolean!
}
@ -905,12 +737,17 @@ mod root_node_test {
old: Int! @deprecated
reallyOld: Float! @deprecated(reason: "This field is deprecated, use another.")
}
union GlutenFree = Cake | IceCream
"#,
input Coordinate {
latitude: Float!
longitude: Float!
}
schema {
query: Query
}
"#,
)
.unwrap();
assert_eq!(actual.as_sdl(), expected.to_string());
assert_eq!(ast.to_string(), schema.as_schema_language());
}
}
}

View file

@ -17,8 +17,8 @@ use crate::schema::{
model::{DirectiveLocation, DirectiveType, RootNode, SchemaType, TypeType},
};
impl<S, QueryT, MutationT, SubscriptionT> GraphQLType<S>
for RootNode<'_, QueryT, MutationT, SubscriptionT, S>
impl<'a, S, QueryT, MutationT, SubscriptionT> GraphQLType<S>
for RootNode<'a, QueryT, MutationT, SubscriptionT, S>
where
S: ScalarValue,
QueryT: GraphQLType<S>,
@ -37,8 +37,8 @@ where
}
}
impl<S, QueryT, MutationT, SubscriptionT> GraphQLValue<S>
for RootNode<'_, QueryT, MutationT, SubscriptionT, S>
impl<'a, S, QueryT, MutationT, SubscriptionT> GraphQLValue<S>
for RootNode<'a, QueryT, MutationT, SubscriptionT, S>
where
S: ScalarValue,
QueryT: GraphQLType<S>,
@ -282,11 +282,11 @@ impl<'a, S: ScalarValue + 'a> TypeType<'a, S> {
TypeType::Concrete(&MetaType::Interface(InterfaceMeta {
name: ref iface_name,
..
})) => {
let mut type_names = context
.types
.values()
.filter_map(|ct| {
})) => Some(
context
.concrete_type_list()
.iter()
.filter_map(|&ct| {
if let MetaType::Object(ObjectMeta {
name,
interface_names,
@ -295,21 +295,15 @@ impl<'a, S: ScalarValue + 'a> TypeType<'a, S> {
{
interface_names
.iter()
.any(|iname| iname == iface_name)
.then(|| name.as_ref())
.any(|name| name == iface_name)
.then(|| context.type_by_name(name))
.flatten()
} else {
None
}
})
.collect::<Vec<_>>();
type_names.sort();
Some(
type_names
.into_iter()
.filter_map(|n| context.type_by_name(n))
.collect(),
)
}
.collect(),
),
_ => None,
}
}

View file

@ -40,9 +40,9 @@ impl<'a, T> SchemaTranslator<'a, graphql_parser::schema::Document<'a, T>>
where
T: Text<'a> + Default,
{
fn translate_schema<S>(input: &'a SchemaType<S>) -> graphql_parser::schema::Document<'a, T>
fn translate_schema<S: 'a>(input: &'a SchemaType<S>) -> graphql_parser::schema::Document<'a, T>
where
S: ScalarValue + 'a,
S: ScalarValue,
{
let mut doc = Document::default();
@ -94,9 +94,9 @@ impl GraphQLParserTranslator {
}
}
fn translate_value<'a, S, T>(input: &'a InputValue<S>) -> ExternalValue<'a, T>
fn translate_value<'a, S: 'a, T>(input: &'a InputValue<S>) -> ExternalValue<'a, T>
where
S: ScalarValue + 'a,
S: ScalarValue,
T: Text<'a>,
{
match input {
@ -250,9 +250,9 @@ impl GraphQLParserTranslator {
}
}
fn translate_field<'a, S, T>(input: &'a Field<S>) -> ExternalField<'a, T>
fn translate_field<'a, S: 'a, T>(input: &'a Field<S>) -> ExternalField<'a, T>
where
S: ScalarValue + 'a,
S: ScalarValue,
T: Text<'a>,
{
let arguments = input
@ -307,113 +307,3 @@ where
vec![]
}
}
/// Sorts the provided [`Document`] in the "type-then-name" manner.
pub(crate) fn sort_schema_document<'a, T: Text<'a>>(document: &mut Document<'a, T>) {
document.definitions.sort_by(move |a, b| {
let type_cmp = sort_value::by_type(a).cmp(&sort_value::by_type(b));
let name_cmp = sort_value::by_is_directive(a)
.cmp(&sort_value::by_is_directive(b))
.then(sort_value::by_name(a).cmp(&sort_value::by_name(b)))
.then(sort_value::by_directive(a).cmp(&sort_value::by_directive(b)));
type_cmp.then(name_cmp)
})
}
/// Evaluation of a [`Definition`] weights for sorting.
mod sort_value {
use graphql_parser::schema::{Definition, Text, TypeDefinition, TypeExtension};
/// Returns a [`Definition`] sorting weight by its type.
pub(super) fn by_type<'a, T>(definition: &Definition<'a, T>) -> u8
where
T: Text<'a>,
{
match definition {
Definition::SchemaDefinition(_) => 0,
Definition::DirectiveDefinition(_) => 1,
Definition::TypeDefinition(t) => match t {
TypeDefinition::Enum(_) => 2,
TypeDefinition::InputObject(_) => 4,
TypeDefinition::Interface(_) => 6,
TypeDefinition::Scalar(_) => 8,
TypeDefinition::Object(_) => 10,
TypeDefinition::Union(_) => 12,
},
Definition::TypeExtension(e) => match e {
TypeExtension::Enum(_) => 3,
TypeExtension::InputObject(_) => 5,
TypeExtension::Interface(_) => 7,
TypeExtension::Scalar(_) => 9,
TypeExtension::Object(_) => 11,
TypeExtension::Union(_) => 13,
},
}
}
/// Returns a [`Definition`] sorting weight by its name.
pub(super) fn by_name<'b, 'a, T>(definition: &'b Definition<'a, T>) -> Option<&'b T::Value>
where
T: Text<'a>,
{
match definition {
Definition::SchemaDefinition(_) => None,
Definition::DirectiveDefinition(d) => Some(&d.name),
Definition::TypeDefinition(t) => match t {
TypeDefinition::Enum(d) => Some(&d.name),
TypeDefinition::InputObject(d) => Some(&d.name),
TypeDefinition::Interface(d) => Some(&d.name),
TypeDefinition::Scalar(d) => Some(&d.name),
TypeDefinition::Object(d) => Some(&d.name),
TypeDefinition::Union(d) => Some(&d.name),
},
Definition::TypeExtension(e) => match e {
TypeExtension::Enum(d) => Some(&d.name),
TypeExtension::InputObject(d) => Some(&d.name),
TypeExtension::Interface(d) => Some(&d.name),
TypeExtension::Scalar(d) => Some(&d.name),
TypeExtension::Object(d) => Some(&d.name),
TypeExtension::Union(d) => Some(&d.name),
},
}
}
/// Returns a [`Definition`] sorting weight by its directive.
pub(super) fn by_directive<'b, 'a, T>(definition: &'b Definition<'a, T>) -> Option<&'b T::Value>
where
T: Text<'a>,
{
match definition {
Definition::SchemaDefinition(_) => None,
Definition::DirectiveDefinition(_) => None,
Definition::TypeDefinition(t) => match t {
TypeDefinition::Enum(d) => d.directives.first().map(|d| &d.name),
TypeDefinition::InputObject(d) => d.directives.first().map(|d| &d.name),
TypeDefinition::Interface(d) => d.directives.first().map(|d| &d.name),
TypeDefinition::Scalar(d) => d.directives.first().map(|d| &d.name),
TypeDefinition::Object(d) => d.directives.first().map(|d| &d.name),
TypeDefinition::Union(d) => d.directives.first().map(|d| &d.name),
},
Definition::TypeExtension(e) => match e {
TypeExtension::Enum(d) => d.directives.first().map(|d| &d.name),
TypeExtension::InputObject(d) => d.directives.first().map(|d| &d.name),
TypeExtension::Interface(d) => d.directives.first().map(|d| &d.name),
TypeExtension::Scalar(d) => d.directives.first().map(|d| &d.name),
TypeExtension::Object(d) => d.directives.first().map(|d| &d.name),
TypeExtension::Union(d) => d.directives.first().map(|d| &d.name),
},
}
}
/// Returns a [`Definition`] sorting weight by whether it represents a directive.
pub(super) fn by_is_directive<'a, T>(definition: &Definition<'a, T>) -> u8
where
T: Text<'a>,
{
match definition {
Definition::SchemaDefinition(_) => 0,
Definition::DirectiveDefinition(_) => 1,
_ => 2,
}
}
}

View file

@ -1,9 +1,8 @@
use crate::{ScalarValue, SchemaType};
#[cfg_attr(not(feature = "schema-language"), allow(dead_code))]
pub trait SchemaTranslator<'a, T> {
fn translate_schema<S: 'a + ScalarValue>(s: &'a SchemaType<S>) -> T;
}
#[cfg(feature = "schema-language")]
#[cfg(feature = "graphql-parser")]
pub mod graphql_parser;

View file

@ -1,6 +1,6 @@
#![allow(missing_docs)]
/// The schema as a static/hardcoded GraphQL SDL (schema definition language).
/// The schema as a static/hardcoded GraphQL Schema Language.
pub const STATIC_GRAPHQL_SCHEMA_DEFINITION: &str = include_str!("starwars.graphql");
#[cfg(test)]
@ -24,7 +24,7 @@ mod tests {
EmptySubscription::<Database>::new(),
);
//dbg!("{}", schema.as_sdl());
dbg!("{}", schema.as_schema_language());
// `include_str()` keeps line endings. `git` will sadly by default
// convert them, making this test fail without runtime tweaks on
@ -32,10 +32,11 @@ mod tests {
//
// See https://github.com/rust-lang/rust/pull/63681.
#[cfg(windows)]
let expected = STATIC_GRAPHQL_SCHEMA_DEFINITION.replace("\r\n", "\n");
let expected = &STATIC_GRAPHQL_SCHEMA_DEFINITION.replace("\r\n", "\n");
#[cfg(not(windows))]
let expected = STATIC_GRAPHQL_SCHEMA_DEFINITION;
assert_eq!(schema.as_sdl(), expected);
assert_eq!(expected, &schema.as_schema_language());
}
}

View file

@ -1,5 +1,22 @@
schema {
query: Query
"A mechanical creature in the Star Wars universe."
type Droid implements Character {
"The id of the droid"
id: String!
"The name of the droid"
name: String
"The friends of the droid"
friends: [Character!]!
"Which movies they appear in"
appearsIn: [Episode!]!
"The primary function of the droid"
primaryFunction: String
}
"The root query object of the schema"
type Query {
human("id of the human" id: String!): Human
droid("id of the droid" id: String!): Droid
hero("If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode" episode: Episode): Character
}
enum Episode {
@ -20,20 +37,6 @@ interface Character {
appearsIn: [Episode!]!
}
"A mechanical creature in the Star Wars universe."
type Droid implements Character {
"The id of the droid"
id: String!
"The name of the droid"
name: String
"The friends of the droid"
friends: [Character!]!
"Which movies they appear in"
appearsIn: [Episode!]!
"The primary function of the droid"
primaryFunction: String
}
"A humanoid creature in the Star Wars universe."
type Human implements Character {
"The id of the human"
@ -48,9 +51,6 @@ type Human implements Character {
homePlanet: String
}
"The root query object of the schema"
type Query {
human("id of the human" id: String!): Human
droid("id of the droid" id: String!): Droid
hero("If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode" episode: Episode): Character
schema {
query: Query
}

View file

@ -1,7 +1,5 @@
use std::collections::HashSet;
use pretty_assertions::assert_eq;
use crate::{
graphql_vars,
introspection::IntrospectionFormat,
@ -186,20 +184,14 @@ async fn test_introspection_directives() {
EmptySubscription::<Database>::new(),
);
let result = crate::execute(q, None, &schema, &graphql_vars! {}, &database)
let mut result = crate::execute(q, None, &schema, &graphql_vars! {}, &database)
.await
.unwrap();
sort_schema_value(&mut result.0);
let expected = graphql_value!({
let mut expected = graphql_value!({
"__schema": {
"directives": [
{
"name": "deprecated",
"locations": [
"FIELD_DEFINITION",
"ENUM_VALUE",
],
},
{
"name": "include",
"locations": [
@ -216,6 +208,13 @@ async fn test_introspection_directives() {
"INLINE_FRAGMENT",
],
},
{
"name": "deprecated",
"locations": [
"FIELD_DEFINITION",
"ENUM_VALUE",
],
},
{
"name": "specifiedBy",
"locations": [
@ -225,6 +224,7 @@ async fn test_introspection_directives() {
],
},
});
sort_schema_value(&mut expected);
assert_eq!(result, (expected, vec![]));
}
@ -286,9 +286,9 @@ async fn test_builtin_introspection_query() {
EmptyMutation::<Database>::new(),
EmptySubscription::<Database>::new(),
);
let result = crate::introspect(&schema, &database, IntrospectionFormat::default()).unwrap();
let mut result = crate::introspect(&schema, &database, IntrospectionFormat::default()).unwrap();
sort_schema_value(&mut result.0);
let expected = schema_introspection_result();
assert_eq!(result, (expected, vec![]));
}
@ -301,8 +301,9 @@ async fn test_builtin_introspection_query_without_descriptions() {
EmptySubscription::<Database>::new(),
);
let result =
let mut result =
crate::introspect(&schema, &database, IntrospectionFormat::WithoutDescriptions).unwrap();
sort_schema_value(&mut result.0);
let expected = schema_introspection_result_without_descriptions();
assert_eq!(result, (expected, vec![]));

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,5 @@
use std::future;
use auto_enums::enum_derive;
use crate::{
ast::Selection,
executor::{ExecutionResult, Executor},
@ -183,7 +181,7 @@ where
{
use futures::stream::{FuturesOrdered, StreamExt as _};
#[enum_derive(Future)]
#[derive(futures_enum::Future)]
enum AsyncValueFuture<A, B, C, D> {
Field(A),
FragmentSpread(B),

View file

@ -277,7 +277,7 @@ where
}
}
impl<T, S> ToInputValue<S> for &[T]
impl<'a, T, S> ToInputValue<S> for &'a [T]
where
T: ToInputValue<S>,
S: ScalarValue,

View file

@ -110,7 +110,7 @@ where
}
}
impl<S, T> GraphQLType<S> for &T
impl<'e, S, T> GraphQLType<S> for &'e T
where
T: GraphQLType<S> + ?Sized,
S: ScalarValue,
@ -127,7 +127,7 @@ where
}
}
impl<S, T> GraphQLValue<S> for &T
impl<'e, S, T> GraphQLValue<S> for &'e T
where
S: ScalarValue,
T: GraphQLValue<S> + ?Sized,
@ -169,7 +169,7 @@ where
}
}
impl<S, T> GraphQLValueAsync<S> for &T
impl<'e, S, T> GraphQLValueAsync<S> for &'e T
where
T: GraphQLValueAsync<S> + ?Sized,
T::TypeInfo: Sync,
@ -196,7 +196,7 @@ where
}
}
impl<T, S> ToInputValue<S> for &T
impl<'a, T, S> ToInputValue<S> for &'a T
where
S: fmt::Debug,
T: ToInputValue<S>,

View file

@ -1,4 +1,6 @@
use std::{char, fmt, marker::PhantomData, ops::Deref, rc::Rc, thread::JoinHandle};
use std::{
char, convert::From, fmt, marker::PhantomData, ops::Deref, rc::Rc, thread::JoinHandle, u32,
};
use serde::{Deserialize, Serialize};
@ -250,7 +252,7 @@ where
}
}
impl<S> ToInputValue<S> for &str
impl<'a, S> ToInputValue<S> for &'a str
where
S: ScalarValue,
{

View file

@ -1,126 +1,48 @@
use std::collections::HashSet;
use crate::{
ast::InputValue,
schema::{
meta::{Argument, EnumMeta, InputObjectMeta, MetaType},
meta::{EnumMeta, InputObjectMeta, MetaType},
model::{SchemaType, TypeType},
},
value::ScalarValue,
};
use std::collections::HashSet;
/// Common error messages used in validation and execution of GraphQL operations
pub(crate) mod error {
use std::fmt::Display;
pub(crate) fn non_null(arg_type: impl Display) -> String {
format!("\"null\" specified for not nullable type \"{arg_type}\"")
}
pub(crate) fn enum_value(arg_value: impl Display, arg_type: impl Display) -> String {
format!("Invalid value \"{arg_value}\" for enum \"{arg_type}\"")
}
pub(crate) fn type_value(arg_value: impl Display, arg_type: impl Display) -> String {
format!("Invalid value \"{arg_value}\" for type \"{arg_type}\"")
}
pub(crate) fn parser(arg_type: impl Display, msg: impl Display) -> String {
format!("Parser error for \"{arg_type}\": {msg}")
}
pub(crate) fn not_input_object(arg_type: impl Display) -> String {
format!("\"{arg_type}\" is not an input object")
}
pub(crate) fn field(
arg_type: impl Display,
field_name: impl Display,
error_message: impl Display,
) -> String {
format!("Error on \"{arg_type}\" field \"{field_name}\": {error_message}")
}
pub(crate) fn missing_fields(arg_type: impl Display, missing_fields: impl Display) -> String {
format!("\"{arg_type}\" is missing fields: {missing_fields}")
}
pub(crate) fn unknown_field(arg_type: impl Display, field_name: impl Display) -> String {
format!("Field \"{field_name}\" does not exist on type \"{arg_type}\"")
}
pub(crate) fn invalid_list_length(
arg_value: impl Display,
actual: usize,
expected: usize,
) -> String {
format!("Expected list of length {expected}, but \"{arg_value}\" has length {actual}")
}
}
/// Validates the specified field of a GraphQL object and returns an error message if the field is
/// invalid.
fn validate_object_field<S>(
schema: &SchemaType<S>,
object_type: &TypeType<S>,
object_fields: &[Argument<S>],
field_value: &InputValue<S>,
field_key: &str,
) -> Option<String>
where
S: ScalarValue,
{
let field_type = object_fields
.iter()
.filter(|f| f.name == field_key)
.map(|f| schema.make_type(&f.arg_type))
.next();
if let Some(field_arg_type) = field_type {
let error_message = validate_literal_value(schema, &field_arg_type, field_value);
error_message.map(|m| error::field(object_type, field_key, m))
} else {
Some(error::unknown_field(object_type, field_key))
}
}
/// Validates the specified GraphQL literal and returns an error message if the it's invalid.
pub fn validate_literal_value<S>(
pub fn is_valid_literal_value<S>(
schema: &SchemaType<S>,
arg_type: &TypeType<S>,
arg_value: &InputValue<S>,
) -> Option<String>
) -> bool
where
S: ScalarValue,
{
match *arg_type {
TypeType::NonNull(ref inner) => {
if arg_value.is_null() {
Some(error::non_null(arg_type))
false
} else {
validate_literal_value(schema, inner, arg_value)
is_valid_literal_value(schema, inner, arg_value)
}
}
TypeType::List(ref inner, expected_size) => match *arg_value {
InputValue::Null | InputValue::Variable(_) => None,
InputValue::Null | InputValue::Variable(_) => true,
InputValue::List(ref items) => {
if let Some(expected) = expected_size {
if items.len() != expected {
return Some(error::invalid_list_length(arg_value, items.len(), expected));
return false;
}
}
items
.iter()
.find_map(|i| validate_literal_value(schema, inner, &i.item))
.all(|i| is_valid_literal_value(schema, inner, &i.item))
}
ref v => {
if let Some(expected) = expected_size {
if expected != 1 {
return Some(error::invalid_list_length(arg_value, 1, expected));
return false;
}
}
validate_literal_value(schema, inner, v)
is_valid_literal_value(schema, inner, v)
}
},
TypeType::Concrete(t) => {
@ -129,23 +51,19 @@ where
if let (&InputValue::Scalar(_), Some(&MetaType::Enum(EnumMeta { .. }))) =
(arg_value, arg_type.to_concrete())
{
return Some(error::enum_value(arg_value, arg_type));
return false;
}
match *arg_value {
InputValue::Null | InputValue::Variable(_) => None,
InputValue::Null | InputValue::Variable(_) => true,
ref v @ InputValue::Scalar(_) | ref v @ InputValue::Enum(_) => {
if let Some(parse_fn) = t.input_value_parse_fn() {
if parse_fn(v).is_ok() {
None
} else {
Some(error::type_value(arg_value, arg_type))
}
parse_fn(v).is_ok()
} else {
Some(error::parser(arg_type, "no parser present"))
false
}
}
InputValue::List(_) => Some("Input lists are not literals".to_owned()),
InputValue::List(_) => false,
InputValue::Object(ref obj) => {
if let MetaType::InputObject(InputObjectMeta {
ref input_fields, ..
@ -159,33 +77,23 @@ where
})
.collect::<HashSet<_>>();
let error_message = obj.iter().find_map(|(key, value)| {
let all_types_ok = obj.iter().all(|(key, value)| {
remaining_required_fields.remove(&key.item);
validate_object_field(
schema,
arg_type,
input_fields,
&value.item,
&key.item,
)
if let Some(ref arg_type) = input_fields
.iter()
.filter(|f| f.name == key.item)
.map(|f| schema.make_type(&f.arg_type))
.next()
{
is_valid_literal_value(schema, arg_type, &value.item)
} else {
false
}
});
if error_message.is_some() {
return error_message;
}
if remaining_required_fields.is_empty() {
None
} else {
let missing_fields = remaining_required_fields
.into_iter()
.map(|s| format!("\"{}\"", &**s))
.collect::<Vec<_>>()
.join(", ");
Some(error::missing_fields(arg_type, missing_fields))
}
all_types_ok && remaining_required_fields.is_empty()
} else {
Some(error::not_input_object(arg_type))
false
}
}
}

View file

@ -10,7 +10,7 @@ use crate::schema::{meta::MetaType, model::SchemaType};
use crate::parser::SourcePosition;
/// Query validation error
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct RuleError {
locations: Vec<SourcePosition>,
message: String,

View file

@ -360,7 +360,7 @@ fn unification_error(
)
}
impl fmt::Display for Path<'_> {
impl<'a> fmt::Display for Path<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Path::Root => write!(f, ""),

View file

@ -3,7 +3,7 @@
mod context;
mod input_value;
mod multi_visitor;
pub mod rules;
mod rules;
mod traits;
mod visitor;
@ -20,7 +20,8 @@ pub use self::{
};
#[cfg(test)]
pub(crate) use self::test_harness::{
expect_fails_fn, expect_fails_rule, expect_fails_rule_with_schema, expect_passes_rule,
pub use self::test_harness::{
expect_fails_fn, expect_fails_fn_with_schema, expect_fails_rule, expect_fails_rule_with_schema,
expect_passes_fn, expect_passes_fn_with_schema, expect_passes_rule,
expect_passes_rule_with_schema,
};

View file

@ -6,7 +6,6 @@ use crate::{
parser::Spanning,
validation::{ValidatorContext, Visitor},
value::ScalarValue,
Span,
};
#[doc(hidden)]
@ -28,7 +27,7 @@ impl<A, B> MultiVisitorCons<A, B> {
}
}
impl<S> Visitor<'_, S> for MultiVisitorNil where S: ScalarValue {}
impl<'a, S> Visitor<'a, S> for MultiVisitorNil where S: ScalarValue {}
impl<'a, A, B, S> Visitor<'a, S> for MultiVisitorCons<A, B>
where
@ -178,46 +177,38 @@ where
self.1.exit_inline_fragment(ctx, f);
}
fn enter_null_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: SpannedInput<'a, ()>) {
fn enter_null_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: Spanning<()>) {
self.0.enter_null_value(ctx, n);
self.1.enter_null_value(ctx, n);
}
fn exit_null_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: SpannedInput<'a, ()>) {
fn exit_null_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: Spanning<()>) {
self.0.exit_null_value(ctx, n);
self.1.exit_null_value(ctx, n);
}
fn enter_scalar_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: SpannedInput<'a, S>) {
fn enter_scalar_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: Spanning<&'a S>) {
self.0.enter_scalar_value(ctx, n);
self.1.enter_scalar_value(ctx, n);
}
fn exit_scalar_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: SpannedInput<'a, S>) {
fn exit_scalar_value(&mut self, ctx: &mut ValidatorContext<'a, S>, n: Spanning<&'a S>) {
self.0.exit_scalar_value(ctx, n);
self.1.exit_scalar_value(ctx, n);
}
fn enter_enum_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: SpannedInput<'a, String>) {
fn enter_enum_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: Spanning<&'a String>) {
self.0.enter_enum_value(ctx, s);
self.1.enter_enum_value(ctx, s);
}
fn exit_enum_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: SpannedInput<'a, String>) {
fn exit_enum_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: Spanning<&'a String>) {
self.0.exit_enum_value(ctx, s);
self.1.exit_enum_value(ctx, s);
}
fn enter_variable_value(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
s: SpannedInput<'a, String>,
) {
fn enter_variable_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: Spanning<&'a String>) {
self.0.enter_variable_value(ctx, s);
self.1.enter_variable_value(ctx, s);
}
fn exit_variable_value(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
s: SpannedInput<'a, String>,
) {
fn exit_variable_value(&mut self, ctx: &mut ValidatorContext<'a, S>, s: Spanning<&'a String>) {
self.0.exit_variable_value(ctx, s);
self.1.exit_variable_value(ctx, s);
}
@ -225,7 +216,7 @@ where
fn enter_list_value(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
l: SpannedInput<'a, Vec<Spanning<InputValue<S>>>>,
l: Spanning<&'a Vec<Spanning<InputValue<S>>>>,
) {
self.0.enter_list_value(ctx, l);
self.1.enter_list_value(ctx, l);
@ -233,7 +224,7 @@ where
fn exit_list_value(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
l: SpannedInput<'a, Vec<Spanning<InputValue<S>>>>,
l: Spanning<&'a Vec<Spanning<InputValue<S>>>>,
) {
self.0.exit_list_value(ctx, l);
self.1.exit_list_value(ctx, l);
@ -251,7 +242,7 @@ where
fn enter_object_field(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
f: (SpannedInput<'a, String>, SpannedInput<'a, InputValue<S>>),
f: &'a (Spanning<String>, Spanning<InputValue<S>>),
) {
self.0.enter_object_field(ctx, f);
self.1.enter_object_field(ctx, f);
@ -259,12 +250,11 @@ where
fn exit_object_field(
&mut self,
ctx: &mut ValidatorContext<'a, S>,
f: (SpannedInput<'a, String>, SpannedInput<'a, InputValue<S>>),
f: &'a (Spanning<String>, Spanning<InputValue<S>>),
) {
self.0.exit_object_field(ctx, f);
self.1.exit_object_field(ctx, f);
}
}
type SpannedInput<'a, T> = Spanning<&'a T, &'a Span>;
type SpannedObject<'a, S> = SpannedInput<'a, Vec<(Spanning<String>, Spanning<InputValue<S>>)>>;
type SpannedObject<'a, S> = Spanning<&'a Vec<(Spanning<String>, Spanning<InputValue<S>>)>>;

View file

@ -4,7 +4,7 @@ use crate::{
ast::{Directive, Field, InputValue},
parser::Spanning,
schema::meta::Argument,
types::utilities::validate_literal_value,
types::utilities::is_valid_literal_value,
validation::{ValidatorContext, Visitor},
value::ScalarValue,
};
@ -58,15 +58,18 @@ where
{
let meta_type = ctx.schema.make_type(&argument_meta.arg_type);
if let Some(err) = validate_literal_value(ctx.schema, &meta_type, &arg_value.item) {
ctx.report_error(&error_message(arg_name.item, err), &[arg_value.span.start]);
if !is_valid_literal_value(ctx.schema, &meta_type, &arg_value.item) {
ctx.report_error(
&error_message(arg_name.item, &argument_meta.arg_type),
&[arg_value.span.start],
);
}
}
}
}
fn error_message(arg_name: impl fmt::Display, msg: impl fmt::Display) -> String {
format!("Invalid value for argument \"{arg_name}\", reason: {msg}")
fn error_message(arg_name: impl fmt::Display, type_name: impl fmt::Display) -> String {
format!("Invalid value for argument \"{arg_name}\", expected type \"{type_name}\"",)
}
#[cfg(test)]
@ -75,7 +78,6 @@ mod tests {
use crate::{
parser::SourcePosition,
types::utilities::error,
validation::{expect_fails_rule, expect_passes_rule, RuleError},
value::DefaultScalarValue,
};
@ -90,7 +92,7 @@ mod tests {
intArgField(intArg: null)
}
}
"#,
"#,
);
}
@ -104,7 +106,7 @@ mod tests {
stringListArgField(stringListArg: null)
}
}
"#,
"#,
);
}
@ -118,9 +120,9 @@ mod tests {
nonNullIntArgField(nonNullIntArg: null)
}
}
"#,
"#,
&[RuleError::new(
&error_message("nonNullIntArg", error::non_null("Int!")),
&error_message("nonNullIntArg", "Int!"),
&[SourcePosition::new(97, 3, 50)],
)],
);
@ -136,9 +138,9 @@ mod tests {
nonNullStringListArgField(nonNullStringListArg: null)
}
}
"#,
"#,
&[RuleError::new(
&error_message("nonNullStringListArg", error::non_null("[String!]!")),
&error_message("nonNullStringListArg", "[String!]!"),
&[SourcePosition::new(111, 3, 64)],
)],
);
@ -154,7 +156,7 @@ mod tests {
intArgField(intArg: 2)
}
}
"#,
"#,
);
}
@ -168,7 +170,7 @@ mod tests {
booleanArgField(booleanArg: true)
}
}
"#,
"#,
);
}
@ -182,7 +184,7 @@ mod tests {
stringArgField(stringArg: "foo")
}
}
"#,
"#,
);
}
@ -196,7 +198,7 @@ mod tests {
floatArgField(floatArg: 1.1)
}
}
"#,
"#,
);
}
@ -210,7 +212,7 @@ mod tests {
floatArgField(floatArg: 1)
}
}
"#,
"#,
);
}
@ -224,7 +226,7 @@ mod tests {
idArgField(idArg: 1)
}
}
"#,
"#,
);
}
@ -238,7 +240,7 @@ mod tests {
idArgField(idArg: "someIdString")
}
}
"#,
"#,
);
}
@ -252,7 +254,7 @@ mod tests {
doesKnowCommand(dogCommand: SIT)
}
}
"#,
"#,
);
}
@ -266,9 +268,9 @@ mod tests {
stringArgField(stringArg: 1)
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringArg", error::type_value("1", "String")),
&error_message("stringArg", "String"),
&[SourcePosition::new(89, 3, 42)],
)],
);
@ -284,9 +286,9 @@ mod tests {
stringArgField(stringArg: 1.0)
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringArg", error::type_value("1", "String")),
&error_message("stringArg", "String"),
&[SourcePosition::new(89, 3, 42)],
)],
);
@ -302,9 +304,9 @@ mod tests {
stringArgField(stringArg: true)
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringArg", error::type_value("true", "String")),
&error_message("stringArg", "String"),
&[SourcePosition::new(89, 3, 42)],
)],
);
@ -320,9 +322,9 @@ mod tests {
stringArgField(stringArg: BAR)
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringArg", error::type_value("BAR", "String")),
&error_message("stringArg", "String"),
&[SourcePosition::new(89, 3, 42)],
)],
);
@ -338,9 +340,9 @@ mod tests {
intArgField(intArg: "3")
}
}
"#,
"#,
&[RuleError::new(
&error_message("intArg", error::type_value("\"3\"", "Int")),
&error_message("intArg", "Int"),
&[SourcePosition::new(83, 3, 36)],
)],
);
@ -356,9 +358,9 @@ mod tests {
intArgField(intArg: FOO)
}
}
"#,
"#,
&[RuleError::new(
&error_message("intArg", error::type_value("FOO", "Int")),
&error_message("intArg", "Int"),
&[SourcePosition::new(83, 3, 36)],
)],
);
@ -374,9 +376,9 @@ mod tests {
intArgField(intArg: 3.0)
}
}
"#,
"#,
&[RuleError::new(
&error_message("intArg", error::type_value("3", "Int")),
&error_message("intArg", "Int"),
&[SourcePosition::new(83, 3, 36)],
)],
);
@ -392,9 +394,9 @@ mod tests {
intArgField(intArg: 3.333)
}
}
"#,
"#,
&[RuleError::new(
&error_message("intArg", error::type_value("3.333", "Int")),
&error_message("intArg", "Int"),
&[SourcePosition::new(83, 3, 36)],
)],
);
@ -410,9 +412,9 @@ mod tests {
floatArgField(floatArg: "3.333")
}
}
"#,
"#,
&[RuleError::new(
&error_message("floatArg", error::type_value("\"3.333\"", "Float")),
&error_message("floatArg", "Float"),
&[SourcePosition::new(87, 3, 40)],
)],
);
@ -428,9 +430,9 @@ mod tests {
floatArgField(floatArg: true)
}
}
"#,
"#,
&[RuleError::new(
&error_message("floatArg", error::type_value("true", "Float")),
&error_message("floatArg", "Float"),
&[SourcePosition::new(87, 3, 40)],
)],
);
@ -446,9 +448,9 @@ mod tests {
floatArgField(floatArg: FOO)
}
}
"#,
"#,
&[RuleError::new(
&error_message("floatArg", error::type_value("FOO", "Float")),
&error_message("floatArg", "Float"),
&[SourcePosition::new(87, 3, 40)],
)],
);
@ -464,9 +466,9 @@ mod tests {
booleanArgField(booleanArg: 2)
}
}
"#,
"#,
&[RuleError::new(
&error_message("booleanArg", error::type_value("2", "Boolean")),
&error_message("booleanArg", "Boolean"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -482,9 +484,9 @@ mod tests {
booleanArgField(booleanArg: 1.0)
}
}
"#,
"#,
&[RuleError::new(
&error_message("booleanArg", error::type_value("1", "Boolean")),
&error_message("booleanArg", "Boolean"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -500,9 +502,9 @@ mod tests {
booleanArgField(booleanArg: "true")
}
}
"#,
"#,
&[RuleError::new(
&error_message("booleanArg", error::type_value("\"true\"", "Boolean")),
&error_message("booleanArg", "Boolean"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -518,9 +520,9 @@ mod tests {
booleanArgField(booleanArg: TRUE)
}
}
"#,
"#,
&[RuleError::new(
&error_message("booleanArg", error::type_value("TRUE", "Boolean")),
&error_message("booleanArg", "Boolean"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -536,9 +538,9 @@ mod tests {
idArgField(idArg: 1.0)
}
}
"#,
"#,
&[RuleError::new(
&error_message("idArg", error::type_value("1", "ID")),
&error_message("idArg", "ID"),
&[SourcePosition::new(81, 3, 34)],
)],
);
@ -554,9 +556,9 @@ mod tests {
idArgField(idArg: true)
}
}
"#,
"#,
&[RuleError::new(
&error_message("idArg", error::type_value("true", "ID")),
&error_message("idArg", "ID"),
&[SourcePosition::new(81, 3, 34)],
)],
);
@ -572,9 +574,9 @@ mod tests {
idArgField(idArg: SOMETHING)
}
}
"#,
"#,
&[RuleError::new(
&error_message("idArg", error::type_value("SOMETHING", "ID")),
&error_message("idArg", "ID"),
&[SourcePosition::new(81, 3, 34)],
)],
);
@ -590,9 +592,9 @@ mod tests {
doesKnowCommand(dogCommand: 2)
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::enum_value("2", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -608,9 +610,9 @@ mod tests {
doesKnowCommand(dogCommand: 1.0)
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::enum_value("1", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -626,9 +628,9 @@ mod tests {
doesKnowCommand(dogCommand: "SIT")
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::enum_value("\"SIT\"", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -644,9 +646,9 @@ mod tests {
doesKnowCommand(dogCommand: true)
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::enum_value("true", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -662,9 +664,9 @@ mod tests {
doesKnowCommand(dogCommand: JUGGLE)
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::type_value("JUGGLE", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -680,9 +682,9 @@ mod tests {
doesKnowCommand(dogCommand: sit)
}
}
"#,
"#,
&[RuleError::new(
&error_message("dogCommand", error::type_value("sit", "DogCommand")),
&error_message("dogCommand", "DogCommand"),
&[SourcePosition::new(79, 3, 44)],
)],
);
@ -698,7 +700,7 @@ mod tests {
stringListArgField(stringListArg: ["one", "two"])
}
}
"#,
"#,
);
}
@ -712,7 +714,7 @@ mod tests {
stringListArgField(stringListArg: [])
}
}
"#,
"#,
);
}
@ -726,7 +728,7 @@ mod tests {
stringListArgField(stringListArg: "one")
}
}
"#,
"#,
);
}
@ -740,9 +742,9 @@ mod tests {
stringListArgField(stringListArg: ["one", 2])
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringListArg", error::type_value("2", "String")),
&error_message("stringListArg", "[String]"),
&[SourcePosition::new(97, 3, 50)],
)],
);
@ -758,9 +760,9 @@ mod tests {
stringListArgField(stringListArg: 1)
}
}
"#,
"#,
&[RuleError::new(
&error_message("stringListArg", error::type_value("1", "String")),
&error_message("stringListArg", "[String]"),
&[SourcePosition::new(97, 3, 50)],
)],
);
@ -776,7 +778,7 @@ mod tests {
isHousetrained(atOtherHomes: true)
}
}
"#,
"#,
);
}
@ -790,7 +792,7 @@ mod tests {
isHousetrained
}
}
"#,
"#,
);
}
@ -804,7 +806,7 @@ mod tests {
multipleReqs(req1: 1, req2: 2)
}
}
"#,
"#,
);
}
@ -818,7 +820,7 @@ mod tests {
multipleReqs(req2: 2, req1: 1)
}
}
"#,
"#,
);
}
@ -832,7 +834,7 @@ mod tests {
multipleOpts
}
}
"#,
"#,
);
}
@ -846,7 +848,7 @@ mod tests {
multipleOpts(opt1: 1)
}
}
"#,
"#,
);
}
@ -860,7 +862,7 @@ mod tests {
multipleOpts(opt2: 1)
}
}
"#,
"#,
);
}
@ -874,7 +876,7 @@ mod tests {
multipleOptAndReq(req1: 3, req2: 4)
}
}
"#,
"#,
);
}
@ -888,7 +890,7 @@ mod tests {
multipleOptAndReq(req1: 3, req2: 4, opt1: 5)
}
}
"#,
"#,
);
}
@ -902,7 +904,7 @@ mod tests {
multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6)
}
}
"#,
"#,
);
}
@ -916,14 +918,14 @@ mod tests {
multipleReqs(req2: "two", req1: "one")
}
}
"#,
"#,
&[
RuleError::new(
&error_message("req2", error::type_value("\"two\"", "Int")),
&error_message("req2", "Int!"),
&[SourcePosition::new(82, 3, 35)],
),
RuleError::new(
&error_message("req1", error::type_value("\"one\"", "Int")),
&error_message("req1", "Int!"),
&[SourcePosition::new(95, 3, 48)],
),
],
@ -940,9 +942,9 @@ mod tests {
multipleReqs(req1: "one")
}
}
"#,
"#,
&[RuleError::new(
&error_message("req1", error::type_value("\"one\"", "Int")),
&error_message("req1", "Int!"),
&[SourcePosition::new(82, 3, 35)],
)],
);
@ -958,7 +960,7 @@ mod tests {
complexArgField
}
}
"#,
"#,
);
}
@ -972,7 +974,7 @@ mod tests {
complexArgField(complexArg: { requiredField: true })
}
}
"#,
"#,
);
}
@ -986,7 +988,7 @@ mod tests {
complexArgField(complexArg: { requiredField: false })
}
}
"#,
"#,
);
}
@ -1000,7 +1002,7 @@ mod tests {
complexArgField(complexArg: { requiredField: true, intField: 4 })
}
}
"#,
"#,
);
}
@ -1020,7 +1022,7 @@ mod tests {
})
}
}
"#,
"#,
);
}
@ -1040,7 +1042,7 @@ mod tests {
})
}
}
"#,
"#,
);
}
@ -1054,12 +1056,9 @@ mod tests {
complexArgField(complexArg: { intField: 4 })
}
}
"#,
"#,
&[RuleError::new(
&error_message(
"complexArg",
error::missing_fields("ComplexInput", "\"requiredField\""),
),
&error_message("complexArg", "ComplexInput"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -1078,16 +1077,9 @@ mod tests {
})
}
}
"#,
"#,
&[RuleError::new(
&error_message(
"complexArg",
error::field(
"ComplexInput",
"stringListField",
error::type_value("2", "String"),
),
),
&error_message("complexArg", "ComplexInput"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -1106,12 +1098,9 @@ mod tests {
})
}
}
"#,
"#,
&[RuleError::new(
&error_message(
"complexArg",
error::unknown_field("ComplexInput", "unknownField"),
),
&error_message("complexArg", "ComplexInput"),
&[SourcePosition::new(91, 3, 44)],
)],
);
@ -1130,7 +1119,7 @@ mod tests {
name
}
}
"#,
"#,
);
}
@ -1139,20 +1128,20 @@ mod tests {
expect_fails_rule::<_, _, DefaultScalarValue>(
factory,
r#"
{
dog @include(if: "yes") {
name @skip(if: ENUM)
}
}
"#,
{
dog @include(if: "yes") {
name @skip(if: ENUM)
}
}
"#,
&[
RuleError::new(
&error_message("if", error::type_value("\"yes\"", "Boolean")),
&[SourcePosition::new(46, 2, 31)],
&error_message("if", "Boolean!"),
&[SourcePosition::new(38, 2, 27)],
),
RuleError::new(
&error_message("if", error::type_value("ENUM", "Boolean")),
&[SourcePosition::new(86, 3, 31)],
&error_message("if", "Boolean!"),
&[SourcePosition::new(74, 3, 27)],
),
],
);

Some files were not shown because too many files have changed in this diff Show more