Skip to content
Open
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
995 changes: 602 additions & 393 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cynic-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ default = ["rustfmt"]
rustfmt = []

[dependencies]
graphql-parser = "0.3.0"
# graphql-parser = { git = "https://github.com/Yatekii/graphql-parser" }
graphql-parser = { path = "../../graphql-parser" }
proc-macro2 = "1.0"
syn = "1.0"
quote = "1.0"
Expand Down
21 changes: 6 additions & 15 deletions cynic-codegen/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use graphql_parser::error::BorrowedParseError;

use crate::{FieldArgument, TypeIndex};

// Alias all the graphql_parser schema types so we don't have to specify generic parameters
Expand Down Expand Up @@ -35,19 +37,8 @@ pub fn load_schema(filename: impl AsRef<std::path::Path>) -> Result<Document, Sc
}

pub(crate) fn parse_schema(schema: &str) -> Result<Document, SchemaLoadError> {
let borrowed_schema = graphql_parser::schema::parse_schema::<String>(schema)?;
Ok(schema_into_static(borrowed_schema))
}

fn schema_into_static<'a>(
doc: graphql_parser::schema::Document<'a, String>,
) -> graphql_parser::schema::Document<'static, String> {
// A workaround until https://github.com/graphql-rust/graphql-parser/pull/33 is
// merged upstream.

// A document that uses Strings for it's data should be safe to cast to 'static lifetime
// as there's nothing inside it to reference 'a anyway
unsafe { std::mem::transmute::<_, Document>(doc) }
let borrowed_schema = graphql_parser::schema::parse_schema::<String>(&schema)?;
Ok(borrowed_schema.into_static())
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -69,8 +60,8 @@ impl SchemaLoadError {
}
}

impl From<graphql_parser::schema::ParseError> for SchemaLoadError {
fn from(e: graphql_parser::schema::ParseError) -> SchemaLoadError {
impl<'a> From<BorrowedParseError<'a>> for SchemaLoadError {
fn from(e: BorrowedParseError) -> SchemaLoadError {
SchemaLoadError::ParseError(e.to_string())
}
}
Expand Down
1 change: 1 addition & 0 deletions cynic-codegen/src/type_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use crate::schema::{Definition, Document, TypeDefinition};

#[derive(Debug)]
pub struct TypeIndex<'a> {
//name_to_kind: HashMap<String, Kind>,
types: HashMap<&'a str, &'a TypeDefinition>,
Expand Down
16 changes: 7 additions & 9 deletions cynic-codegen/src/use_schema/field_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,7 @@ impl quote::ToTokens for FieldSelector {
);
}

let argument_vals: Vec<_> = self
.required_args
.iter()
.map(|a| {
let name = &a.name;
quote! { ::cynic::serde_json::to_value(&#name) }
})
.collect();
let argument_vals: Vec<_> = self.required_args.iter().map(|a| a.name.clone()).collect();
let argument_strings: Vec<_> = self
.required_args
.iter()
Expand All @@ -86,12 +79,17 @@ impl quote::ToTokens for FieldSelector {
pub fn #rust_field_name(
#(#argument_defs, )*
) -> #selection_builder {
use cynic::InputType;
#selection_builder::new(vec![
#(
::cynic::Argument::new(
#argument_strings,
#argument_gql_types,
#argument_vals
if let Some(upload) = #argument_vals.into_upload() {
cynic::ArgumentWireFormat::Upload(upload.clone())
} else {
cynic::ArgumentWireFormat::Serialize(::cynic::serde_json::to_value(&#argument_vals))
}
),
)*
])
Expand Down
7 changes: 6 additions & 1 deletion cynic-codegen/src/use_schema/selection_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ impl quote::ToTokens for FieldSelectionBuilder {
pub fn #argument_names(
mut self, #argument_names: #argument_types
) -> Self {
use cynic::InputType;
self.args.push(
::cynic::Argument::new(
#argument_strings,
#argument_gql_types,
::cynic::serde_json::to_value(&#argument_names)
if let Some(upload) = #argument_names.into_upload() {
cynic::ArgumentWireFormat::Upload(upload.clone())
} else {
cynic::ArgumentWireFormat::Serialize(::cynic::serde_json::to_value(&#argument_names))
}
)
);

Expand Down
Loading