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 exactly to build wrapper libraries? #956

Open
negezor opened this issue May 17, 2024 · 2 comments
Open

How exactly to build wrapper libraries? #956

negezor opened this issue May 17, 2024 · 2 comments

Comments

@negezor
Copy link

negezor commented May 17, 2024

Hi, I am making a wrapper library for a website with GraphQL API. I would like to ship the schema file with the library so the user doesn't have to download it manually. It only had to define own objects or import ready-made ones.

I have not found clarification in the documentation how this can be done. I would like to ask you as the author of the library. As an option I saw to use include_str!() from the main library, then the user would only have to call in build.rs:

// lib.rs
#[cfg(feature = "codegen"]]
pub fn register_schema() {
    cynic_codegen::register_schema("some-schema")
        .from_sdl(include_str!("./schema.graphql"))
        .unwrap()
        .as_default()
        .unwrap();
}

// build.rs
fn main() {
  library::register_schema();
}

Also is it possible to avoid build.rs in user code? If it will also define its own objects?

@obmarg
Copy link
Owner

obmarg commented May 17, 2024

Hi @negezor - good question, this isn't something I've put a lot of thought into.

The approach you suggest certainly seems like a good option - it's not too hard for you, and simple enough for the consumers of your library.

Also is it possible to avoid build.rs in user code? If it will also define its own objects?

I think that depends how much effort you want to put in. All of the derive macros in cynic need to get the schema from somewhere - either the output of that register_schema function (which goes into the current crates OUT_DIR) or you can give a path to the schema file to each individual macro.

But if you were really wanting to avoid users dealing with this it might be possible to write your own derives that wrap the built in cynic ones, re-directing them towards your specific schema. Either by prepopulating the schema_path attribute, or (if you wanted to get more advanced) maybe passing in the path to your libraries OUT_DIR somehow so it loads the schema from there.

I'm not sure how easy (or even possible) that'd be mind you - but you could try it if you wanted. It might need some API changes in cynic-codegen to expose some things. But I could be open to accepting those, depending on what they are - probably best to talk it through with me after investigating if you do want to go down this route.

@0xangelo
Copy link

Thanks @negezor! I was having the same problem since we were trying to reuse the 'schema' crate in our company (we use a private Cargo registry) but it was difficult since we had to copy the schema files to the dependent crates, which could live in other repos

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

3 participants