Skip to content

Latest commit

 

History

History
66 lines (52 loc) · 1.71 KB

File metadata and controls

66 lines (52 loc) · 1.71 KB

Tracing

Juniper has optional support for the tracing crate for instrumentation.

This feature is off by default and can be enabled via the tracing feature.

!FILENAME Cargo.toml

[dependencies]
juniper = { version = "0.14.2", features = ["default", "tracing"]}
tracing = "0.1.17"
tracing-subscriber = "0.2.9"

Usage

# extern crate juniper;
extern crate tokio;
extern crate tracing;
extern crate tracing_subscriber;
use juniper::{EmptyMutation, EmptySubscription, RootNode, graphql_object, Variables};

#[derive(Clone, Copy, Debug)]
struct Query;

#[graphql_object]
impl Query {
    async fn foo() -> i32 {
        42
    }
}

type Schema = RootNode<'static, Query, EmptyMutation<()>, EmptySubscription<()>>;


#[tokio::main]
async fn main() {
    // Set up the tracing subscriber.
    let subscriber = tracing_subscriber::fmt()
        // This enables standard env variables such as `RUST_LOG=trace`.
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        // This makes it so we can see all span events.
        .with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
        .finish();

    tracing::subscriber::set_global_default(subscriber)
        .expect("Setting default tracing subscriber failed");

    // Set up GraphQL information.
    let vars = Variables::new();
    let root = Schema::new(
        Query,
        EmptyMutation::<()>::new(),
        EmptySubscription::<()>::new(),
    );

    // When run with `RUST_LOG=trace cargo run`, this should output traces /
    // span events to `stdout`.
    let query = "{ foo }";
    let (_, _errors) = juniper::execute(query, None, &root, &vars, &())
        .await
        .unwrap();
}