Skip to content

Commit a7455ae

Browse files
committed
fixed some more stuff
1 parent 94e5b47 commit a7455ae

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

cynic-codegen/src/fragment_derive/fragment_impl.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ impl quote::ToTokens for FieldSelection<'_> {
286286
}
287287
}
288288
FieldKind::Scalar if self.flatten => quote_spanned! { self.span =>
289-
<#aligned_type as cynic::schema::IsScalar<#aligned_type>>::SchemaType
289+
<#aligned_type as cynic::schema::IsOutputScalar<#aligned_type>>::SchemaType
290290
},
291291
FieldKind::Scalar => quote_spanned! { self.span =>
292-
<#aligned_type as cynic::schema::IsScalar<
292+
<#aligned_type as cynic::schema::IsOutputScalar<
293293
<#field_marker_type_path as cynic::schema::Field>::Type
294294
>>::SchemaType
295295
},
@@ -409,3 +409,12 @@ impl quote::ToTokens for SpreadSelection {
409409
})
410410
}
411411
}
412+
413+
impl OutputType<'_> {
414+
fn is_composite(&self) -> bool {
415+
matches!(
416+
self,
417+
OutputType::Object(_) | OutputType::Interface(_) | OutputType::Union(_)
418+
)
419+
}
420+
}

cynic-codegen/src/scalar_derive/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ pub fn scalar_derive_impl(input: ScalarDeriveInput) -> Result<TokenStream, syn::
9494
}
9595

9696
#[automatically_derived]
97-
impl #impl_generics cynic::schema::ScalarDeser<#marker_ident> for #ident #ty_generics #where_clause {
97+
impl #impl_generics cynic::schema::IsOutputScalar<#marker_ident> for #ident #ty_generics #where_clause {
98+
type SchemaType = #marker_ident;
99+
98100
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
99101
where
100102
D: cynic::serde::Deserializer<'de>

cynic/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ macro_rules! impl_scalar {
247247
}
248248

249249
#[automatically_derived]
250-
impl $crate::schema::ScalarDeser<$schema_module$(::$schema_module_rest)*::$type_lock> for $type {
250+
impl $crate::schema::IsOutputScalar<$schema_module$(::$schema_module_rest)*::$type_lock> for $type {
251+
type SchemaType = $schema_module$(::$schema_module_rest)*::$type_lock;
252+
251253
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
252254
where
253255
D: $crate::serde::Deserializer<'de>

cynic/src/private/scalar_de.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::marker::PhantomData;
22

33
use serde::Deserialize;
44

5-
use crate::schema::{IsScalar, ScalarDeser};
5+
use crate::schema::{IsOutputScalar, IsScalar};
66

77
pub struct ScalarDeseralize<T, U> {
88
inner: T,
@@ -17,13 +17,13 @@ impl<T, U> ScalarDeseralize<T, U> {
1717

1818
impl<'de, T, U> Deserialize<'de> for ScalarDeseralize<T, U>
1919
where
20-
T: ScalarDeser<U>,
20+
T: IsOutputScalar<U>,
2121
{
2222
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2323
where
2424
D: serde::Deserializer<'de>,
2525
{
26-
let inner = <T as ScalarDeser<U>>::deserialize(deserializer)?;
26+
let inner = <T as IsOutputScalar<U>>::deserialize(deserializer)?;
2727

2828
Ok(ScalarDeseralize {
2929
inner,

cynic/src/schema.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,18 @@ pub trait IsScalar<SchemaType> {
6363
/// The schema marker type this scalar represents.
6464
type SchemaType;
6565

66+
// TODO: serialize should maybe be on an OutputScalar trait
6667
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
6768
where
6869
S: Serializer;
6970
}
7071

71-
// TODO: better name on this
72-
pub trait ScalarDeser<SchemaType>: IsScalar<SchemaType> + Sized {
72+
// TODO: serialize should maybe be on an InputScalar trait
73+
// or maybe just ScalarSerialize/ScalarDeserialize? not sure...
74+
pub trait IsOutputScalar<SchemaType>: Sized {
75+
/// The schema marker type this scalar represents.
76+
type SchemaType;
77+
7378
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
7479
where
7580
D: Deserializer<'de>;
@@ -103,10 +108,12 @@ where
103108
}
104109
}
105110

106-
impl<T, U> ScalarDeser<Option<T>> for Option<U>
111+
impl<T, U> IsOutputScalar<Option<T>> for Option<U>
107112
where
108-
U: ScalarDeser<T>,
113+
U: IsOutputScalar<T>,
109114
{
115+
type SchemaType = Option<U::SchemaType>;
116+
110117
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
111118
where
112119
D: Deserializer<'de>,
@@ -129,10 +136,12 @@ where
129136
}
130137
}
131138

132-
impl<T, U> ScalarDeser<Vec<T>> for Vec<U>
139+
impl<T, U> IsOutputScalar<Vec<T>> for Vec<U>
133140
where
134-
U: ScalarDeser<T>,
141+
U: IsOutputScalar<T>,
135142
{
143+
type SchemaType = Vec<U::SchemaType>;
144+
136145
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
137146
where
138147
D: Deserializer<'de>,
@@ -183,10 +192,12 @@ where
183192
}
184193
}
185194

186-
impl<T, U: ?Sized> ScalarDeser<Box<T>> for Box<U>
195+
impl<T, U: ?Sized> IsOutputScalar<Box<T>> for Box<U>
187196
where
188-
U: ScalarDeser<T>,
197+
U: IsOutputScalar<T>,
189198
{
199+
type SchemaType = Box<U::SchemaType>;
200+
190201
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
191202
where
192203
D: Deserializer<'de>,
@@ -209,10 +220,12 @@ where
209220
}
210221
}
211222

212-
impl<T, U: ?Sized> ScalarDeser<T> for std::borrow::Cow<'_, U>
223+
impl<T, U: ?Sized> IsOutputScalar<T> for std::borrow::Cow<'_, U>
213224
where
214-
U: ScalarDeser<T> + ToOwned,
225+
U: IsOutputScalar<T> + ToOwned,
215226
{
227+
type SchemaType = U::SchemaType;
228+
216229
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
217230
where
218231
D: Deserializer<'de>,
@@ -232,7 +245,9 @@ impl IsScalar<bool> for bool {
232245
}
233246
}
234247

235-
impl ScalarDeser<bool> for bool {
248+
impl IsOutputScalar<bool> for bool {
249+
type SchemaType = bool;
250+
236251
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
237252
where
238253
D: Deserializer<'de>,
@@ -252,7 +267,9 @@ impl IsScalar<String> for String {
252267
}
253268
}
254269

255-
impl ScalarDeser<String> for String {
270+
impl IsOutputScalar<String> for String {
271+
type SchemaType = String;
272+
256273
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
257274
where
258275
D: Deserializer<'de>,
@@ -283,7 +300,9 @@ impl IsScalar<i32> for i32 {
283300
}
284301
}
285302

286-
impl ScalarDeser<i32> for i32 {
303+
impl IsOutputScalar<i32> for i32 {
304+
type SchemaType = i32;
305+
287306
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
288307
where
289308
D: Deserializer<'de>,
@@ -303,7 +322,9 @@ impl IsScalar<f64> for f64 {
303322
}
304323
}
305324

306-
impl ScalarDeser<f64> for f64 {
325+
impl IsOutputScalar<f64> for f64 {
326+
type SchemaType = f64;
327+
307328
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
308329
where
309330
D: Deserializer<'de>,
@@ -323,7 +344,9 @@ impl IsScalar<crate::Id> for crate::Id {
323344
}
324345
}
325346

326-
impl ScalarDeser<crate::Id> for crate::Id {
347+
impl IsOutputScalar<crate::Id> for crate::Id {
348+
type SchemaType = crate::Id;
349+
327350
fn deserialize<'de, D>(deserializer: D) -> Result<Self, D::Error>
328351
where
329352
D: Deserializer<'de>,

cynic/tests/mutation_generics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub struct SignIn {
4040
variables = "SignInVariablesMoreGeneric",
4141
schema_path = "../schemas/raindancer.graphql"
4242
)]
43-
pub struct SignInMoreGeneric<SI: cynic::schema::IsScalar<String>>
43+
pub struct SignInMoreGeneric<SI: cynic::schema::IsOutputScalar<String>>
4444
where
45-
<SI as cynic::schema::IsScalar<String>>::SchemaType: cynic::queries::IsFieldType<String>,
45+
<SI as cynic::schema::IsOutputScalar<String>>::SchemaType: cynic::queries::IsFieldType<String>,
4646
{
4747
#[arguments(input: $input)]
4848
pub sign_in: SI,

cynic/tests/scalars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde::Serialize;
1+
use serde::{de::DeserializeOwned, Serialize};
22

33
mod schema {
44
cynic::use_schema!("tests/test-schema.graphql");
@@ -13,7 +13,7 @@ cynic::impl_scalar!(chrono::DateTime<chrono::Utc>, schema::DateTime);
1313

1414
#[derive(cynic::Scalar)]
1515
#[cynic(graphql_type = "DateTime")]
16-
pub struct DateTimeInner<DT: Serialize>(pub DT);
16+
pub struct DateTimeInner<DT: Serialize + DeserializeOwned>(pub DT);
1717

1818
// Make sure we can use impl scalar on built in types
1919
pub struct MyString(#[allow(dead_code)] String);

0 commit comments

Comments
 (0)