Skip to content

Commit 64ad4e3

Browse files
authored
Merge pull request #396 from mathstuf/gql-parser-0.3
2 parents e79e30c + d0bc344 commit 64ad4e3

File tree

9 files changed

+213
-118
lines changed

9 files changed

+213
-118
lines changed

graphql_client_codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2018"
99

1010
[dependencies]
1111
graphql-introspection-query = { version = "0.2.0", path = "../graphql-introspection-query" }
12-
graphql-parser = "^0.2"
12+
graphql-parser = "0.4"
1313
heck = "0.4.0"
1414
lazy_static = "1.3"
1515
proc-macro2 = { version = "^1.0", features = [] }

graphql_client_codegen/src/codegen.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,16 @@ fn generate_fragment_definitions<'a>(
236236
}
237237

238238
/// For default value constructors.
239-
fn graphql_parser_value_to_literal(
240-
value: &graphql_parser::query::Value,
239+
fn graphql_parser_value_to_literal<'doc, T>(
240+
value: &graphql_parser::query::Value<'doc, T>,
241241
ty: TypeId,
242242
is_optional: bool,
243243
query: &BoundQuery<'_>,
244-
) -> TokenStream {
244+
) -> TokenStream
245+
where
246+
T: graphql_parser::query::Text<'doc>,
247+
T::Value: quote::ToTokens,
248+
{
245249
use graphql_parser::query::Value;
246250

247251
let inner = match value {
@@ -289,11 +293,15 @@ fn graphql_parser_value_to_literal(
289293
}
290294

291295
/// For default value constructors.
292-
fn render_object_literal(
293-
object_map: &BTreeMap<String, graphql_parser::query::Value>,
296+
fn render_object_literal<'doc, T>(
297+
object_map: &BTreeMap<T::Value, graphql_parser::query::Value<'doc, T>>,
294298
input_id: InputId,
295299
query: &BoundQuery<'_>,
296-
) -> TokenStream {
300+
) -> TokenStream
301+
where
302+
T: graphql_parser::query::Text<'doc>,
303+
T::Value: quote::ToTokens,
304+
{
297305
let input = query.schema.get_input(input_id);
298306
let constructor = Ident::new(&input.name, Span::call_site());
299307
let fields: Vec<TokenStream> = input

graphql_client_codegen/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;
4747

4848
lazy_static! {
4949
static ref SCHEMA_CACHE: CacheMap<schema::Schema> = CacheMap::default();
50-
static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document)> =
50+
static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document<'static, String>)> =
5151
CacheMap::default();
5252
}
5353

@@ -63,17 +63,18 @@ pub fn generate_module_token_stream(
6363
.extension()
6464
.and_then(std::ffi::OsStr::to_str)
6565
.unwrap_or("INVALID");
66+
let schema_string;
6667

6768
// Check the schema cache.
6869
let schema: schema::Schema = {
6970
let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned");
7071
match lock.entry(schema_path.to_path_buf()) {
7172
hash_map::Entry::Occupied(o) => o.get().clone(),
7273
hash_map::Entry::Vacant(v) => {
73-
let schema_string = read_file(v.key())?;
74+
schema_string = read_file(v.key())?;
7475
let schema = match schema_extension {
7576
"graphql" | "gql" => {
76-
let s = graphql_parser::schema::parse_schema(&schema_string).map_err(|parser_error| GeneralError(format!("Parser error: {}", parser_error)))?;
77+
let s = graphql_parser::schema::parse_schema::<&str>(&schema_string).map_err(|parser_error| GeneralError(format!("Parser error: {}", parser_error)))?;
7778
schema::Schema::from(s)
7879
}
7980
"json" => {
@@ -96,7 +97,8 @@ pub fn generate_module_token_stream(
9697
hash_map::Entry::Vacant(v) => {
9798
let query_string = read_file(v.key())?;
9899
let query = graphql_parser::parse_query(&query_string)
99-
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?;
100+
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?
101+
.into_static();
100102
v.insert((query_string, query)).clone()
101103
}
102104
}

0 commit comments

Comments
 (0)