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

GraphQLObject field attribute like serde's serialize_with #780

Open
DusterTheFirst opened this issue Oct 8, 2020 · 2 comments
Open

GraphQLObject field attribute like serde's serialize_with #780

DusterTheFirst opened this issue Oct 8, 2020 · 2 comments
Assignees
Labels
enhancement Improvement of existing features or bugfix
Milestone

Comments

@DusterTheFirst
Copy link

DusterTheFirst commented Oct 8, 2020

When creating graphql objects using the GraphQLObject custom derive which have types that do not implement GraphQLType you end up being forced to use the #[juniper::object] proc macro and redefine methods to expose each and every field manually. It would be useful to have a #[graphql(transform = "fn_name")] field attribute that would allow users to transform the type into another ie. u64 (snowflake) to string.

example:

/// User id struct from library, so it is impossible to derive GraphQLType
pub struct UserId(pub u64);

#[derive(GraphQLObject)] 
struct GraphQLObj {
    normal_string: String,
    #[graphql(transform = "ToString::to_string")]
    custom_type: UserId
}

This improvement would probably be a better solution to #553.
could be a possible duplicate of #652

@DusterTheFirst DusterTheFirst added the enhancement Improvement of existing features or bugfix label Oct 8, 2020
@DusterTheFirst
Copy link
Author

DusterTheFirst commented Oct 8, 2020

Another solution could be to enable a way to make a type that implements Display to be usable as a GraphQLType

@tyranron
Copy link
Member

tyranron commented Jun 1, 2022

@DusterTheFirst we need a concrete Rust type to be named for building up a GraphQL schema by introspecting the Rust code given to macros.

Unfortunately, having UserId type and ToString::to_string ident of the function, it's impossible to name the transformation result type of applying this function to UserId in the current stable Rust. If we had sort of real typeof in Rust, it would be possible. But we have no ability to name a type out of an ident.

However, if we force users to name the result type, it will work:

#[derive(GraphQLObject)] 
struct GraphQLObj {
    normal_string: String,
    #[graphql(into(String) = ToString::to_string)]
    custom_type: UserId,
}

@tyranron tyranron self-assigned this Jun 1, 2022
@tyranron tyranron added this to the 0.16.0 milestone Jun 1, 2022
@tyranron tyranron mentioned this issue Jun 7, 2022
26 tasks
@tyranron tyranron modified the milestones: 0.16.0, 0.17.0 Oct 17, 2022
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
Projects
None yet
Development

No branches or pull requests

2 participants