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

Cohesiveness between GraphQLQuery struct, its Variables and ResponseData

Open tomasol opened this issue 5 years ago • 2 comments

With current APIs it is possible to have mismatch between the struct, Variables and ResponseData . Example from the README.md:

    let request_body = UnionQuery::build_query(variables);
    let client = reqwest::Client::new();
    let mut res = client.post("/graphql").json(&request_body).send()?;
    let response_body: Response<union_query::ResponseData> = res.json()?;

The cast in the last line can be replaced with any other ResponseData without a compilation error.

I would like to know if there is interest in fixing the issue. I've prototyped a simple trait that solves the issue for me:

let response_body = UnionQuery::execute(variables, &client)?;

The client is actually a trait so it is not coupled with reqwest. I will add a PR with draft of the proposed API.

tomasol avatar Aug 26 '20 21:08 tomasol

The ResponseData is an associated type on the Query trait (as well as variales), so the clients can enforce that all three match. Something like https://github.com/graphql-rust/graphql-client/pull/338 seems like the right direction to me. I would avoid a trait for clients, because they can take a bunch of other configuration we cannot foresee (caching, authentication headers, etc.), and that would force us to box all futures.

tomhoule avatar Aug 27 '20 08:08 tomhoule

Thanks for the quick response. I am not familiar with the issue you are mentioning, can you please provide a pointer? Is it only related to async execution?

tomasol avatar Sep 02 '20 15:09 tomasol