28 stable releases

3.10.0 Feb 10, 2025
3.9.1 Dec 3, 2024
3.9.0 Nov 12, 2024
3.7.3 Jun 4, 2024
3.2.2 Jun 26, 2023

#725 in HTTP server

Download history 2289/week @ 2025-01-13 2443/week @ 2025-01-20 2336/week @ 2025-01-27 2428/week @ 2025-02-03 4137/week @ 2025-02-10 3197/week @ 2025-02-17 2514/week @ 2025-02-24 3114/week @ 2025-03-03 2658/week @ 2025-03-10 3141/week @ 2025-03-17 3875/week @ 2025-03-24 3718/week @ 2025-03-31 3865/week @ 2025-04-07 2432/week @ 2025-04-14 1849/week @ 2025-04-21 2439/week @ 2025-04-28

10,741 downloads per month
Used in cynic-cli

MPL-2.0 license

605KB
14K SLoC

cynic-introspection defines a GraphQL introspection query that can be run using cynic, a rust GraphQL client.

This can be used for any reason you'd want to introspect a GraphQL server - including when you're using cynic as a library in your own project.

It also provides a [Schema] abstraction on top of the introspection query results, which provides some stronger typing than using the introspection results directly.

use cynic::{QueryBuilder, http::ReqwestExt};
use cynic_introspection::IntrospectionQuery;

// We can run an introspection query and unwrap the data contained within
let introspection_data = reqwest::Client::new()
    .post(url)
    .run_graphql(IntrospectionQuery::build(()))
    .await
    .unwrap()
    .data
    .unwrap();

// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();

assert_eq!(schema.query_type, "Root");

GraphQL Versions

GraphQL servers currently commonly support two different versions of the GraphQL specification:

The fields available for introspection differ between these two versions. By default we query only for fields supported in the June 2018 specification. You can request a different version of the query using InstrospectionQuery::with_capabilities:

use cynic::http::ReqwestBlockingExt;
use cynic_introspection::{IntrospectionQuery, SpecificationVersion};

// We can run an introspection query and unwrap the data contained within
let introspection_data = reqwest::blocking::Client::new()
    .post("https://spacex-production.up.railway.app/")
    .run_graphql(
        IntrospectionQuery::with_capabilities(
            SpecificationVersion::October2021.capabilities()
        )
    )
    .unwrap()
    .data
    .unwrap();

// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();

assert_eq!(schema.query_type, "Query");

Detecting Capabilities

cynic-introspection also provides [CapabilitiesQuery], a query which can determine the capabilites of a remote GraphQL server. This can be paired with Introspection::with_capabilities:

use cynic::{QueryBuilder, http::ReqwestExt};
use cynic_introspection::{CapabilitiesQuery, IntrospectionQuery};

// First we run a capabilites query to check what the server supports
let capabilities = reqwest::Client::new()
    .post(url)
    .run_graphql(CapabilitiesQuery::build(()))
    .await
    .unwrap()
    .data
    .unwrap()
    .capabilities();

// Now we can safely run introspection, only querying for what the server supports.
let introspection_data = reqwest::Client::new()
    .post(url)
    .run_graphql(IntrospectionQuery::with_capabilities(capabilities))
    .await
    .unwrap()
    .data
    .unwrap();

// And then convert it into a schema for easier use.
let schema = introspection_data.into_schema().unwrap();

assert_eq!(schema.query_type, "Root");

Cynic Introspection

cynic-introspection defines a GraphQL introspection query that can be run using cynic, a rust GraphQL client

This can be used for any reason you'd want to introspect a GraphQL server - including when you're using cynic as a library in your own project.

Features

  • Support for introspecting servers that support both GraphQL 2021 and GraphQL 2018.
  • Contains a capability detection query that can determine which version of the GraphQL specification a server supports prior to querying it.

Usage

See the documentation on docs.rs for instructions on using cynic-introspection.

Dependencies

~5MB
~80K SLoC