-
Hello, having the following query: subscription transactionInitializeSession {
event {
... on TransactionInitializeSession {
data
sourceObject {
__typename
... on Checkout {
...CheckoutDetails
}
... on Order {
...OrderDetails
}
}
}
}
} And the following rust structs: #[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "TransactionInitializeSession")]
pub struct TransactionInitializeSession2 {
pub data: Option<Json>,
pub source_object: OrderOrCheckout,
}
//...
#[derive(cynic::Scalar, Debug, Clone)]
#[cynic(graphql_type = "JSON")]
pub struct Json(pub String); I get error
I assume this is because my data is
and not an actual double escaped stringify like:
Thus I would need to substitute the #[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub struct ChosenPaymentMethodData {
pub payment_method: PaymentMethodType,
} but then using it like: #[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "TransactionInitializeSession")]
pub struct TransactionInitializeSession2 {
pub data: Option<ChosenPaymentMethodData>,
pub source_object: OrderOrCheckout,
} gives error rustc: the trait bound `queries::event_transactions::ChosenPaymentMethodData: cynic::schema::IsScalar<queries::event_transactions::schema::JSON>` is not satisfied
the following other types implement trait `cynic::schema::IsScalar<SchemaType>`:
<bool as cynic::schema::IsScalar<bool>>
<i32 as cynic::schema::IsScalar<i32>>
<f64 as cynic::schema::IsScalar<f64>>
<std::boxed::Box<U> as cynic::schema::IsScalar<std::boxed::Box<T>>>
<std::borrow::Cow<'_, U> as cynic::schema::IsScalar<T>>
<cynic::Id as cynic::schema::IsScalar<cynic::Id>>
<std::vec::Vec<U> as cynic::schema::IsScalar<std::vec::Vec<T>>>
<std::string::String as cynic::schema::IsScalar<std::string::String>>
and 7 others
required for `std::option::Option<queries::event_transactions::ChosenPaymentMethodData>` to implement `cynic::schema::IsScalar<std::option::Option<queries::event_transactions::schema::JSON>>` [E0277] I can't define it as scalar cause that's not what it is, and I can't define it as a fragment cause then it complains that it's not in the schema as such. How can I solve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
So I'm guessing in the GraphQL schema this is defined as You've got a few options for handling this:
This would generally be the best option if the type of |
Beta Was this translation helpful? Give feedback.
So I'm guessing in the GraphQL schema this is defined as
JSON
, which is a scalar that represents arbitrary some arbitrary JSON object?You've got a few options for handling this:
ChosenPaymentMethodData
as you've done above, and then putcynic::impl_scalar!(ChosenPaymentMethodData, schema::JSON)
afterwards, and it should work. You can docynic::impl_scalar
on any type that implementsSerialize
&Deserialize
, and it'll become acceptable in positions expecting the given scalar. It's often better than derivingScalar
for that reason. Although it's worth noting that this will let you useChosenPaymentMethodData
on anyJSON
field, not just this one. So be careful with that.s…