Skip to content
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

Support codegen from query string #470

Merged
merged 2 commits into from
Mar 18, 2024
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
6 changes: 3 additions & 3 deletions examples/github/examples/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ type IssueTimelineConnection {

"An item in an issue timeline"
union IssueTimelineItem =
AssignedEvent
| AssignedEvent
| ClosedEvent
| Commit
| CrossReferencedEvent
Expand Down Expand Up @@ -5009,7 +5009,7 @@ type PullRequestTimelineConnection {

"An item in an pull request timeline"
union PullRequestTimelineItem =
AssignedEvent
| AssignedEvent
| BaseRefForcePushedEvent
| ClosedEvent
| Commit
Expand Down Expand Up @@ -6940,7 +6940,7 @@ type ReviewRequestedEvent implements Node {

"The results of a search."
union SearchResultItem =
Issue
| Issue
| MarketplaceListing
| Organization
| PullRequest
Expand Down
132 changes: 78 additions & 54 deletions graphql_client_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#![warn(rust_2018_idioms)]
#![allow(clippy::option_option)]

//! Crate for internal use by other graphql-client crates, for code generation.
//!
//! It is not meant to be used directly by users of the library.
//! Crate for Rust code generation from a GraphQL query, schema, and options.

use lazy_static::*;
use proc_macro2::TokenStream;
use quote::*;
use schema::Schema;

mod codegen;
mod codegen_options;
Expand Down Expand Up @@ -44,67 +43,92 @@ impl std::error::Error for GeneralError {}

type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
type CacheMap<T> = std::sync::Mutex<BTreeMap<std::path::PathBuf, T>>;
type QueryDocument = graphql_parser::query::Document<'static, String>;

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

/// Generates Rust code given a query document, a schema and options.
fn get_set_cached<T: Clone>(
cache: &CacheMap<T>,
key: &std::path::Path,
value_func: impl FnOnce() -> T,
) -> T {
let mut lock = cache.lock().expect("cache is poisoned");
lock.entry(key.into()).or_insert_with(value_func).clone()
}

fn query_document(query_string: &str) -> Result<QueryDocument, BoxError> {
let document = graphql_parser::parse_query(query_string)
.map_err(|err| GeneralError(format!("Query parser error: {}", err)))?
.into_static();
Ok(document)
}

fn get_set_query_from_file(query_path: &std::path::Path) -> (String, QueryDocument) {
get_set_cached(&QUERY_CACHE, query_path, || {
let query_string = read_file(query_path).unwrap();
let query_document = query_document(&query_string).unwrap();
(query_string, query_document)
})
}

fn get_set_schema_from_file(schema_path: &std::path::Path) -> Schema {
get_set_cached(&SCHEMA_CACHE, schema_path, || {
let schema_extension = schema_path
.extension()
.map(|ext| ext.to_str().expect("Path must be valid UTF-8"))
.unwrap_or("<no extension>");
let schema_string = read_file(schema_path).unwrap();
match schema_extension {
"graphql" | "gql" => {
let s = graphql_parser::schema::parse_schema::<&str>(&schema_string).map_err(|parser_error| GeneralError(format!("Parser error: {}", parser_error))).unwrap();
Schema::from(s)
}
"json" => {
let parsed: graphql_introspection_query::introspection_response::IntrospectionResponse = serde_json::from_str(&schema_string).unwrap();
Schema::from(parsed)
}
extension => panic!("Unsupported extension for the GraphQL schema: {} (only .json and .graphql are supported)", extension)
}
})
}

/// Generates Rust code given a path to a query file, a path to a schema file, and options.
pub fn generate_module_token_stream(
query_path: std::path::PathBuf,
schema_path: &std::path::Path,
options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
use std::collections::btree_map;

let schema_extension = schema_path
.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()) {
btree_map::Entry::Occupied(o) => o.get().clone(),
btree_map::Entry::Vacant(v) => {
schema_string = read_file(v.key())?;
let schema = match schema_extension {
"graphql" | "gql" => {
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" => {
let parsed: graphql_introspection_query::introspection_response::IntrospectionResponse = serde_json::from_str(&schema_string)?;
schema::Schema::from(parsed)
}
extension => return Err(GeneralError(format!("Unsupported extension for the GraphQL schema: {} (only .json and .graphql are supported)", extension)).into())
};

v.insert(schema).clone()
}
}
};
let query = get_set_query_from_file(query_path.as_path());
let schema = get_set_schema_from_file(schema_path);

// We need to qualify the query with the path to the crate it is part of
let (query_string, query) = {
let mut lock = QUERY_CACHE.lock().expect("query cache is poisoned");
match lock.entry(query_path) {
btree_map::Entry::Occupied(o) => o.get().clone(),
btree_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)))?
.into_static();
v.insert((query_string, query)).clone()
}
}
};
generate_module_token_stream_inner(&query, &schema, options)
}

/// Generates Rust code given a query string, a path to a schema file, and options.
pub fn generate_module_token_stream_from_string(
query_string: &str,
schema_path: &std::path::Path,
options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
let query = (query_string.to_string(), query_document(query_string)?);
let schema = get_set_schema_from_file(schema_path);

generate_module_token_stream_inner(&query, &schema, options)
}

let query = crate::query::resolve(&schema, &query)?;
/// Generates Rust code given a query string and query document, a schema, and options.
fn generate_module_token_stream_inner(
query: &(String, QueryDocument),
schema: &Schema,
options: GraphQLClientCodegenOptions,
) -> Result<TokenStream, BoxError> {
let (query_string, query_document) = query;

// We need to qualify the query with the path to the crate it is part of
let query = crate::query::resolve(schema, query_document)?;

// Determine which operation we are generating code for. This will be used in operationName.
let operations = options
Expand All @@ -131,7 +155,7 @@ pub fn generate_module_token_stream(
for operation in &operations {
let generated = generated_module::GeneratedModule {
query_string: query_string.as_str(),
schema: &schema,
schema,
resolved_query: &query,
operation: &operation.1.name,
options: &options,
Expand Down
6 changes: 3 additions & 3 deletions graphql_client_codegen/src/schema/tests/github_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ type IssueTimelineConnection {

"An item in an issue timeline"
union IssueTimelineItem =
AssignedEvent
| AssignedEvent
| ClosedEvent
| Commit
| CrossReferencedEvent
Expand Down Expand Up @@ -5008,7 +5008,7 @@ type PullRequestTimelineConnection {

"An item in an pull request timeline"
union PullRequestTimelineItem =
AssignedEvent
| AssignedEvent
| BaseRefForcePushedEvent
| ClosedEvent
| Commit
Expand Down Expand Up @@ -6939,7 +6939,7 @@ type ReviewRequestedEvent implements Node {

"The results of a search."
union SearchResultItem =
Issue
| Issue
| MarketplaceListing
| Organization
| PullRequest
Expand Down
Loading