libgraphql_macros/
lib.rs

1mod graphql_schema_token_consumer;
2mod graphql_schema_from_str_token_consumer;
3mod emittable_schema;
4mod graphql_schema_parser;
5mod graphql_parse_error;
6mod graphql_token_stream;
7mod rust_to_graphql_token_adapter;
8
9#[cfg(test)]
10mod tests;
11
12use crate::graphql_schema_token_consumer::GraphQLSchemaTokenConsumer;
13use crate::graphql_schema_from_str_token_consumer::GraphQLSchemaFromStrTokenConsumer;
14
15/// Evaluates to a [`Schema`](libgraphql_core::schema::Schema) object given direct GraphQL
16/// schema document syntax.
17///
18/// This macro is effectively a compile-time version of
19/// [`SchemaBuilder::build_from_str()`](libgraphql_core::schema::SchemaBuilder::build_from_ast()),
20/// except you write GraphQL syntax in your Rust file and the macro parses it as
21/// GraphQL for you.
22///
23/// > **⚠️ NOTE:** Due to limitations in Rust macros' ability to tokenize `#` as
24/// > a single token, `#` cannot be used to specify a GraphQL comment in a
25/// > GraphQL schema document defined with `graphql_schema! { .. }`. Instead,
26/// > you can use Rust's `//` inline comment syntax directly in your GraphQL
27/// > syntax.
28///
29/// Example usage:
30///
31/// ```rust
32/// use libgraphql::macros::graphql_schema;
33///
34/// let schema = graphql_schema! {
35///     type Query {
36///         // This field always resolves the currently-authenticated `User`.
37///         me: User,
38///     }
39///
40///     type User {
41///         firstName: String,
42///         lastName: String,
43///     }
44/// };
45///
46/// let user_type =
47///     schema.defined_types()
48///         .get("User")
49///         .unwrap()
50///         .as_object()
51///         .unwrap();
52///
53/// assert_eq!(user_type.name(), "User");
54/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
55/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
56/// assert_eq!(user_type.fields().get("doesntExist").is_some(), false);
57/// ```
58#[proc_macro]
59pub fn graphql_schema(
60    input: proc_macro::TokenStream,
61) -> proc_macro::TokenStream {
62    GraphQLSchemaTokenConsumer::new(input).into()
63}
64
65/// Evaluates to a [`Schema`](libgraphql_core::schema::Schema) object given a literal
66/// Rust `str` containing GraphQL document text that represents a GraphQL
67/// schema.
68///
69/// This macro is effectively a compile-time version of
70/// [`SchemaBuilder::build_from_str()`](libgraphql_core::schema::SchemaBuilder::build_from_str()).
71///
72/// Example usage:
73///
74/// ```rust
75/// use libgraphql::macros::graphql_schema_from_str;
76///
77/// let schema = graphql_schema_from_str!(r#"
78///     type Query {
79///         me: User,
80///
81///     }
82///
83///     type User {
84///         firstName: String,
85///         lastName: String,
86///     }
87/// "#r);
88///
89/// let user_type =
90///     schema.defined_types()
91///         .get("User")
92///         .unwrap()
93///         .as_object()
94///         .unwrap();
95///
96/// assert_eq!(user_type.name(), "User");
97/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
98/// assert_eq!(user_type.fields().get("firstName").is_some(), true);
99/// assert_eq!(user_type.fields().get("doesntExist").is_some(), false);
100/// ```
101#[proc_macro]
102pub fn graphql_schema_from_str(
103    input: proc_macro::TokenStream,
104) -> proc_macro::TokenStream {
105    GraphQLSchemaFromStrTokenConsumer::new(input).into()
106}