graphql_schema

Macro graphql_schema 

Source
graphql_schema!() { /* proc-macro */ }
Expand description

Evaluates to a Schema object given direct GraphQL schema document syntax.

This macro is effectively a compile-time version of SchemaBuilder::build_from_str(), except you write GraphQL syntax in your Rust file and the macro parses it as GraphQL for you.

⚠️ NOTE: Due to limitations in Rust macros’ ability to tokenize # as a single token, # cannot be used to specify a GraphQL comment in a GraphQL schema document defined with graphql_schema! { .. }. Instead, you can use Rust’s // inline comment syntax directly in your GraphQL syntax.

Example usage:

use libgraphql::macros::graphql_schema;

let schema = graphql_schema! {
    type Query {
        // This field always resolves the currently-authenticated `User`.
        me: User,
    }

    type User {
        firstName: String,
        lastName: String,
    }
};

let user_type =
    schema.defined_types()
        .get("User")
        .unwrap()
        .as_object()
        .unwrap();

assert_eq!(user_type.name(), "User");
assert_eq!(user_type.fields().get("firstName").is_some(), true);
assert_eq!(user_type.fields().get("firstName").is_some(), true);
assert_eq!(user_type.fields().get("doesntExist").is_some(), false);