Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use graphql_client via a crate that re-exports it? #483

Open
cameronpickham opened this issue Apr 13, 2024 · 1 comment
Open

How to use graphql_client via a crate that re-exports it? #483

cameronpickham opened this issue Apr 13, 2024 · 1 comment

Comments

@cameronpickham
Copy link

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?

@andor44
Copy link

andor44 commented Apr 17, 2024

There are two solutions to the issue:

  1. 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;
  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants