@@ -45,6 +45,26 @@ pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::Token
45
45
gen. into ( )
46
46
}
47
47
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
+
48
68
fn impl_gql_query ( input : & syn:: DeriveInput ) -> Result < TokenStream , failure:: Error > {
49
69
use std:: io:: prelude:: * ;
50
70
@@ -56,14 +76,12 @@ fn impl_gql_query(input: &syn::DeriveInput) -> Result<TokenStream, failure::Erro
56
76
57
77
// We need to qualify the query with the path to the crate it is part of
58
78
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) ?;
61
80
let query = graphql_parser:: parse_query ( & query_string) ?;
62
81
63
82
// We need to qualify the schema with the path to the crate it is part of
64
83
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) ?;
67
85
68
86
let extension = schema_path
69
87
. extension ( )
0 commit comments