Skip to content

Commit

Permalink
Update cynic-codegen to use cynic-parser (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg authored Apr 7, 2024
1 parent a73244d commit 94389bc
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 251 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ version = "3.5.1"
rust-version = "1.69"

[workspace.dependencies]
cynic-parser.path = "cynic-parser"
darling = "0.20"
rstest = "0.18"
syn = "2"
Expand Down
2 changes: 1 addition & 1 deletion cynic-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ rkyv = ["dep:rkyv"]

[dependencies]
counter = "0.5"
cynic-parser.workspace = true
darling.workspace = true
graphql-parser = "0.4.0"
once_cell = "1.9.0"
ouroboros = "0.18"
proc-macro2 = "1.0"
Expand Down
7 changes: 3 additions & 4 deletions cynic-codegen/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,10 @@ impl SchemaRegistration<'_> {

fn schema(&self) -> Result<&Schema<'_, schema::Validated>, SchemaRegistrationError> {
self.schema.get_or_try_init(|| {
let document = crate::schema::load_schema(self.data.as_ref())
.map_err(|error| SchemaRegistrationError::SchemaErrors(error.to_string()))?
.into_static();
let ast = crate::schema::load_schema(self.data.as_ref())
.map_err(|error| SchemaRegistrationError::SchemaErrors(error.to_string()))?;

let schema = Schema::new(SchemaInput::Document(document))
let schema = Schema::new(SchemaInput::Document(ast))
.validate()
.map_err(|errors| SchemaRegistrationError::SchemaErrors(errors.to_string()))?;

Expand Down
10 changes: 5 additions & 5 deletions cynic-codegen/src/schema/input.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::schema::load_schema;

use super::{parser::SchemaLoadError, Document};
use super::parser::SchemaLoadError;

pub enum SchemaInput {
Document(Document),
Document(cynic_parser::TypeSystemDocument),
#[cfg(feature = "rkyv")]
Archive(Vec<u8>),
}
Expand Down Expand Up @@ -53,8 +53,8 @@ impl SchemaInput {
path: impl AsRef<std::path::Path>,
) -> Result<SchemaInput, SchemaLoadError> {
let path = path.as_ref();
if let Some(document) = document_from_path(path)? {
return Ok(SchemaInput::Document(document));
if let Some(ast) = document_from_path(path)? {
return Ok(SchemaInput::Document(ast));
}
return Err(SchemaLoadError::FileNotFound(
path.to_string_lossy().to_string(),
Expand All @@ -78,7 +78,7 @@ impl SchemaInput {

fn document_from_path(
filename: impl AsRef<std::path::Path>,
) -> Result<Option<Document>, SchemaLoadError> {
) -> Result<Option<cynic_parser::TypeSystemDocument>, SchemaLoadError> {
use std::path::PathBuf;
let mut pathbuf = PathBuf::new();

Expand Down
10 changes: 3 additions & 7 deletions cynic-codegen/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ pub mod markers;
use std::convert::Infallible;
use std::marker::PhantomData;

pub use self::{
input::SchemaInput,
names::FieldName,
parser::{load_schema, Document},
};
pub use self::{input::SchemaInput, names::FieldName, parser::load_schema};
use self::{type_index::TypeIndex, types::SchemaRoots};

// TODO: Uncomment this
Expand All @@ -32,8 +28,8 @@ pub struct Schema<'a, State> {
impl<'a> Schema<'a, Unvalidated> {
pub fn new(input: SchemaInput) -> Self {
let type_index: Box<dyn TypeIndex> = match input {
SchemaInput::Document(document) => {
Box::new(type_index::SchemaBackedTypeIndex::for_schema(document))
SchemaInput::Document(ast) => {
Box::new(type_index::SchemaBackedTypeIndex::for_schema(ast))
}
#[cfg(feature = "rkyv")]
SchemaInput::Archive(data) => Box::new(
Expand Down
83 changes: 4 additions & 79 deletions cynic-codegen/src/schema/parser.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
// TODO: It would be good to make these use &'str since this is compile time
// and max speed is best.

// TODO: It would be good to make these use &'str since this is compile time
// and max speed is best.

// Alias all the graphql_parser schema types so we don't have to specify generic parameters
// everywhere
pub type Document = graphql_parser::schema::Document<'static, String>;
pub type Type = graphql_parser::schema::Type<'static, String>;
pub type Field = graphql_parser::schema::Field<'static, String>;
pub type Definition = graphql_parser::schema::Definition<'static, String>;
pub type TypeDefinition = graphql_parser::schema::TypeDefinition<'static, String>;
pub type ScalarType = graphql_parser::schema::ScalarType<'static, String>;
pub type InputValue = graphql_parser::schema::InputValue<'static, String>;

/// Loads a schema from a string
pub fn load_schema(sdl: &str) -> Result<Document, SchemaLoadError> {
Ok(add_typenames(parse_schema(sdl)?))
}

fn add_typenames(mut schema: Document) -> Document {
for definition in &mut schema.definitions {
match definition {
Definition::TypeDefinition(TypeDefinition::Object(def)) => {
def.fields.push(typename_field());
}
Definition::TypeDefinition(TypeDefinition::Interface(def)) => {
def.fields.push(typename_field());
}
_ => {}
}
}
schema
}

pub(crate) fn parse_schema(schema: &str) -> Result<Document, SchemaLoadError> {
Ok(graphql_parser::schema::parse_schema::<String>(schema)?.into_static())
pub fn load_schema(sdl: &str) -> Result<cynic_parser::TypeSystemDocument, SchemaLoadError> {
let ast = cynic_parser::parse_type_system_document(sdl)
.map_err(|error| SchemaLoadError::ParseError(error.to_string()))?;
Ok(ast)
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -85,50 +52,8 @@ impl std::fmt::Display for SchemaLoadError {
const SCHEMA_DOCUMENTATION_TEXT: &str =
"See the cynic documentation on regsistering schemas if you need help.";

impl From<graphql_parser::schema::ParseError> for SchemaLoadError {
fn from(e: graphql_parser::schema::ParseError) -> SchemaLoadError {
SchemaLoadError::ParseError(e.to_string())
}
}

impl From<std::io::Error> for SchemaLoadError {
fn from(e: std::io::Error) -> SchemaLoadError {
SchemaLoadError::IoError(e.to_string())
}
}

/// Extension trait for the schema Type type
pub trait TypeExt {
fn inner_name(&self) -> &str;
}

impl TypeExt for Type {
fn inner_name(&self) -> &str {
match self {
Type::NamedType(s) => s,
Type::ListType(inner) => inner.inner_name(),
Type::NonNullType(inner) => inner.inner_name(),
}
}
}

pub trait ScalarTypeExt {
fn is_builtin(&self) -> bool;
}

impl ScalarTypeExt for ScalarType {
fn is_builtin(&self) -> bool {
matches!(self.name.as_ref(), "String" | "Int" | "Boolean" | "ID")
}
}

fn typename_field() -> Field {
Field {
position: graphql_parser::Pos { line: 0, column: 0 },
description: None,
name: "__typename".into(),
arguments: vec![],
field_type: Type::NonNullType(Box::new(Type::NamedType("String".into()))),
directives: vec![],
}
}
Loading

0 comments on commit 94389bc

Please sign in to comment.