Skip to content

Gql parser 0.4 #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphql_client_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2018"

[dependencies]
graphql-introspection-query = { version = "0.2.0", path = "../graphql-introspection-query" }
graphql-parser = "^0.2"
graphql-parser = "0.4"
heck = "0.4.0"
lazy_static = "1.3"
proc-macro2 = { version = "^1.0", features = [] }
Expand Down
20 changes: 14 additions & 6 deletions graphql_client_codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,16 @@ fn generate_fragment_definitions<'a>(
}

/// For default value constructors.
fn graphql_parser_value_to_literal(
value: &graphql_parser::query::Value,
fn graphql_parser_value_to_literal<'doc, T>(
value: &graphql_parser::query::Value<'doc, T>,
ty: TypeId,
is_optional: bool,
query: &BoundQuery<'_>,
) -> TokenStream {
) -> TokenStream
where
T: graphql_parser::query::Text<'doc>,
T::Value: quote::ToTokens,
{
use graphql_parser::query::Value;

let inner = match value {
Expand Down Expand Up @@ -289,11 +293,15 @@ fn graphql_parser_value_to_literal(
}

/// For default value constructors.
fn render_object_literal(
object_map: &BTreeMap<String, graphql_parser::query::Value>,
fn render_object_literal<'doc, T>(
object_map: &BTreeMap<T::Value, graphql_parser::query::Value<'doc, T>>,
input_id: InputId,
query: &BoundQuery<'_>,
) -> TokenStream {
) -> TokenStream
where
T: graphql_parser::query::Text<'doc>,
T::Value: quote::ToTokens,
{
let input = query.schema.get_input(input_id);
let constructor = Ident::new(&input.name, Span::call_site());
let fields: Vec<TokenStream> = input
Expand Down
10 changes: 6 additions & 4 deletions graphql_client_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;

lazy_static! {
static ref SCHEMA_CACHE: CacheMap<schema::Schema> = CacheMap::default();
static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document)> =
static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document<'static, String>)> =
CacheMap::default();
}

Expand All @@ -63,17 +63,18 @@ pub fn generate_module_token_stream(
.extension()
.and_then(std::ffi::OsStr::to_str)
.unwrap_or("INVALID");
let schema_string;

// Check the schema cache.
let schema: schema::Schema = {
let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned");
match lock.entry(schema_path.to_path_buf()) {
hash_map::Entry::Occupied(o) => o.get().clone(),
hash_map::Entry::Vacant(v) => {
let schema_string = read_file(v.key())?;
schema_string = read_file(v.key())?;
let schema = match schema_extension {
"graphql" | "gql" => {
let s = graphql_parser::schema::parse_schema(&schema_string).map_err(|parser_error| GeneralError(format!("Parser error: {}", parser_error)))?;
let s = graphql_parser::schema::parse_schema::<&str>(&schema_string).map_err(|parser_error| GeneralError(format!("Parser error: {}", parser_error)))?;
schema::Schema::from(s)
}
"json" => {
Expand All @@ -96,7 +97,8 @@ pub fn generate_module_token_stream(
hash_map::Entry::Vacant(v) => {
let query_string = read_file(v.key())?;
let query = graphql_parser::parse_query(&query_string)
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?;
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?
.into_static();
v.insert((query_string, query)).clone()
}
}
Expand Down
Loading