Skip to content

refactor: store __typename in value instead of separate entity #2626

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 8 commits into from
Aug 26, 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
39 changes: 16 additions & 23 deletions src/core/blueprint/into_schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::borrow::Cow;
use std::sync::Arc;

use anyhow::{bail, Result};
use async_graphql::dynamic::{self, FieldFuture, FieldValue, SchemaBuilder};
use async_graphql::ErrorExtensions;
use async_graphql_value::ConstValue;
Expand All @@ -11,7 +10,7 @@ use tracing::Instrument;

use crate::core::blueprint::{Blueprint, Definition, Type};
use crate::core::http::RequestContext;
use crate::core::ir::{EvalContext, ResolverContext, TypeName};
use crate::core::ir::{EvalContext, ResolverContext, TypedValue};
use crate::core::scalar;

fn to_type_ref(type_of: &Type) -> dynamic::TypeRef {
Expand Down Expand Up @@ -59,27 +58,21 @@ fn set_default_value(
}
}

fn to_field_value<'a>(
ctx: &mut EvalContext<'a, ResolverContext<'a>>,
value: async_graphql::Value,
) -> Result<FieldValue<'static>> {
let type_name = ctx.type_name.take();

Ok(match (value, type_name) {
// NOTE: Mostly type_name is going to be None so we should keep that as the first check.
(value, None) => FieldValue::from(value),
(ConstValue::List(values), Some(TypeName::Vec(names))) => FieldValue::list(
values
.into_iter()
.zip(names)
.map(|(value, type_name)| FieldValue::from(value).with_type(type_name)),
),
(value @ ConstValue::Object(_), Some(TypeName::Single(type_name))) => {
FieldValue::from(value).with_type(type_name)
fn to_field_value(value: async_graphql::Value) -> FieldValue<'static> {
match value {
ConstValue::List(vec) => FieldValue::list(vec.into_iter().map(to_field_value)),
value => {
let type_name = value.get_type_name().map(|s| s.to_string());

let field_value = FieldValue::from(value);

if let Some(type_name) = type_name {
field_value.with_type(type_name)
} else {
field_value
}
}
(ConstValue::Null, _) => FieldValue::NULL,
(_, Some(_)) => bail!("Failed to match type_name"),
})
}
}

fn to_type(def: &Definition) -> dynamic::Type {
Expand Down Expand Up @@ -131,7 +124,7 @@ fn to_type(def: &Definition) -> dynamic::Type {
if let ConstValue::Null = value {
Ok(FieldValue::NONE)
} else {
Ok(Some(to_field_value(ctx, value)?))
Ok(Some(to_field_value(value)))
}
}
.instrument(span)
Expand Down
Loading
Loading