Introspection
GraphQL defines a special built-in top-level field called __schema
. Querying
for this field allows one to introspect the schema
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:
{
__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:
#![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()); }