You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library contains a procedural macro that reads a GraphQL schema file, and generates the
corresponding Junipermacro calls. This means you can
have a real schema file and be guaranteed that it matches your Rust implementation. It also
removes most of the boilerplate involved in using Juniper.
Looking for juniper 0.15 support?
The version of juniper-from-schema that is released on crates.io (0.5.2) doesn't support juniper 0.15. However the master branch does! So you will have to use a git dependency for now. We plan to do an official release soon. Follow this milestone to see whats left.
use juniper_from_schema::graphql_schema_from_file;// This is the important linegraphql_schema_from_file!("readme_schema.graphql");pubstructContext;impl juniper::ContextforContext{}pubstructQuery;// This trait is generated by `graphql_schema_from_file!` based on the schemaimplQueryFieldsforQuery{fnfield_hello_world(&self,_executor:&juniper::Executor<Context>,name:String,) -> juniper::FieldResult<String>{Ok(format!("Hello, {}!", name))}}fnmain(){let ctx = Context;let query = "query { helloWorld(name: \"Ferris\") }";let(result, errors) = juniper::execute_sync(
query,None,&Schema::new(Query, juniper::EmptyMutation::new()),&juniper::Variables::new(),&ctx,).unwrap();assert_eq!(errors.len(),0);assert_eq!(
result
.as_object_value().unwrap().get_field_value("helloWorld").unwrap().as_scalar_value::<String>().unwrap(),"Hello, Ferris!",);}
If you're having issues with N+1 query bugs consider using juniper-eager-loading. It was built to integrate seamlessly with juniper-from-schema.
Development
If you're seeing No such file or directory (os error 2) when running the tests
This might be caused by setting CARGO_TARGET_DIR. Setting that env var changes the directory the trybuild tests are run from which means all the paths to the test schemas no longer match. The only workaround is to unset CARGO_TARGET_DIR when working on juniper-from-schema. I recommend direnv to unset the env var only this directory and not globally.