examples icon indicating copy to clipboard operation
examples copied to clipboard

Example of throwing error for missing token via GraphQLResponse

Open rex-remind101 opened this issue 4 years ago • 1 comments

Hi, if the token is missing I'd like to throw an error by adding it to the errors payload in the response. This example doesn't demonstrate that, do you have another example or could one be added please? Thank you.

rex-remind101 avatar Dec 02 '21 20:12 rex-remind101

Do it like this:

async fn index(
    schema: web::Data<TokenSchema>,
    req: HttpRequest,
    gql_request: GraphQLRequest,
) -> Result<GraphQLResponse> {
    let mut request = gql_request.into_inner();
    if let Some(token) = get_token_from_headers(req.headers()) {
        request = request.data(token);
    } else {
        return Err(actix_web::error::ErrorUnauthorized("unauthorized"));
    }
    Ok(schema.execute(request).await.into())
}

sunli829 avatar Dec 03 '21 01:12 sunli829