Skip to content

Strip Value from to_output() function for GraphQL scalars #1330

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

Merged
merged 17 commits into from
Jun 30, 2025

Conversation

tyranron
Copy link
Member

@tyranron tyranron commented Jun 18, 2025

Related to #1327

Synopsis

Counterpart of #1327 making the to_output() function returning ScalarValue directly instead of Value.

Solution

Now to_output() function should return a ScalarValue directly, instead of a Value.

Of course, the output type polymorphism is supported.

Either concrete type could be specified:

#[derive(Debug, GraphQLScalar)]
#[graphql(parse_token(String))]
struct TestComplexScalar;

impl TestComplexScalar {
    fn to_output(&self) -> &'static str {
        "SerializedValue"
    }
    // ...
}

Or a Displayable one:

#[graphql_scalar]
#[graphql(with = local_date, parse_token(String))]
pub type LocalDate = chrono::NaiveDate;

mod local_date {
    use std::fmt::Display;
    use super::LocalDate;

    pub(super) fn to_output(v: &LocalDate) -> impl Display {
        v.format("%Y-%m-%d")
    }
    // ....
}

Or just a generic:

    #[graphql_scalar]
    #[graphql(
        to_output_with = to_output,
        parse_token(prelude::String, i32),
    )]
    type StringOrInt = StringOrIntScalar;

    fn to_output<S: ScalarValue>(v: &StringOrInt) -> S {
        match v {
            StringOrInt::String(s) => S::from_displayable(s),
            StringOrInt::Int(i) => (*i).into(),
        }
    }
    // ...

@tyranron tyranron added this to the 0.17.0 milestone Jun 18, 2025
@tyranron tyranron self-assigned this Jun 18, 2025
@tyranron tyranron added enhancement Improvement of existing features or bugfix semver::breaking Breaking change in terms of SemVer k::api Related to API (application interface) k::refactor Refactoring, technical debt elimination and other improvements of existing code base labels Jun 18, 2025
@tyranron tyranron marked this pull request as ready for review June 25, 2025 17:19
@tyranron tyranron merged commit ffe8064 into master Jun 30, 2025
180 checks passed
@tyranron tyranron deleted the to-output-refactor branch June 30, 2025 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix k::api Related to API (application interface) k::refactor Refactoring, technical debt elimination and other improvements of existing code base semver::breaking Breaking change in terms of SemVer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant