Skip to content

Commit

Permalink
wip simplifying the schema module
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed Dec 30, 2023
1 parent 9521d56 commit 6953023
Show file tree
Hide file tree
Showing 15 changed files with 1,129 additions and 5,005 deletions.
14 changes: 11 additions & 3 deletions cynic-codegen/src/fragment_derive/fragment_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum Selection<'a> {
}

struct FieldSelection<'a> {
object_marker_type: syn::Path,
rust_field_type: syn::Type,
field_marker_type_path: syn::Path,
graphql_field_kind: FieldKind,
Expand Down Expand Up @@ -90,6 +91,7 @@ impl<'schema, 'a: 'schema> FragmentImpl<'schema, 'a> {
schema,
field,
schema_field.as_ref(),
&schema_type_path,
&field_module_path,
schema_module_path,
variables_fields,
Expand Down Expand Up @@ -119,6 +121,7 @@ fn process_field<'a>(
schema: &'a Schema<'a, Unvalidated>,
field: &FragmentDeriveField,
schema_field: Option<&'a Field<'a>>,
object_marker_type: &syn::Path,
field_module_path: &syn::Path,
schema_module_path: &syn::Path,
variables_fields: Option<&syn::Path>,
Expand Down Expand Up @@ -151,6 +154,7 @@ fn process_field<'a>(
let field_marker_type_path = schema_field.marker_ident().to_path(field_module_path);

Ok(Selection::Field(FieldSelection {
object_marker_type: object_marker_type.clone(),
rust_field_type: field.ty.clone(),
arguments,
field_marker_type_path,
Expand Down Expand Up @@ -224,6 +228,7 @@ impl quote::ToTokens for FieldSelection<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
use quote::TokenStreamExt;

let object_marker_type = &self.object_marker_type;
let field_marker_type_path = &self.field_marker_type_path;
let field_type = &self.rust_field_type;
let arguments = &self.arguments;
Expand Down Expand Up @@ -264,7 +269,8 @@ impl quote::ToTokens for FieldSelection<'_> {
}
FieldKind::Scalar => quote_spanned! { self.span =>
<#aligned_type as cynic::schema::IsScalar<
<#field_marker_type_path as cynic::schema::Field>::Type
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
// <#field_marker_type_path as cynic::schema::Field>::Type
>>::SchemaType
},
FieldKind::Enum => quote_spanned! { self.span =>
Expand Down Expand Up @@ -295,7 +301,8 @@ impl quote::ToTokens for FieldSelection<'_> {
.select_flattened_field::<
#field_marker_type_path,
#schema_type_lookup,
<#field_marker_type_path as cynic::schema::Field>::Type,
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
// <#field_marker_type_path as cynic::schema::Field>::Type,
>();

#alias
Expand All @@ -312,7 +319,8 @@ impl quote::ToTokens for FieldSelection<'_> {
.select_flattened_field::<
#field_marker_type_path,
#schema_type_lookup,
<#field_marker_type_path as cynic::schema::Field>::Type,
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
// <#field_marker_type_path as cynic::schema::Field>::Type,
>();

#alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl cynic::QueryFragment for MyQuery {
#![allow(unused_mut)]
let mut field_builder = builder
.select_field::<schema::__fields::Query::__typename, <String as cynic::schema::IsScalar<
<schema::__fields::Query::__typename as cynic::schema::Field>::Type,
>>::SchemaType>();
<schema::Query as cynic::schema::HasField<schema::__fields::Query::__typename>>::Type,
>>::SchemaType>();
if builder.is_feature_enabled("2018") {
let mut field_builder = builder . select_field :: < schema :: __fields :: Query :: filteredPosts , < Vec < BlogPostOutput > as cynic :: QueryFragment > :: SchemaType > () ;
<Vec<BlogPostOutput> as cynic::QueryFragment>::query(field_builder.select_children());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl cynic::QueryFragment for Film {
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
) {
#![allow(unused_mut)]
let mut field_builder = builder . select_flattened_field :: < schema :: __fields :: Film :: producers , < Vec < String > as cynic :: schema :: IsScalar < < schema :: __fields :: Film :: producers as cynic :: schema :: Field > :: Type >> :: SchemaType , < schema :: __fields :: Film :: producers as cynic :: schema :: Field > :: Type , > () ;
let mut field_builder = builder . select_flattened_field :: < schema :: __fields :: Film :: producers , < Vec < String > as cynic :: schema :: IsScalar < < schema :: Film as cynic :: schema :: HasField < schema :: __fields :: Film :: producers >> :: Type >> :: SchemaType , < schema :: Film as cynic :: schema :: HasField < schema :: __fields :: Film :: producers >> :: Type > () ;
}
fn name() -> Option<std::borrow::Cow<'static, str>> {
Some(std::borrow::Cow::Borrowed("Film"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl cynic::QueryFragment for BlogPostOutput {
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
) {
#![allow(unused_mut)]
let mut field_builder = builder . select_field :: < schema :: __fields :: BlogPost :: hasMetadata , < Option < bool > as cynic :: schema :: IsScalar < < schema :: __fields :: BlogPost :: hasMetadata as cynic :: schema :: Field > :: Type >> :: SchemaType > () ;
let mut field_builder = builder . select_field :: < schema :: __fields :: BlogPost :: hasMetadata , < Option < bool > as cynic :: schema :: IsScalar < < schema :: BlogPost as cynic :: schema :: HasField < schema :: __fields :: BlogPost :: hasMetadata >> :: Type >> :: SchemaType > () ;
let mut field_builder = builder . select_field :: < schema :: __fields :: BlogPost :: author , < AuthorOutput as cynic :: QueryFragment > :: SchemaType > () ;
<AuthorOutput as cynic::QueryFragment>::query(field_builder.select_children());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl cynic::QueryFragment for Film {
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
) {
#![allow(unused_mut)]
let mut field_builder = builder . select_field :: < schema :: __fields :: Film :: releaseDate , < Option < String > as cynic :: schema :: IsScalar < < schema :: __fields :: Film :: releaseDate as cynic :: schema :: Field > :: Type >> :: SchemaType > () ;
let mut field_builder = builder . select_field :: < schema :: __fields :: Film :: releaseDate , < Option < String > as cynic :: schema :: IsScalar < < schema :: Film as cynic :: schema :: HasField < schema :: __fields :: Film :: releaseDate >> :: Type >> :: SchemaType > () ;
<FilmDetails as cynic::QueryFragment>::query(
builder
.inline_fragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl cynic::QueryFragment for Film {
.inline_fragment()
.select_children::<<FilmDetails as cynic::QueryFragment>::VariablesFields>(),
);
let mut field_builder = builder . select_field :: < schema :: __fields :: Film :: releaseDate , < Option < String > as cynic :: schema :: IsScalar < < schema :: __fields :: Film :: releaseDate as cynic :: schema :: Field > :: Type >> :: SchemaType > () ;
let mut field_builder = builder . select_field :: < schema :: __fields :: Film :: releaseDate , < Option < String > as cynic :: schema :: IsScalar < < schema :: Film as cynic :: schema :: HasField < schema :: __fields :: Film :: releaseDate >> :: Type >> :: SchemaType > () ;
}
fn name() -> Option<std::borrow::Cow<'static, str>> {
Some(std::borrow::Cow::Borrowed("Film"))
Expand Down
6 changes: 1 addition & 5 deletions cynic-codegen/src/use_schema/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ impl ToTokens for FieldOutput<'_> {
tokens.append_all(quote! {
pub struct #field_marker;

impl cynic::schema::Field for #field_marker{
impl cynic::schema::HasField<#field_marker> for super::super::#parent_marker {
type Type = #field_type_marker;

const NAME: &'static str = #field_name_literal;
}

impl cynic::schema::HasField<#field_marker> for super::super::#parent_marker {
type Type = #field_type_marker;
}
});

if !self.field.arguments.is_empty() {
Expand Down
5 changes: 1 addition & 4 deletions cynic-codegen/src/use_schema/input_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ impl ToTokens for FieldOutput<'_> {
tokens.append_all(quote! {
pub struct #field_marker;

impl cynic::schema::Field for #field_marker {
impl cynic::schema::HasInputField<#field_marker> for super::super::#object_marker {
type Type = #field_type_marker;

const NAME: &'static str = #field_name_literal;
}

impl cynic::schema::HasInputField<#field_marker, #field_type_marker> for super::super::#object_marker {
}
});
}
}
80 changes: 16 additions & 64 deletions cynic-codegen/tests/snapshots/use_schema__books.graphql.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,53 @@ impl cynic::schema::NamedType for SubscriptionRoot {
pub mod __fields {
pub mod Book {
pub struct id;
impl cynic::schema::Field for id {
type Type = super::super::String;
const NAME: &'static str = "id";
}
impl cynic::schema::HasField<id> for super::super::Book {
type Type = super::super::String;
const NAME: &'static str = "id";
}
pub struct name;
impl cynic::schema::Field for name {
type Type = super::super::String;
const NAME: &'static str = "name";
}
impl cynic::schema::HasField<name> for super::super::Book {
type Type = super::super::String;
const NAME: &'static str = "name";
}
pub struct author;
impl cynic::schema::Field for author {
type Type = super::super::String;
const NAME: &'static str = "author";
}
impl cynic::schema::HasField<author> for super::super::Book {
type Type = super::super::String;
const NAME: &'static str = "author";
}
pub struct __typename;
impl cynic::schema::Field for __typename {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
impl cynic::schema::HasField<__typename> for super::super::Book {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
}
pub mod BookChanged {
pub struct mutationType;
impl cynic::schema::Field for mutationType {
type Type = super::super::MutationType;
const NAME: &'static str = "mutationType";
}
impl cynic::schema::HasField<mutationType> for super::super::BookChanged {
type Type = super::super::MutationType;
const NAME: &'static str = "mutationType";
}
pub struct id;
impl cynic::schema::Field for id {
type Type = super::super::ID;
const NAME: &'static str = "id";
}
impl cynic::schema::HasField<id> for super::super::BookChanged {
type Type = super::super::ID;
const NAME: &'static str = "id";
}
pub struct book;
impl cynic::schema::Field for book {
type Type = Option<super::super::Book>;
const NAME: &'static str = "book";
}
impl cynic::schema::HasField<book> for super::super::BookChanged {
type Type = Option<super::super::Book>;
const NAME: &'static str = "book";
}
pub struct __typename;
impl cynic::schema::Field for __typename {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
impl cynic::schema::HasField<__typename> for super::super::BookChanged {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
}
pub mod MutationRoot {
pub struct createBook;
impl cynic::schema::Field for createBook {
type Type = super::super::ID;
const NAME: &'static str = "createBook";
}
impl cynic::schema::HasField<createBook> for super::super::MutationRoot {
type Type = super::super::ID;
const NAME: &'static str = "createBook";
}
pub mod _create_book_arguments {
pub struct name;
Expand All @@ -118,12 +91,9 @@ pub mod __fields {
}
}
pub struct deleteBook;
impl cynic::schema::Field for deleteBook {
type Type = super::super::Boolean;
const NAME: &'static str = "deleteBook";
}
impl cynic::schema::HasField<deleteBook> for super::super::MutationRoot {
type Type = super::super::Boolean;
const NAME: &'static str = "deleteBook";
}
pub mod _delete_book_arguments {
pub struct id;
Expand All @@ -133,40 +103,28 @@ pub mod __fields {
}
}
pub struct __typename;
impl cynic::schema::Field for __typename {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
impl cynic::schema::HasField<__typename> for super::super::MutationRoot {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
}
pub mod QueryRoot {
pub struct books;
impl cynic::schema::Field for books {
type Type = Vec<super::super::Book>;
const NAME: &'static str = "books";
}
impl cynic::schema::HasField<books> for super::super::QueryRoot {
type Type = Vec<super::super::Book>;
const NAME: &'static str = "books";
}
pub struct __typename;
impl cynic::schema::Field for __typename {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
impl cynic::schema::HasField<__typename> for super::super::QueryRoot {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
}
pub mod SubscriptionRoot {
pub struct interval;
impl cynic::schema::Field for interval {
type Type = super::super::Int;
const NAME: &'static str = "interval";
}
impl cynic::schema::HasField<interval> for super::super::SubscriptionRoot {
type Type = super::super::Int;
const NAME: &'static str = "interval";
}
pub mod _interval_arguments {
pub struct n;
Expand All @@ -176,12 +134,9 @@ pub mod __fields {
}
}
pub struct books;
impl cynic::schema::Field for books {
type Type = super::super::BookChanged;
const NAME: &'static str = "books";
}
impl cynic::schema::HasField<books> for super::super::SubscriptionRoot {
type Type = super::super::BookChanged;
const NAME: &'static str = "books";
}
pub mod _books_arguments {
pub struct mutationType;
Expand All @@ -191,12 +146,9 @@ pub mod __fields {
}
}
pub struct __typename;
impl cynic::schema::Field for __typename {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
impl cynic::schema::HasField<__typename> for super::super::SubscriptionRoot {
type Type = super::super::String;
const NAME: &'static str = "__typename";
}
}
}
Expand Down
Loading

0 comments on commit 6953023

Please sign in to comment.