Features
- A (comparatively) low-level AST for GraphQL grammar,
and high-level representation of
Schema
andExecutableDocument
. - All three can be parsed (using
apollo-parser
internally), created or modified programatically, and serialized. - Validation of schemas and executable documents, as defined in the GraphQL specification.
- Execution of the schema introspection portion of queries.
Getting started
Add the dependency to start using apollo-compiler
:
Or add this to your Cargo.toml
for a manual installation:
# Just an example, change to the necessary package version.
[]
= "1.28.0"
Rust versions
apollo-compiler
is tested on the latest stable version of Rust.
Older version may or may not be compatible.
Usage
You can get started with apollo-compiler
:
use Schema;
use ExecutableDocument;
let sdl = r#"
type Query {
field: Int
}
"#;
let query = "{ field }";
/// In case of validation errors, the panic message will be nicely formatted
/// to point at relevant parts of the source file(s)
let schema = parse_and_validate.unwrap;
let doc = parse_and_validate.unwrap;
Examples
Accessing fragment definition field types
use ;
let schema_input = r#"
type User {
id: ID
name: String
profilePic(size: Int): URL
}
schema { query: User }
scalar URL @specifiedBy(url: "https://tools.ietf.org/html/rfc3986")
"#;
let query_input = r#"
query getUser {
... vipCustomer
}
#fragment definition where we want to know the field types.
fragment vipCustomer on User {
id
name
profilePic(size: 50)
}
"#;
let schema = parse_and_validate.unwrap;
let document = parse_and_validate
.unwrap;
let op = document.operations.get.expect;
let fragment_in_op = op.selection_set.selections.iter.filter_map.;
let fragment_fields = fragment_in_op.iter.flat_map.;
let field_ty = fragment_fields
.iter
.map
.;
assert_eq!;
Get a directive defined on a field used in a query operation definition.
use ;
let schema_input = r#"
type Query {
topProducts: Product
}
type Product {
inStock: Boolean @join__field(graph: INVENTORY)
name: String @join__field(graph: PRODUCTS)
}
enum join__Graph {
INVENTORY,
PRODUCTS,
}
directive @join__field(graph: join__Graph) on FIELD_DEFINITION
"#;
let query_input = r#"
query getProduct {
topProducts {
inStock
}
}
"#;
let schema = parse_and_validate.unwrap;
let document = parse_and_validate
.unwrap;
let get_product_op = document
.operations
.get
.expect;
let in_stock_field = &get_product_op
.selection_set
.fields
.find
.expect
.selection_set
.fields
.find
.expect
.definition;
let in_stock_directive: = in_stock_field
.directives
.iter
.map
.collect;
assert_eq!;
Printing diagnostics for a faulty GraphQL document
use Parser;
let input = "{ ... }";
if let Err = new.parse_mixed_validate
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.