-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Deserialize Account
from its string representation
#3476
base: main
Are you sure you want to change the base?
Deserialize Account
from its string representation
#3476
Conversation
52b1a86
to
3227652
Compare
Prepare to be able to deserialize an `Account` from its `String` representation.
Allow the type to blend better with the GraphQL conventions.
Also deserialize an `Account` if it was serialized as a string.
Ensure it can be properly serialized to and deserialized from an `async_graphql::Value`.
Ensure that the alternative serialization format is supported.
3227652
to
fde71da
Compare
@@ -47,6 +48,36 @@ where | |||
.expect("Flattening WIT roundtrip test failed"); | |||
} | |||
|
|||
/// Test GraphQL serialization roundtrip. | |||
#[test_case(account_test_case(); "of_account")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there more types that could use these type of tests? Maybe we can enforce it? i.e. for example when we generate *.graphql
schemas we could run these tests over all types that are outputted there.
while let Some(key) = accessor.next_key()? { | ||
match key { | ||
AccountFieldDiscriminator::ChainId => { | ||
ensure!( | ||
chain_id.is_none(), | ||
Accessor::Error::duplicate_field("chain_id") | ||
); | ||
chain_id = Some(accessor.next_value()?); | ||
} | ||
AccountFieldDiscriminator::Owner => { | ||
ensure!(owner.is_none(), Accessor::Error::duplicate_field("owner")); | ||
owner = Some(accessor.next_value()?); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively sth like:
match (accessor.next_key()?, accessor.next_key()?) { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that's possible, because I think the API requires a call to next_value
after the next_key
if you want that entry 🤔
An alternative would be to just use strings if |
Yes, please. Binary formats for composed types should follow Serde. For text (aka human-readable) formats, I would directly write the parser/printer (without the Serde visitor framework). Also, this |
Motivation
It is useful to be able to use an
Account
's string representation emitted by itsDisplay
implementation in a GraphQL request. However, that requires the type to be able to be deserialized from a string.Proposal
Manually implement
Deserialize
forAccount
so that it can also be deserialized from its string representation.Test Plan
Added unit tests for the roundtrip serialization of
Account
to GraphQL, in both its object and string representations.Release Plan
Links
Account
structure from its format string #3462