When I request a ticket it calls to Ticket::get_by_id() but I don't see how something like Ticket::updates() ever gets called. get_by_id doesn't call to it. Neither does Query::ticket() so then how is this working?
@twiclo juniper only calls resolvers, when they are actually requested. So if you query looks something like:
query {\n ticket(id: 3) {\n ticketId\n }\n}You don't need to execute Ticket::updates() in that case.
\n\nbut I don't see how something like Ticket::updates() ever gets called.
\n
To see updates you have to request it explicitly:
query {\n ticket(id: 3) {\n ticketId\n updates\n }\n}\n\nso then how is this working?
\n
So with GraphQL object this is pretty easy. Macros generate GraphQLValue/GraphQLValueAsync impls, where you have a simple match:
// Very simplified, just to give an idea\nimpl GraphQLValueAsync for Ticket {\n fn resolve_field_async(&self, field: &str) -> BoxFuture<ExecutionResult> {\n async {\n match field {\n \"ticketId\" => self.ticket_id().as_graphql_value(),\n \"updates\" => self.updates().await?.as_graphql_value(),\n _ => Err(\"unknown field\")\n }\n }\n .boxed_local()\n }\n}juniper just parses query and calls resolve_field_async() for every requested field.
With GraphQL interfaces this is whole another story, which is way more complicated and involves compile-time reflection and const panics for error messages. You can dive deeper by reading our discussions around the implementation: #1009
-
|
Say I have this setup with actix_web: When I request a ticket it calls to |
Beta Was this translation helpful? Give feedback.
-
|
@twiclo query {
ticket(id: 3) {
ticketId
}
}You don't need to execute
To see query {
ticket(id: 3) {
ticketId
updates
}
}
So with // Very simplified, just to give an idea
impl GraphQLValueAsync for Ticket {
fn resolve_field_async(&self, field: &str) -> BoxFuture<ExecutionResult> {
async {
match field {
"ticketId" => self.ticket_id().as_graphql_value(),
"updates" => self.updates().await?.as_graphql_value(),
_ => Err("unknown field")
}
}
.boxed_local()
}
}
With |
Beta Was this translation helpful? Give feedback.
@twiclo
juniperonly calls resolvers, when they are actually requested. So if you query looks something like:You don't need to execute
Ticket::updates()in that case.To see
updatesyou have to request it explicitly:So with
GraphQLobject this is pretty easy. Macros generateGraphQLValue/GraphQLValueAsyncimpls, where you have a simple match: