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.
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);§⚠️ NOTE:
Due to limitations downstream of how Rust macros tokenize syntax, there are a few inline GraphQL syntax edge-cases that are not supported by this macro:
-
#cannot be used to specify GraphQL comments. Instead, you can use Rust’s//or/*comment syntax.So for example, this won’t compile:
ⓘlet schema = graphql_schema! { type Query { me: User, } # Represents a user in the system. type User { firstName: String, lastName: String, } };But you can use rust’s
//and/*comment syntax instead:let schema = graphql_schema! { type Query { me: User, } // Represents a user in the system. type User { /* The user's first name. */ firstName: String, /* The user's last name. */ lastName: String, } }; -
Block-quoted strings (
""") are supported, but if you ever need to nest a quoted string of any kind /within/ a block-quoted string, you’ll need to use Rust’sr#""#“raw string” syntax instead.So for example, this won’t compile:
ⓘlet schema = graphql_schema! { type Query { me: User, } type User { """ The user's "primary" address. """ address: String, } };But the workaround is to use raw-string syntax instead:
let schema = graphql_schema! { type Query { me: User, } type User { r#" The user's "primary" address. "# address: String, } };