graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Setup a post_graphql to use RequestBuilder in addition to reqwest::Client

Open hgm-king opened this issue 4 years ago • 2 comments

Background

Cheers, I am working on an API that needs to communicate with external graphql services. These services require that I include headers with keys for authorization. I have my reqwest::Client in a global scope, and all the api requests share a reference to it. I definitely cannot mutate it, and I wouldn't want to update it in the same manner all of the examples in this crate use. That is, build a unique client every single time I send out a request.

Also, as I am a new Ruster, please let me know if building a new RequestBuilder is comparable to building a new Client with every response handler in my API.

Suggestion

In my mind, it would be nice to be able to send through a RequestBuilder into the post_graphql method that I use, instead of sending through the client that is mutated to set the headers on all requests.

Example

Imagine I have this function:

pub async fn create_product(
    client: Arc<Client>,
    access_token: String,
)  {
    // it would be nice to setup headers here, but I cannot with my Arc<Client> (its not mutable).
    // Maybe I could make a client.request() that I can toss headers and the like onto?
    // then pass my requestBuilder into the method below
    let response_body = post_graphql::<Product, _>(&client, uri, variables).await?;
}

Proposal

What if we were to make a new method, in addition to the post_graphql method with the following signature(not really sure what to name it but I dont care)?

pub async fn post_graphql_prime<Q: GraphQLQuery, U: reqwest::IntoUrl>(
    builder: &reqwest::RequestBuilder,
    url: U,
    variables: Q::Variables,
) -> Result<crate::Response<Q::ResponseData>, reqwest::Error>

hgm-king avatar Aug 06 '21 01:08 hgm-king

Sounds good to me :+1: I went for the simplest possible implementation since I'm not using any of this code personally. It sounds like a very reasonable addition under the reqwest feature flag.

tomhoule avatar Aug 16 '21 06:08 tomhoule

For the name, since the method may already be set in the RequestBuilder, maybe something to do with body?

tomhoule avatar Aug 16 '21 06:08 tomhoule