-
Notifications
You must be signed in to change notification settings - Fork 54
Description
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.