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

Deserialize Account from its string representation #3476

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jvff
Copy link
Contributor

@jvff jvff commented Mar 5, 2025

Motivation

It is useful to be able to use an Account's string representation emitted by its Display implementation in a GraphQL request. However, that requires the type to be able to be deserialized from a string.

Proposal

Manually implement Deserialize for Account 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

  • These changes follow the usual release cycle, because it introduces a new backwards compatible feature.

Links

@jvff jvff added the bug Something isn't working label Mar 5, 2025
@jvff jvff added this to the Testnet #2 milestone Mar 5, 2025
@jvff jvff requested review from ma2bd and afck March 5, 2025 03:46
@jvff jvff self-assigned this Mar 5, 2025
@jvff jvff force-pushed the deserialize-account-from-string-representation branch 2 times, most recently from 52b1a86 to 3227652 Compare March 5, 2025 04:24
jvff added 5 commits March 5, 2025 04:35
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.
@jvff jvff force-pushed the deserialize-account-from-string-representation branch from 3227652 to fde71da Compare March 5, 2025 04:35
@@ -47,6 +48,36 @@ where
.expect("Flattening WIT roundtrip test failed");
}

/// Test GraphQL serialization roundtrip.
#[test_case(account_test_case(); "of_account")]
Copy link
Contributor

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.

Comment on lines +143 to +157
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()?);
}
}
}
Copy link
Contributor

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()?) { ... }

Copy link
Contributor Author

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 🤔

@afck
Copy link
Contributor

afck commented Mar 5, 2025

An alternative would be to just use strings if is_human_readable, like we do e.g. for Amount and other types. I think that would be a way simpler implementation?

@ma2bd
Copy link
Contributor

ma2bd commented Mar 6, 2025

An alternative would be to just use strings if is_human_readable, like we do e.g. for Amount and other types. I think that would be a way simpler implementation?

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 Account type is not amazing in the first place and we may have to revisit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

GraphQL cannot parse Account structure from its format string
4 participants