Skip to content

Commit

Permalink
fix: remove custom scalars from blueprint (#2987)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
karatakis and tusharmath authored Oct 11, 2024
1 parent 79f89e9 commit 56d536b
Show file tree
Hide file tree
Showing 206 changed files with 222 additions and 7,200 deletions.
48 changes: 40 additions & 8 deletions src/core/blueprint/into_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use async_graphql::dynamic::{self, FieldFuture, FieldValue, SchemaBuilder, TypeR
use async_graphql::ErrorExtensions;
use async_graphql_value::ConstValue;
use futures_util::TryFutureExt;
use strum::IntoEnumIterator;
use tracing::Instrument;

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

/// We set the default value for an `InputValue` by reading it from the
/// blueprint and assigning it to the provided `InputValue` during the
Expand Down Expand Up @@ -199,12 +198,7 @@ impl From<&Blueprint> for SchemaBuilder {
let mutation = blueprint.mutation();
let mut schema = dynamic::Schema::build(query.as_str(), mutation.as_deref(), None);

for scalar in scalar::Scalar::iter() {
let k = scalar.name();
schema = schema.register(dynamic::Type::Scalar(
dynamic::Scalar::new(k.clone()).validator(move |val| scalar.validate(val)),
));
}
schema = inject_custom_scalars(schema, blueprint);

for def in blueprint.definitions.iter() {
schema = schema.register(to_type(def));
Expand All @@ -213,3 +207,41 @@ impl From<&Blueprint> for SchemaBuilder {
schema
}
}

fn inject_custom_scalars(mut schema: SchemaBuilder, blueprint: &Blueprint) -> SchemaBuilder {
fn inject_scalar(schema: SchemaBuilder, type_name: &str) -> SchemaBuilder {
if let Some(scalar) = Scalar::find(type_name) {
let scalar = scalar.clone();
schema.register(dynamic::Type::Scalar(
dynamic::Scalar::new(scalar.name()).validator(move |val| scalar.validate(val)),
))
} else {
schema
}
}

for ty in blueprint.definitions.clone() {
match ty {
Definition::Interface(interface_type_definition) => {
for field in interface_type_definition.fields {
let type_name = field.of_type.name();
schema = inject_scalar(schema, type_name);
}
}
Definition::Object(object_type_definition) => {
for field in object_type_definition.fields {
let type_name = field.of_type.name();
schema = inject_scalar(schema, type_name);
}
}
Definition::InputObject(input_object_type_definition) => {
for field in input_object_type_definition.fields {
let type_name = field.of_type.name();
schema = inject_scalar(schema, type_name);
}
}
_ => (),
}
}
schema
}
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-index-list.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,11 @@
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
username: String
users: [User]
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type User {
name: String
}
Expand Down
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-many-list.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ type A {
d: String
}

scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
u: U
}
Expand All @@ -44,18 +20,6 @@ type U {
e: String
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

schema {
query: Query
}
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-many.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

type Foo {
a: String
b: String
Expand All @@ -20,36 +10,10 @@ type Foo {
x: X
}

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
user: Foo
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type X {
a: String
b: String
Expand Down
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-modify.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,10 @@ type Address {
zipcode: String
}

scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
user: User
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type User {
address: Address
city: String
Expand Down
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-with-composition.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,17 @@ type Address {
street: String
}

scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

type Geo {
lat: String
lng: String
}

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
lat: String
lng: String
user: User
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type User {
address: Address
}
Expand Down
36 changes: 0 additions & 36 deletions tests/core/snapshots/add-field-with-modify.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,13 @@
source: tests/core/spec.rs
expression: formatted
---
scalar Bytes

scalar Date

scalar DateTime

scalar Email

scalar Empty

scalar Int128

scalar Int16

scalar Int32

scalar Int64

scalar Int8

scalar JSON

scalar PhoneNumber

type Query {
person1: User
person2: User
user1: String
user2: String
}

scalar UInt128

scalar UInt16

scalar UInt32

scalar UInt64

scalar UInt8

scalar Url

type User {
name: String
}
Expand Down
Loading

1 comment on commit 56d536b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.59ms 3.27ms 46.01ms 71.57%
Req/Sec 3.35k 362.04 3.71k 94.33%

399487 requests in 30.02s, 765.88MB read

Requests/sec: 13307.05

Transfer/sec: 25.51MB

Please sign in to comment.