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
Embed support for #[SimpleObject], #[ComplexObject] #533
Comments
Can you provide a code to illustrate what you expect? |
For my use case if there's support for #[embed] then I can use this to make a macro that will generate field resolvers for receivers stored in a preloads container. But I could see this being used broadly for other use cases as well, for instance anytime you have a through table and need to decorate a struct with extra fields. |
Maybe |
I don't understand the purpose of this |
I think Bajix means Ex in serde: #[derive(Serialize, Deserialize)]
struct Pagination {
limit: u64,
offset: u64,
total: u64,
}
#[derive(Serialize, Deserialize)]
struct Users {
users: Vec<User>,
#[serde(flatten)]
pagination: Pagination,
} The output of serialization is: {
"limit": 100,
"offset": 200,
"total": 1053,
"users": [
{"id": "49824073-979f-4814-be10-5ea416ee1c2f", "username": "john_doe"},
...
]
} The fields of |
A good example use case is to have contextual nodes. For example, say we have Profile, and WorkspaceProfile, whereby Profile can be owned by a User or a Workspace, and a Workspace can have a number of Profiles using a through table to designate roles; should we embed Profile in WorkspaceProfile, then we build base resolvers in Profile and extend with contextual behavior in WorkspaceProfile. Without this, one is either to make wrapper field resolvers for each profile resolver, or else to nest the properties. Any time one would use a through table to add extra contextual fields, this is useful. This could be queried by joining profiles, workspaces, workspace_roles and including the role from workspace_roles, but generically this would apply any time you'd want to use a through table to include extra contextual columns as fields. use async_graphql::*;
#[derive(SimpleObject)]
pub struct WorkspaceProfile {
#[graphql(embed)]
profile: Profile,
role: Role
} |
There is already |
I also support this use case. @sunli829: we make an API where the Query object will have different fields enabled or disabled depending on which customer we're building the API for. Right now that means having a lot of It would indeed work the same as serde's flatten. |
@sunli829 I'd like to take a shot at implementing this. Do you have a recommendation on where I should start looking inside the derives? 😄 |
Now there is just a |
Released in SimpleObject: async-graphql/tests/simple_object.rs Line 13 in bfb454b
ComplexObject: async-graphql/tests/complex_object.rs Line 323 in bfb454b
Object: Line 16 in bfb454b
|
Works as advertised. You are a wizard. :D |
Currently there's no way to use wrapper types for output objects without redefining every field resolver or having a nested path. This makes type composition harder, especially when using libraries such as Diesel where extra fields cannot exist. It would be very useful if embed worked for output types and not just input types
The text was updated successfully, but these errors were encountered: