oliverjumpertz/axum-graphql

I do have problem while tryng chaining graphql_handler function to Router

Closed this issue · 6 comments

Just before testing graphql_playground with axum server [main.rs]

...

#[tokio::main]
async fn main() {
    let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();

    let app = Router::new()
        .route("/", get(graphql_playground).post(graphql_handler))
        .route("/health", get(health))
        .layer(Extension(schema)); // (1)

...
}

I got compile error:

error[E0277]: the trait bound `fn(GraphQLRequest, axum::Extension<async_graphql::Schema<QueryRoot, EmptyMutation, EmptySubscription>>) -> impl std::future::Future<Output = GraphQLResponse> {graphql_handler}: Handler<_, _, _>` is not satisfied
   --> src/main.rs:15:50
    |
15  |         .route("/", get(graphql_playground).post(graphql_handler))
    |                                             ---- ^^^^^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(GraphQLRequest, axum::Extension<async_graphql::Schema<QueryRoot, EmptyMutation, EmptySubscription>>) -> impl std::future::Future<Output = GraphQLResponse> {graphql_handler}`
    |                                             |
    |                                             required by a bound introduced by this call

Can you pls explain?

It was an errata in tutorial: https://oliverjumpertz.com/blog/how-to-build-a-powerful-graphql-api-with-rust/

Consuming GraphqlRequest argument req in the fn graphql_handler() was occasionally mentioned first:
изображение

Easy to fix it!
PS: Many tnx for tutorial, btw!

Hey, happy you figured it out! :) Closing this then.

@ayratmsk I don't understand. I am having the same issue. What did you do to fix it?

@ayratmsk I don't understand. I am having the same issue. What did you do to fix it?

Check async graphql_handler() signature!
You should place "Extension(schema): Extension" parameter first, because "req: GraphQLRequest" is consuming one.

@ayratmsk I don't understand. I am having the same issue. What did you do to fix it?

What basically happens is that after you place the actual Axum request, you can add no more extractors (Extension is a so-called extractor). I have just fixed the order in the article. I had forgotten to push that!

Check async graphql_handler() signature!
You should place "Extension(schema): Extension" parameter first, because "req: GraphQLRequest" is consuming one.

Thanks @ayratmsk

Great tutorial by the way @oliverjumpertz - look forward to seeing more rust ones :)