Crate cynic_introspection

Source
Expand description

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");

Modules§

query
Defines the types & results for running an introspection query against a GraphQL server.

Structs§

CapabilitiesQuery
A query that detects what capabilities a remote GraphQL server has, which can be used to determine what introspection query should be made against that server.
CapabilitySet
The set of capaiblities a GraphQL server supports.
Directive
A directive either used in the schema or available to queries
EnumType
GraphQL Enum types represent leaf values in a GraphQL type system which can have one of a set of possible values.
EnumValue
One of the possible values of an EnumType
Field
One of the fields of an ObjectType or InterfaceType
FieldType
The type of a Field or InputValue
FieldWrapping
A list of WrappingTypes that wrap a FieldType
InputObjectType
A GraphQL Input Object defines a set of input fields; the input fields are either scalars, enums, or other input objects
InputValue
Represents field and directive arguments as well as the fields of an input object.
InterfaceType
Interfaces are an abstract type where there are common fields declared.
IntrospectionQuery
A GraphQL Introspection Query for Cynic.
ObjectType
GraphQL Objects represent a list of named fields, each of which yield a value of a specific type.
ScalarType
Scalar types represent primitive leaf values in a GraphQL type system.
Schema
A GraphQL schema
UnionType
GraphQL Unions represent objects that could be any of a list of GraphQL Object types.

Enums§

Deprecated
Whether a Field or EnumValue is deprecated.
DirectiveLocation
A location where a directive can be used
SchemaError
An error that can occur when building the schema
SpecificationVersion
Versions of the GraphQL specification that the CapabilitiesQuery can detect.
Type
A type defined in a Schema
WrappingType
Types that can wrap either named types or other wrapper types