Skip to content

Commit

Permalink
add macro output snapshots (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi authored Jun 26, 2024
1 parent 49bf6c5 commit 8f61125
Show file tree
Hide file tree
Showing 15 changed files with 1,072 additions and 6 deletions.
2 changes: 2 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ tempfile = "3"
async-stream = "0.3"
futures-util = "0.3"
insta={version="1",features=["json"]}
prettier-please="0.2"
unindent = "0.2"

[lib]
proc-macro = true
12 changes: 7 additions & 5 deletions derive/src/args/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;

pub use args::*;
pub use clippy_error::impl_suppress_tupple_clippy_error;
pub use fields::*;
Expand Down Expand Up @@ -274,7 +272,7 @@ where
A: CommonArg,
{
let mut errors = Vec::new();
let mut types = HashSet::new();
let mut types = Vec::new();

let fields = object.get_fields()?;
fields
Expand All @@ -293,13 +291,17 @@ where
if is_arg_ctx(arg) {
return;
}
types.insert(&ty.ty);
if !types.contains(&&ty.ty) {
types.push(&ty.ty);
}
}
});
let ty = field.get_type();
match ty {
Ok(ty) => {
types.insert(ty);
if !types.contains(&ty) {
types.push(ty);
}
}
Err(err) => errors.push(err),
};
Expand Down
2 changes: 1 addition & 1 deletion derive/src/args/common/clippy_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn impl_suppress_tupple_clippy_error(
impl #impl_generics #ident #ty_generics #where_clause {
#[allow(dead_code)]
#[doc(hidden)]
fn _suppress_clippy_error(&self) {
fn __suppress_clippy_error(&self) {
#accesses
}
}
Expand Down
2 changes: 2 additions & 0 deletions derive/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ mod resolved_object;
mod resolved_object_fields;
mod scalar;
mod simple_object;
#[cfg(test)]
mod test_output;
mod union;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: derive/src/args/test_output.rs
expression: output
---
```rust
struct App(QueryRoot, PostApp);
impl dynamic_graphql::internal::Register for App {
fn register(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry {
let registry = registry.register::<QueryRoot>();
let registry = registry.register::<PostApp>();
registry
}
}
impl App {
pub fn create_schema() -> dynamic_graphql::dynamic::SchemaBuilder {
let registry = dynamic_graphql::internal::Registry::new();
let registry = registry.register::<Self>();
registry.create_schema()
}
}
impl App {
#[allow(dead_code)]
#[doc(hidden)]
fn __suppress_clippy_error(&self) {
let _ = self.0;
let _ = self.1;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
source: derive/src/args/test_output.rs
expression: output
---
```rust
enum Example {
Foo,
Bar,
}
impl dynamic_graphql::internal::TypeName for Example {
fn get_type_name() -> std::borrow::Cow<'static, str> {
"Example".into()
}
}
impl dynamic_graphql::internal::InputTypeName for Example {}
impl dynamic_graphql::internal::OutputTypeName for Example {}
impl dynamic_graphql::internal::Enum for Example {}
impl From<&Example> for dynamic_graphql::Value {
fn from(value: &Example) -> Self {
match value {
Example::Foo => {
dynamic_graphql::Value::Enum(dynamic_graphql::Name::new("FOO"))
}
Example::Bar => {
dynamic_graphql::Value::Enum(dynamic_graphql::Name::new("BAR"))
}
}
}
}
impl<
'__dynamic_graphql_lifetime,
> dynamic_graphql::internal::ResolveOwned<'__dynamic_graphql_lifetime> for Example {
fn resolve_owned(
self,
_ctx: &dynamic_graphql::Context,
) -> dynamic_graphql::Result<
Option<dynamic_graphql::FieldValue<'__dynamic_graphql_lifetime>>,
> {
Ok(Some(dynamic_graphql::FieldValue::value(&self)))
}
}
impl<
'__dynamic_graphql_lifetime,
> dynamic_graphql::internal::ResolveRef<'__dynamic_graphql_lifetime> for Example {
fn resolve_ref(
&'__dynamic_graphql_lifetime self,
_ctx: &dynamic_graphql::Context,
) -> dynamic_graphql::Result<
Option<dynamic_graphql::FieldValue<'__dynamic_graphql_lifetime>>,
> {
Ok(Some(dynamic_graphql::FieldValue::value(self)))
}
}
impl dynamic_graphql::internal::FromValue for Example {
fn from_value(
__value: dynamic_graphql::Result<dynamic_graphql::dynamic::ValueAccessor>,
) -> dynamic_graphql::internal::InputValueResult<Self> {
let __value = __value?;
let string_value = __value.enum_name()?;
match string_value {
"FOO" => Ok(Example::Foo),
"BAR" => Ok(Example::Bar),
_ => {
Err(
dynamic_graphql::internal::InputValueError::custom(
format!(
"Unknown variant `{}` for enum `{}`", string_value, < Example
as dynamic_graphql::internal::Enum > ::get_enum_type_name()
.as_ref()
),
),
)
}
}
}
}
impl dynamic_graphql::internal::Register for Example {
fn register(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry {
let object = dynamic_graphql::dynamic::Enum::new(
<Example as dynamic_graphql::internal::Enum>::get_enum_type_name().as_ref(),
);
let field = dynamic_graphql::dynamic::EnumItem::new("FOO");
let object = object.item(field);
let field = dynamic_graphql::dynamic::EnumItem::new("BAR");
let object = object.item(field);
registry.register_type(object)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
source: derive/src/args/test_output.rs
expression: output
---
```rust
struct ExampleQuery<'a>(&'a Query);
impl<'a> ExampleQuery<'a> {
fn __registers(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry {
registry
}
}
impl<'a> dynamic_graphql::internal::ParentType for ExampleQuery<'a> {
type Type = Query;
}
impl<'a> dynamic_graphql::internal::ExpandObject for ExampleQuery<'a> {
fn get_expand_object_name() -> std::borrow::Cow<'static, str> {
"ExampleQuery".into()
}
}
impl<'a> From<&'a Query> for ExampleQuery<'a> {
fn from(target: &'a Query) -> Self {
Self(target)
}
}
impl<'a> dynamic_graphql::internal::RegisterFns for ExampleQuery<'a> {
const REGISTER_FNS: &'static [fn(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry] = &[ExampleQuery::<'a>::__registers];
}
impl<'a> ExampleQuery<'a> {
#[allow(dead_code)]
#[doc(hidden)]
fn __suppress_clippy_error(&self) {
let _ = self.0;
}
}
impl ExampleQuery<'_> {
fn the_example(&self) -> Example {
Example {
field: "field".to_string(),
}
}
}
impl dynamic_graphql::internal::Register for ExampleQuery<'_> {
fn register(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry {
let registry = registry.register::<Example>();
let registry = <Self as dynamic_graphql::internal::RegisterFns>::REGISTER_FNS
.iter()
.fold(registry, |registry, f| f(registry));
let field = dynamic_graphql::dynamic::Field::new(
"theExample",
<Example as dynamic_graphql::internal::GetOutputTypeRef>::get_output_type_ref(),
|ctx| {
dynamic_graphql::dynamic::FieldFuture::new(async move {
let parent = ctx
.parent_value
.try_downcast_ref::<
<Self as dynamic_graphql::internal::ParentType>::Type,
>()?
.into();
let arg0 = &parent;
let value = ExampleQuery::the_example(arg0);
dynamic_graphql::internal::Resolve::resolve(value, &ctx)
})
},
);
let __field_0 = field;
registry
.update_object(
<<Self as dynamic_graphql::internal::ParentType>::Type as dynamic_graphql::internal::Object>::get_object_type_name()
.as_ref(),
<Self as dynamic_graphql::internal::ExpandObject>::get_expand_object_name()
.as_ref(),
|object| {
let object = object.field(__field_0);
object
},
)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: derive/src/args/test_output.rs
expression: output
---
```rust
struct ExampleInput {
pub string: String,
}
impl dynamic_graphql::internal::TypeName for ExampleInput {
fn get_type_name() -> std::borrow::Cow<'static, str> {
"ExampleInput".into()
}
}
impl dynamic_graphql::internal::InputTypeName for ExampleInput {}
impl dynamic_graphql::internal::InputObject for ExampleInput {}
impl dynamic_graphql::internal::Register for ExampleInput {
fn register(
registry: dynamic_graphql::internal::Registry,
) -> dynamic_graphql::internal::Registry {
let registry = registry.register::<String>();
let object = dynamic_graphql::dynamic::InputObject::new(
<Self as dynamic_graphql::internal::InputObject>::get_input_object_type_name()
.as_ref(),
);
let field = dynamic_graphql::dynamic::InputValue::new(
"string",
<String as dynamic_graphql::internal::GetInputTypeRef>::get_input_type_ref(),
);
let object = object.field(field);
registry.register_type(object)
}
}
impl dynamic_graphql::internal::FromValue for ExampleInput {
fn from_value(
__value: dynamic_graphql::Result<dynamic_graphql::dynamic::ValueAccessor>,
) -> dynamic_graphql::internal::InputValueResult<Self> {
let __value = __value?;
let __object = __value.object()?;
let field0 = dynamic_graphql::internal::FromValue::from_value(
__object.try_get("string"),
)
.map_err(|e| e.into_field_error("string"))?;
Ok(Self { string: field0 })
}
}
```
Loading

0 comments on commit 8f61125

Please sign in to comment.