How to use `graphql_client` via a crate that re-exports it?
cameronpickham opened this issue · 1 comments
I've been running into a roadblock trying to set up a crate that re-exports graphql_client and allows users of the crate to define queries using the GraphQL macro without having to add graphql_client to their Cargo.toml. The error that occurs is: failed to resolve: could not find graphql_client in the list of imported crates.
I was able to get my crate to compile by changing this line in graphql_client_codegen from:
fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody<Self::Variables> {
to
fn build_query(variables: Self::Variables) -> graphql_client::QueryBody<Self::Variables> {
What is the recommendation on how to do this? Is there a way to achieve this functionality without having to change the codegen crate?
There are two solutions to the issue:
- As mentioned by @cameronpickham is a quick and easy fix, if the unqualified path is used then downstream crates can bring the type into scope through the middleman crate.
// in the library reexporting `graphql_client`, foo/lib.rs
pub use graphql_client;
// in the binary using the crate `foo`, bar/bin.rs
use foo::graphql_client;
#[derive(GraphQLQuery)]
struct Thing;- Use an attribute on the proc macro invocation to control the path. This is what some big, popular crates do, e.g. serde: https://serde.rs/container-attrs.html#crate