Skip to content
\n

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?

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

@twiclo juniper only calls resolvers, when they are actually requested. So if you query looks something like:

\n
query {\n    ticket(id: 3) {\n        ticketId\n    }\n}
\n

You don't need to execute Ticket::updates() in that case.

\n
\n

but I don't see how something like Ticket::updates() ever gets called.

\n
\n

To see updates you have to request it explicitly:

\n
query {\n    ticket(id: 3) {\n        ticketId\n        updates\n    }\n}
\n
\n
\n

so then how is this working?

\n
\n

So with GraphQL object this is pretty easy. Macros generate GraphQLValue/GraphQLValueAsync impls, where you have a simple match:

\n
// 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}
\n

juniper just parses query and calls resolve_field_async() for every requested field.

\n

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

","upvoteCount":2,"url":"https://github.com/graphql-rust/juniper/discussions/1100#discussioncomment-3533261"}}}
Discussion options

You must be logged in to vote

@twiclo juniper only calls resolvers, when they are actually requested. So if you query looks something like:

query {
    ticket(id: 3) {
        ticketId
    }
}

You don't need to execute Ticket::updates() in that case.

but I don't see how something like Ticket::updates() ever gets called.

To see updates you have to request it explicitly:

query {
    ticket(id: 3) {
        ticketId
        updates
    }
}

so then how is this working?

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
impl GraphQLValueAsync for Ticket {
    fn resolve_field_async(&self, field: &str)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ilslv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants