apollo-compiler
A query-based compiler for the GraphQL query language.
## Features
* A (comparatively) low-level AST for GraphQL grammar,
and high-level representation of `Schema` and `ExecutableDocument`.
* 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][val].
[val]: https://spec.graphql.org/October2021/#sec-Validation
## Getting started
Add the dependency to start using `apollo-compiler`:
```bash
cargo add apollo-compiler
```
Or add this to your `Cargo.toml` for a manual installation:
```toml
# Just an example, change to the necessary package version.
# Using an exact dependency is recommended for beta versions
[dependencies]
apollo-compiler = "=1.0.0-beta.24"
```
## 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`:
```rust
use apollo_compiler::Schema;
let input = r#"
interface Pet {
name: String
}
type Dog implements Pet {
name: String
nickname: String
barkVolume: Int
}
type Cat implements Pet {
name: String
nickname: String
meowVolume: Int
}
union CatOrDog = Cat | Dog
type Human {
name: String
pets: [Pet]
}
type Query {
human: Human
}
"#;
/// In case of validation errors, the panic message will be nicely formatted
/// to point at relevant parts of the source file(s)
let schema = Schema::parse_and_validate(input, "document.graphql").unwrap();
```
### Examples
#### Accessing fragment definition field types
```rust
use apollo_compiler::{Schema, ExecutableDocument, Node, executable};
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 = Schema::parse_and_validate(schema_input, "schema.graphql").unwrap();
let document = ExecutableDocument::parse_and_validate(&schema, query_input, "query.graphql")
.unwrap();
let op = document.operations.get(Some("getUser")).expect("getUser query does not exist");
let fragment_in_op = op.selection_set.selections.iter().filter_map(|sel| match sel {
executable::Selection::FragmentSpread(spread) => {
Some(document.fragments.get(&spread.fragment_name)?.as_ref())
}
_ => None
}).collect::