Skip to content

Commit ab33a26

Browse files
authored
Merge pull request #24 from tomhoule/interfaces
Improve error messages on wrong query/schema file path
2 parents 96618d8 + 10d5697 commit ab33a26

File tree

1 file changed

+22
-4
lines changed
  • graphql_query_derive/src

1 file changed

+22
-4
lines changed

graphql_query_derive/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::Token
4545
gen.into()
4646
}
4747

48+
fn read_file(
49+
path: impl AsRef<::std::path::Path> + ::std::fmt::Debug,
50+
) -> Result<String, failure::Error> {
51+
use std::io::prelude::*;
52+
53+
let mut out = String::new();
54+
let mut file = ::std::fs::File::open(&path).map_err(|io_err| {
55+
let err: failure::Error = io_err.into();
56+
err.context(format!(
57+
r#"
58+
Could not find file with path: {:?}
59+
Hint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = "src/my_query.graphql".
60+
"#,
61+
path
62+
))
63+
})?;
64+
file.read_to_string(&mut out)?;
65+
Ok(out)
66+
}
67+
4868
fn impl_gql_query(input: &syn::DeriveInput) -> Result<TokenStream, failure::Error> {
4969
use std::io::prelude::*;
5070

@@ -56,14 +76,12 @@ fn impl_gql_query(input: &syn::DeriveInput) -> Result<TokenStream, failure::Erro
5676

5777
// We need to qualify the query with the path to the crate it is part of
5878
let query_path = format!("{}/{}", cargo_manifest_dir, query_path);
59-
let mut query_string = String::new();
60-
::std::fs::File::open(query_path)?.read_to_string(&mut query_string)?;
79+
let query_string = read_file(&query_path)?;
6180
let query = graphql_parser::parse_query(&query_string)?;
6281

6382
// We need to qualify the schema with the path to the crate it is part of
6483
let schema_path = ::std::path::Path::new(&cargo_manifest_dir).join(schema_path);
65-
let mut schema_string = String::new();
66-
::std::fs::File::open(&schema_path)?.read_to_string(&mut schema_string)?;
84+
let schema_string = read_file(&schema_path)?;
6785

6886
let extension = schema_path
6987
.extension()

0 commit comments

Comments
 (0)