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

is it possible get db client in post handler? #62

Open
swaitw opened this issue Nov 8, 2022 · 0 comments
Open

is it possible get db client in post handler? #62

swaitw opened this issue Nov 8, 2022 · 0 comments

Comments

@swaitw
Copy link

swaitw commented Nov 8, 2022

I have some code as below:

// shchema

   let schema = Schema::build(Query::default(), Mutation::default(), EmptySubscription)
        .data(db)
        .data(jwt_key) 
        .finish();

// handler

pub async fn graphql_handler(
    schema: Extension<Schema<Query, Mutation, EmptySubscription>>,
    req: GraphQLRequest,
    headers: HeaderMap,
    db: Extension<Arc<PrismaClient>>, // from handler Extention 
    jwt_key: Extension<Arc<HS256Key>>, // from handler Extention 
) -> GraphQLResponse {
   
    let mut req = req.into_inner();
    let operation_name = req.operation_name.clone();
    match operation_name.as_deref() {
        Some("login") | Some("register") => {
           req = get_token_and_user_from_headers(&headers,&db,req).await; 
        } 
        _ => {}
    }

// try to attached db and jwt_key to context, but not work for me, i cant find by their type.
    req = req.data(db).data(jwt_key);
    schema.execute(req).await.into()
}

i want check the token and insert current user into context if the operation is one of "login" and “register”, in that way i will need get the db-client in graphql handler to run get current user query. but i dont knwo how can i get the db-client which attached when build the shchem.

i also tried to attached the db-client to an Extension and attachd it to graphql Request in the handler . but it not work for me, i cant get them back from context by the client type, may be i did not use the right type.

so I have to attach the db-client to an handler Extention and also push it into the shchema when build it. in that way i can use the db-client from extention to check the token user and use the db-clinet from context in resover.

just wondering is there anyway we can get the db-client pushed in schema in the handler or we can push db-client from extention to graphql-request in hander. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant