Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the schema module #798

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
simplify the schema module
  • Loading branch information
obmarg committed Dec 30, 2023
commit 63e6194d54aee1427ff5e1613e18ff2f394a391f
11 changes: 8 additions & 3 deletions cynic-codegen/src/fragment_derive/fragment_impl.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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,
@@ -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>,
@@ -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,
@@ -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;
@@ -264,7 +269,7 @@ 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
>>::SchemaType
},
FieldKind::Enum => quote_spanned! { self.span =>
@@ -295,7 +300,7 @@ 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
>();

#alias
@@ -312,7 +317,7 @@ 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
>();

#alias
Original file line number Diff line number Diff line change
@@ -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());
Original file line number Diff line number Diff line change
@@ -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"))
Original file line number Diff line number Diff line change
@@ -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());
}
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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"))
6 changes: 1 addition & 5 deletions cynic-codegen/src/use_schema/fields.rs
Original file line number Diff line number Diff line change
@@ -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() {
5 changes: 1 addition & 4 deletions cynic-codegen/src/use_schema/input_object.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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";
}
}
}
Loading
Loading
Oops, something went wrong.