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

Allow flatten on arguments #1514

Open
ShaddyDC opened this issue Apr 24, 2024 · 1 comment
Open

Allow flatten on arguments #1514

ShaddyDC opened this issue Apr 24, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ShaddyDC
Copy link

Description of the feature

I've seen the flatten attribute in a bunch of places, but I don't think it's possible to add it to arguments yet. It would be useful to compose some structures better while keeping the API simple.
Let me know if it's already a thing and I missed it.

Code example (if possible)

#[derive(InputObject)]
struct MyData {
    a: String,
    b: String,
}

impl MyData {
    fn compute_hash(&self) -> String {
        let Self { a, b } = self;
        format!("{a}-{b}")
    }
}

#[Object]
impl QueryRoot {
    async fn test_1(&self, _ctx: &Context<'_>, #[graphql(flatten)] data: MyData) -> String {
        data.compute_hash()
    }
    async fn test_2(&self, _ctx: &Context<'_>, a: String, b: String) -> String {
        let data = MyData { a, b };
        data.compute_hash()
    }
}

Basically, I would like test_1 and test_2 to have the same calling interface, especially in cases where I've only got a single argument.
In simple cases like this, it's simple enough to just construct MyData inside the function, but for more complicated cases, it gets very verbose, and it also gets weird when you may have some copyable variables both inside MyData and also as a plain function argument floating around.

@ShaddyDC ShaddyDC added the enhancement New feature or request label Apr 24, 2024
@carlocorradini
Copy link

This is super useful! 🤯🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants