Skip to content

Commit 63e6194

Browse files
committed
simplify the schema module
1 parent 9521d56 commit 63e6194

15 files changed

+1126
-5005
lines changed

cynic-codegen/src/fragment_derive/fragment_impl.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum Selection<'a> {
3737
}
3838

3939
struct FieldSelection<'a> {
40+
object_marker_type: syn::Path,
4041
rust_field_type: syn::Type,
4142
field_marker_type_path: syn::Path,
4243
graphql_field_kind: FieldKind,
@@ -90,6 +91,7 @@ impl<'schema, 'a: 'schema> FragmentImpl<'schema, 'a> {
9091
schema,
9192
field,
9293
schema_field.as_ref(),
94+
&schema_type_path,
9395
&field_module_path,
9496
schema_module_path,
9597
variables_fields,
@@ -119,6 +121,7 @@ fn process_field<'a>(
119121
schema: &'a Schema<'a, Unvalidated>,
120122
field: &FragmentDeriveField,
121123
schema_field: Option<&'a Field<'a>>,
124+
object_marker_type: &syn::Path,
122125
field_module_path: &syn::Path,
123126
schema_module_path: &syn::Path,
124127
variables_fields: Option<&syn::Path>,
@@ -151,6 +154,7 @@ fn process_field<'a>(
151154
let field_marker_type_path = schema_field.marker_ident().to_path(field_module_path);
152155

153156
Ok(Selection::Field(FieldSelection {
157+
object_marker_type: object_marker_type.clone(),
154158
rust_field_type: field.ty.clone(),
155159
arguments,
156160
field_marker_type_path,
@@ -224,6 +228,7 @@ impl quote::ToTokens for FieldSelection<'_> {
224228
fn to_tokens(&self, tokens: &mut TokenStream) {
225229
use quote::TokenStreamExt;
226230

231+
let object_marker_type = &self.object_marker_type;
227232
let field_marker_type_path = &self.field_marker_type_path;
228233
let field_type = &self.rust_field_type;
229234
let arguments = &self.arguments;
@@ -264,7 +269,7 @@ impl quote::ToTokens for FieldSelection<'_> {
264269
}
265270
FieldKind::Scalar => quote_spanned! { self.span =>
266271
<#aligned_type as cynic::schema::IsScalar<
267-
<#field_marker_type_path as cynic::schema::Field>::Type
272+
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
268273
>>::SchemaType
269274
},
270275
FieldKind::Enum => quote_spanned! { self.span =>
@@ -295,7 +300,7 @@ impl quote::ToTokens for FieldSelection<'_> {
295300
.select_flattened_field::<
296301
#field_marker_type_path,
297302
#schema_type_lookup,
298-
<#field_marker_type_path as cynic::schema::Field>::Type,
303+
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
299304
>();
300305

301306
#alias
@@ -312,7 +317,7 @@ impl quote::ToTokens for FieldSelection<'_> {
312317
.select_flattened_field::<
313318
#field_marker_type_path,
314319
#schema_type_lookup,
315-
<#field_marker_type_path as cynic::schema::Field>::Type,
320+
<#object_marker_type as cynic::schema::HasField<#field_marker_type_path>>::Type
316321
>();
317322

318323
#alias

cynic-codegen/src/fragment_derive/snapshots/cynic_codegen__fragment_derive__tests__feature_flagging.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl cynic::QueryFragment for MyQuery {
1313
#![allow(unused_mut)]
1414
let mut field_builder = builder
1515
.select_field::<schema::__fields::Query::__typename, <String as cynic::schema::IsScalar<
16-
<schema::__fields::Query::__typename as cynic::schema::Field>::Type,
17-
>>::SchemaType>();
16+
<schema::Query as cynic::schema::HasField<schema::__fields::Query::__typename>>::Type,
17+
>>::SchemaType>();
1818
if builder.is_feature_enabled("2018") {
1919
let mut field_builder = builder . select_field :: < schema :: __fields :: Query :: filteredPosts , < Vec < BlogPostOutput > as cynic :: QueryFragment > :: SchemaType > () ;
2020
<Vec<BlogPostOutput> as cynic::QueryFragment>::query(field_builder.select_children());

cynic-codegen/src/fragment_derive/snapshots/cynic_codegen__fragment_derive__tests__flatten_attr.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl cynic::QueryFragment for Film {
1111
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
1212
) {
1313
#![allow(unused_mut)]
14-
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 , > () ;
14+
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 > () ;
1515
}
1616
fn name() -> Option<std::borrow::Cow<'static, str>> {
1717
Some(std::borrow::Cow::Borrowed("Film"))

cynic-codegen/src/fragment_derive/snapshots/cynic_codegen__fragment_derive__tests__simple_struct.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl cynic::QueryFragment for BlogPostOutput {
1111
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
1212
) {
1313
#![allow(unused_mut)]
14-
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 > () ;
14+
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 > () ;
1515
let mut field_builder = builder . select_field :: < schema :: __fields :: BlogPost :: author , < AuthorOutput as cynic :: QueryFragment > :: SchemaType > () ;
1616
<AuthorOutput as cynic::QueryFragment>::query(field_builder.select_children());
1717
}

cynic-codegen/src/fragment_derive/snapshots/cynic_codegen__fragment_derive__tests__spread_attr_multi_field1.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl cynic::QueryFragment for Film {
1111
mut builder: cynic::queries::SelectionBuilder<'_, Self::SchemaType, Self::VariablesFields>,
1212
) {
1313
#![allow(unused_mut)]
14-
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 > () ;
14+
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 > () ;
1515
<FilmDetails as cynic::QueryFragment>::query(
1616
builder
1717
.inline_fragment()

cynic-codegen/src/fragment_derive/snapshots/cynic_codegen__fragment_derive__tests__spread_attr_multi_field2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl cynic::QueryFragment for Film {
1616
.inline_fragment()
1717
.select_children::<<FilmDetails as cynic::QueryFragment>::VariablesFields>(),
1818
);
19-
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 > () ;
19+
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 > () ;
2020
}
2121
fn name() -> Option<std::borrow::Cow<'static, str>> {
2222
Some(std::borrow::Cow::Borrowed("Film"))

cynic-codegen/src/use_schema/fields.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ impl ToTokens for FieldOutput<'_> {
3030
tokens.append_all(quote! {
3131
pub struct #field_marker;
3232

33-
impl cynic::schema::Field for #field_marker{
33+
impl cynic::schema::HasField<#field_marker> for super::super::#parent_marker {
3434
type Type = #field_type_marker;
3535

3636
const NAME: &'static str = #field_name_literal;
3737
}
38-
39-
impl cynic::schema::HasField<#field_marker> for super::super::#parent_marker {
40-
type Type = #field_type_marker;
41-
}
4238
});
4339

4440
if !self.field.arguments.is_empty() {

cynic-codegen/src/use_schema/input_object.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,11 @@ impl ToTokens for FieldOutput<'_> {
6565
tokens.append_all(quote! {
6666
pub struct #field_marker;
6767

68-
impl cynic::schema::Field for #field_marker {
68+
impl cynic::schema::HasInputField<#field_marker> for super::super::#object_marker {
6969
type Type = #field_type_marker;
7070

7171
const NAME: &'static str = #field_name_literal;
7272
}
73-
74-
impl cynic::schema::HasInputField<#field_marker, #field_type_marker> for super::super::#object_marker {
75-
}
7673
});
7774
}
7875
}

cynic-codegen/tests/snapshots/use_schema__books.graphql.snap

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,80 +30,53 @@ impl cynic::schema::NamedType for SubscriptionRoot {
3030
pub mod __fields {
3131
pub mod Book {
3232
pub struct id;
33-
impl cynic::schema::Field for id {
34-
type Type = super::super::String;
35-
const NAME: &'static str = "id";
36-
}
3733
impl cynic::schema::HasField<id> for super::super::Book {
3834
type Type = super::super::String;
35+
const NAME: &'static str = "id";
3936
}
4037
pub struct name;
41-
impl cynic::schema::Field for name {
42-
type Type = super::super::String;
43-
const NAME: &'static str = "name";
44-
}
4538
impl cynic::schema::HasField<name> for super::super::Book {
4639
type Type = super::super::String;
40+
const NAME: &'static str = "name";
4741
}
4842
pub struct author;
49-
impl cynic::schema::Field for author {
50-
type Type = super::super::String;
51-
const NAME: &'static str = "author";
52-
}
5343
impl cynic::schema::HasField<author> for super::super::Book {
5444
type Type = super::super::String;
45+
const NAME: &'static str = "author";
5546
}
5647
pub struct __typename;
57-
impl cynic::schema::Field for __typename {
58-
type Type = super::super::String;
59-
const NAME: &'static str = "__typename";
60-
}
6148
impl cynic::schema::HasField<__typename> for super::super::Book {
6249
type Type = super::super::String;
50+
const NAME: &'static str = "__typename";
6351
}
6452
}
6553
pub mod BookChanged {
6654
pub struct mutationType;
67-
impl cynic::schema::Field for mutationType {
68-
type Type = super::super::MutationType;
69-
const NAME: &'static str = "mutationType";
70-
}
7155
impl cynic::schema::HasField<mutationType> for super::super::BookChanged {
7256
type Type = super::super::MutationType;
57+
const NAME: &'static str = "mutationType";
7358
}
7459
pub struct id;
75-
impl cynic::schema::Field for id {
76-
type Type = super::super::ID;
77-
const NAME: &'static str = "id";
78-
}
7960
impl cynic::schema::HasField<id> for super::super::BookChanged {
8061
type Type = super::super::ID;
62+
const NAME: &'static str = "id";
8163
}
8264
pub struct book;
83-
impl cynic::schema::Field for book {
84-
type Type = Option<super::super::Book>;
85-
const NAME: &'static str = "book";
86-
}
8765
impl cynic::schema::HasField<book> for super::super::BookChanged {
8866
type Type = Option<super::super::Book>;
67+
const NAME: &'static str = "book";
8968
}
9069
pub struct __typename;
91-
impl cynic::schema::Field for __typename {
92-
type Type = super::super::String;
93-
const NAME: &'static str = "__typename";
94-
}
9570
impl cynic::schema::HasField<__typename> for super::super::BookChanged {
9671
type Type = super::super::String;
72+
const NAME: &'static str = "__typename";
9773
}
9874
}
9975
pub mod MutationRoot {
10076
pub struct createBook;
101-
impl cynic::schema::Field for createBook {
102-
type Type = super::super::ID;
103-
const NAME: &'static str = "createBook";
104-
}
10577
impl cynic::schema::HasField<createBook> for super::super::MutationRoot {
10678
type Type = super::super::ID;
79+
const NAME: &'static str = "createBook";
10780
}
10881
pub mod _create_book_arguments {
10982
pub struct name;
@@ -118,12 +91,9 @@ pub mod __fields {
11891
}
11992
}
12093
pub struct deleteBook;
121-
impl cynic::schema::Field for deleteBook {
122-
type Type = super::super::Boolean;
123-
const NAME: &'static str = "deleteBook";
124-
}
12594
impl cynic::schema::HasField<deleteBook> for super::super::MutationRoot {
12695
type Type = super::super::Boolean;
96+
const NAME: &'static str = "deleteBook";
12797
}
12898
pub mod _delete_book_arguments {
12999
pub struct id;
@@ -133,40 +103,28 @@ pub mod __fields {
133103
}
134104
}
135105
pub struct __typename;
136-
impl cynic::schema::Field for __typename {
137-
type Type = super::super::String;
138-
const NAME: &'static str = "__typename";
139-
}
140106
impl cynic::schema::HasField<__typename> for super::super::MutationRoot {
141107
type Type = super::super::String;
108+
const NAME: &'static str = "__typename";
142109
}
143110
}
144111
pub mod QueryRoot {
145112
pub struct books;
146-
impl cynic::schema::Field for books {
147-
type Type = Vec<super::super::Book>;
148-
const NAME: &'static str = "books";
149-
}
150113
impl cynic::schema::HasField<books> for super::super::QueryRoot {
151114
type Type = Vec<super::super::Book>;
115+
const NAME: &'static str = "books";
152116
}
153117
pub struct __typename;
154-
impl cynic::schema::Field for __typename {
155-
type Type = super::super::String;
156-
const NAME: &'static str = "__typename";
157-
}
158118
impl cynic::schema::HasField<__typename> for super::super::QueryRoot {
159119
type Type = super::super::String;
120+
const NAME: &'static str = "__typename";
160121
}
161122
}
162123
pub mod SubscriptionRoot {
163124
pub struct interval;
164-
impl cynic::schema::Field for interval {
165-
type Type = super::super::Int;
166-
const NAME: &'static str = "interval";
167-
}
168125
impl cynic::schema::HasField<interval> for super::super::SubscriptionRoot {
169126
type Type = super::super::Int;
127+
const NAME: &'static str = "interval";
170128
}
171129
pub mod _interval_arguments {
172130
pub struct n;
@@ -176,12 +134,9 @@ pub mod __fields {
176134
}
177135
}
178136
pub struct books;
179-
impl cynic::schema::Field for books {
180-
type Type = super::super::BookChanged;
181-
const NAME: &'static str = "books";
182-
}
183137
impl cynic::schema::HasField<books> for super::super::SubscriptionRoot {
184138
type Type = super::super::BookChanged;
139+
const NAME: &'static str = "books";
185140
}
186141
pub mod _books_arguments {
187142
pub struct mutationType;
@@ -191,12 +146,9 @@ pub mod __fields {
191146
}
192147
}
193148
pub struct __typename;
194-
impl cynic::schema::Field for __typename {
195-
type Type = super::super::String;
196-
const NAME: &'static str = "__typename";
197-
}
198149
impl cynic::schema::HasField<__typename> for super::super::SubscriptionRoot {
199150
type Type = super::super::String;
151+
const NAME: &'static str = "__typename";
200152
}
201153
}
202154
}

0 commit comments

Comments
 (0)