|
1 | | -#![recursion_limit = "512"] |
2 | | - |
3 | | -#[macro_use] |
4 | | -extern crate failure; |
5 | | -extern crate graphql_parser; |
6 | | -extern crate heck; |
7 | | -extern crate itertools; |
8 | | -#[macro_use] |
9 | | -extern crate lazy_static; |
| 1 | +extern crate graphql_client_codegen; |
10 | 2 | extern crate proc_macro; |
11 | 3 | extern crate proc_macro2; |
12 | | -extern crate serde; |
13 | | -#[macro_use] |
14 | | -extern crate serde_derive; |
15 | | -extern crate serde_json; |
16 | 4 | extern crate syn; |
17 | | -#[macro_use] |
18 | | -extern crate quote; |
| 5 | +use graphql_client_codegen::*; |
19 | 6 |
|
20 | 7 | use proc_macro2::TokenStream; |
21 | 8 |
|
22 | | -mod attributes; |
23 | | -mod codegen; |
24 | | -mod constants; |
25 | | -mod deprecation; |
26 | | -mod enums; |
27 | | -mod field_type; |
28 | | -mod fragments; |
29 | | -mod inputs; |
30 | | -mod interfaces; |
31 | | -mod introspection_response; |
32 | | -mod objects; |
33 | | -mod operations; |
34 | | -mod query; |
35 | | -mod scalars; |
36 | | -mod schema; |
37 | | -mod selection; |
38 | | -mod shared; |
39 | | -mod unions; |
40 | | -mod variables; |
41 | | - |
42 | | -#[cfg(test)] |
43 | | -mod tests; |
44 | | - |
45 | | -use heck::*; |
46 | | -use proc_macro2::{Ident, Span}; |
47 | | - |
48 | | -type CacheMap<T> = ::std::sync::Mutex<::std::collections::hash_map::HashMap<::std::path::PathBuf, T>>; |
49 | | - |
50 | | -lazy_static! { |
51 | | - static ref SCHEMA_CACHE: CacheMap<schema::Schema> = CacheMap::default(); |
52 | | - static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document)> = CacheMap::default(); |
53 | | -} |
54 | | - |
55 | 9 | #[proc_macro_derive(GraphQLQuery, attributes(graphql))] |
56 | 10 | pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
57 | 11 | let input = TokenStream::from(input); |
58 | 12 | let ast = syn::parse2(input).expect("Derive input is well formed"); |
59 | | - let gen = impl_gql_query(&ast).unwrap(); |
| 13 | + let (query_path, schema_path) = build_query_and_schema_path(&ast); |
| 14 | + let option = GraphQLClientDeriveOptions { input: &ast }; |
| 15 | + let gen = generate_module_token_stream(query_path, schema_path, Some(option)).unwrap(); |
60 | 16 | gen.into() |
61 | 17 | } |
62 | 18 |
|
63 | | -fn read_file( |
64 | | - path: &::std::path::Path, |
65 | | -) -> Result<String, failure::Error> { |
66 | | - use std::io::prelude::*; |
67 | | - |
68 | | - let mut out = String::new(); |
69 | | - let mut file = ::std::fs::File::open(path).map_err(|io_err| { |
70 | | - let err: failure::Error = io_err.into(); |
71 | | - err.context(format!( |
72 | | - r#" |
73 | | - Could not find file with path: {} |
74 | | - 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". |
75 | | - "#, |
76 | | - path.display() |
77 | | - )) |
78 | | - })?; |
79 | | - file.read_to_string(&mut out)?; |
80 | | - Ok(out) |
81 | | -} |
82 | | - |
83 | | -#[derive(Serialize, Deserialize, Debug)] |
84 | | -pub(crate) struct FullResponse<T> { |
85 | | - data: T, |
86 | | -} |
87 | | - |
88 | | -fn impl_gql_query(input: &syn::DeriveInput) -> Result<TokenStream, failure::Error> { |
| 19 | +fn build_query_and_schema_path( |
| 20 | + input: &syn::DeriveInput, |
| 21 | +) -> (std::path::PathBuf, std::path::PathBuf) { |
89 | 22 | let cargo_manifest_dir = |
90 | 23 | ::std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env variable is defined"); |
91 | 24 |
|
92 | | - let query_path = attributes::extract_attr(input, "query_path")?; |
93 | | - let schema_path = attributes::extract_attr(input, "schema_path")?; |
94 | | - let response_derives = attributes::extract_attr(input, "response_derives").ok(); |
95 | | - |
96 | | - // The user can determine what to do about deprecations. |
97 | | - let deprecation_strategy = deprecation::extract_deprecation_strategy(input) |
98 | | - .unwrap_or(deprecation::DeprecationStrategy::Warn); |
99 | | - |
100 | | - // We need to qualify the query with the path to the crate it is part of |
101 | | - let query_path = ::std::path::Path::new(&cargo_manifest_dir).join(query_path); |
102 | | - // Check the query cache. |
103 | | - let (query_string, query) = { |
104 | | - let mut lock = QUERY_CACHE.lock().expect("query cache is poisoned"); |
105 | | - match lock.entry(query_path) { |
106 | | - ::std::collections::hash_map::Entry::Occupied(o) => o.get().clone(), |
107 | | - ::std::collections::hash_map::Entry::Vacant(v) => { |
108 | | - let query_string = read_file(v.key())?; |
109 | | - let query = graphql_parser::parse_query(&query_string)?; |
110 | | - v.insert((query_string, query)).clone() |
111 | | - }, |
112 | | - } |
113 | | - }; |
114 | | - |
115 | | - // We need to qualify the schema with the path to the crate it is part of |
| 25 | + let query_path = attributes::extract_attr(input, "query_path").unwrap(); |
| 26 | + let query_path = format!("{}/{}", cargo_manifest_dir, query_path); |
| 27 | + let query_path = ::std::path::Path::new(&query_path).to_path_buf(); |
| 28 | + let schema_path = attributes::extract_attr(input, "schema_path").unwrap(); |
116 | 29 | let schema_path = ::std::path::Path::new(&cargo_manifest_dir).join(schema_path); |
117 | | - // Check the schema cache. |
118 | | - let schema = { |
119 | | - let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned"); |
120 | | - match lock.entry(schema_path) { |
121 | | - ::std::collections::hash_map::Entry::Occupied(o) => o.get().clone(), |
122 | | - ::std::collections::hash_map::Entry::Vacant(v) => { |
123 | | - let schema_string = read_file(v.key())?; |
124 | | - let schema = { |
125 | | - let extension = v |
126 | | - .key() |
127 | | - .extension() |
128 | | - .and_then(|e| e.to_str()) |
129 | | - .unwrap_or("INVALID"); |
130 | | - |
131 | | - match extension { |
132 | | - "graphql" | "gql" => { |
133 | | - let s = graphql_parser::schema::parse_schema(&schema_string)?; |
134 | | - schema::Schema::from(s) |
135 | | - } |
136 | | - "json" => { |
137 | | - let parsed: FullResponse<introspection_response::IntrospectionResponse> = ::serde_json::from_str(&schema_string)?; |
138 | | - schema::Schema::from(parsed.data) |
139 | | - } |
140 | | - extension => panic!("Unsupported extension for the GraphQL schema: {} (only .json and .graphql are supported)", extension) |
141 | | - } |
142 | | - }; |
143 | | - |
144 | | - v.insert(schema).clone() |
145 | | - }, |
146 | | - } |
147 | | - }; |
148 | | - |
149 | | - let module_name = Ident::new(&input.ident.to_string().to_snake_case(), Span::call_site()); |
150 | | - let struct_name = &input.ident; |
151 | | - let schema_output = codegen::response_for_query( |
152 | | - schema, |
153 | | - query, |
154 | | - input.ident.to_string(), |
155 | | - response_derives, |
156 | | - deprecation_strategy, |
157 | | - )?; |
158 | | - |
159 | | - let result = quote!( |
160 | | - pub mod #module_name { |
161 | | - #![allow(non_camel_case_types)] |
162 | | - #![allow(non_snake_case)] |
163 | | - #![allow(dead_code)] |
164 | | - |
165 | | - use serde; |
166 | | - |
167 | | - pub const QUERY: &'static str = #query_string; |
168 | | - |
169 | | - #schema_output |
170 | | - } |
171 | | - |
172 | | - impl<'de> ::graphql_client::GraphQLQuery<'de> for #struct_name { |
173 | | - type Variables = #module_name::Variables; |
174 | | - type ResponseData = #module_name::ResponseData; |
175 | | - |
176 | | - fn build_query(variables: Self::Variables) -> ::graphql_client::GraphQLQueryBody<Self::Variables> { |
177 | | - ::graphql_client::GraphQLQueryBody { |
178 | | - variables, |
179 | | - query: #module_name::QUERY, |
180 | | - } |
181 | | - |
182 | | - } |
183 | | - } |
184 | | - ); |
185 | | - |
186 | | - Ok(result) |
| 30 | + (query_path, schema_path) |
187 | 31 | } |
0 commit comments