Skip to content

Commit

Permalink
Add a ty! macro to build a static ast::Type using GraphQL-like sy…
Browse files Browse the repository at this point in the history
…ntax
  • Loading branch information
SimonSapin committed Nov 14, 2023
1 parent 1ea6831 commit ed0d481
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
7 changes: 6 additions & 1 deletion crates/apollo-compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## Features

- **Helper features for `Name` - [SimonSapin], [pull/???]:**
- **Helper features for `Name` and `Type` - [SimonSapin], [pull/739]:**
* The `name!` macro also accepts an identifier:
`name!(Query)` and `name!("Query")` create equivalent `Name` values.
* `InvalidNameError` now contain a public `NodeStr` for the input string that is invalid,
and implements `Display`, `Debug`, and `Error` traits.
* Add `TryFrom` conversion to `Name` from `NodeStr`, `&NodeStr`, `&str`, `String`, and `&String`.
* Add a `ty!` macro to build a static `ast::Type` using GraphQL-like syntax.

[SimonSapin]: https://github.com/SimonSapin
[pull/739]: https://github.com/apollographql/apollo-rs/pull/739


# [1.0.0-beta.6](https://crates.io/crates/apollo-compiler/1.0.0-beta.6) - 2023-11-10

Expand Down
27 changes: 27 additions & 0 deletions crates/apollo-compiler/src/ast/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,33 @@ impl VariableDefinition {
serialize_method!();
}

/// Create a static [`Type`] with GraphQL-like syntax
///
/// ```
/// use apollo_compiler::ty;
///
/// assert_eq!(ty!(Obj).to_string(), "Obj");
/// assert_eq!(ty!(Obj!).to_string(), "Obj!");
/// assert_eq!(ty!([Obj]).to_string(), "[Obj]");
/// assert_eq!(ty!([Obj]!).to_string(), "[Obj]!");
/// assert_eq!(ty!([[[Obj ] !]]!).to_string(), "[[[Obj]!]]!");
/// ```
#[macro_export]
macro_rules! ty {
($name: ident) => {
$crate::ast::Type::Named($crate::name!($name))
};
($name: ident !) => {
$crate::ast::Type::NonNullNamed($crate::name!($name))
};
([ $($tt: tt)+ ]) => {
$crate::ast::Type::List(::std::boxed::Box::new($crate::ty!( $($tt)+ )))
};
([ $($tt: tt)+ ]!) => {
$crate::ast::Type::NonNullList(::std::boxed::Box::new($crate::ty!( $($tt)+ )))
};
}

impl Type {
/// Returns this type made non-null, if it isn’t already.
pub fn non_null(self) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions crates/apollo-compiler/src/executable/from_ast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::name;
use crate::ty;

pub(crate) struct BuildErrors {
pub(crate) errors: Vec<BuildError>,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl SelectionSet {
description: None,
name: ast.name.clone(),
arguments: Vec::new(),
ty: Type::Named(name!("UNKNOWN")),
ty: ty!(UNKNOWN),
directives: Default::default(),
}))
};
Expand Down
9 changes: 5 additions & 4 deletions crates/apollo-compiler/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use crate::ast::{
InputValueDefinition, Name, NamedType, Type, Value,
};
use crate::name;
use crate::ty;
use crate::validation::DiagnosticList;

/// High-level representation of a GraphQL schema
Expand Down Expand Up @@ -485,15 +486,15 @@ impl Schema {
description: None,
name: name!("__typename"),
arguments: Vec::new(),
ty: Type::Named(name!("String")).non_null(),
ty: ty!(String!),
directives: ast::DirectiveList::new(),
}),
// __schema: __Schema!
Component::new(FieldDefinition {
description: None,
name: name!("__schema"),
arguments: Vec::new(),
ty: Type::Named(name!("__Schema")).non_null(),
ty: ty!(__Schema!),
directives: ast::DirectiveList::new(),
}),
// __type(name: String!): __Type
Expand All @@ -503,12 +504,12 @@ impl Schema {
arguments: vec![InputValueDefinition {
description: None,
name: name!("name"),
ty: ast::Type::Named(name!("String")).non_null().into(),
ty: ty!(String!).into(),
default_value: None,
directives: ast::DirectiveList::new(),
}
.into()],
ty: Type::Named(name!("__Type")),
ty: ty!(__Type),
directives: ast::DirectiveList::new(),
}),
]
Expand Down
3 changes: 2 additions & 1 deletion crates/apollo-compiler/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use apollo_compiler::ast;
use apollo_compiler::name;
use apollo_compiler::schema;
use apollo_compiler::ty;
use apollo_compiler::DiagnosticList;
use apollo_compiler::FileId;
use apollo_compiler::Schema;
Expand Down Expand Up @@ -266,7 +267,7 @@ fn test_invalid_synthetic_node() {
description: Default::default(),
name: name!("field"),
arguments: Default::default(),
ty: schema::Type::Named(name!("UndefinedType")),
ty: ty!(UndefinedType),
directives: Default::default(),
}
.into(),
Expand Down

0 comments on commit ed0d481

Please sign in to comment.