Skip to content

Commit

Permalink
fix(scalar): custom scalars should return data as is (#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill authored May 3, 2024
1 parent b534fce commit e72f0b0
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/blueprint/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn validate_field_has_resolver(
let type_name = &field.type_of;
if let Some(ty) = types.get(type_name) {
// It's an enum
if ty.variants.is_some() {
if ty.variants.is_some() || ty.scalar {
return true;
}
let res = validate_type_has_resolvers(type_name, ty, types);
Expand Down
4 changes: 4 additions & 0 deletions src/config/config_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ fn get_input_types(config: &Config) -> HashSet<String> {
.filter(|(_, arg)| !scalar::is_scalar(&arg.type_of))
{
if let Some(t) = config.find_type(&arg.type_of) {
if t.scalar {
continue;
}

t.fields.iter().for_each(|(_, f)| {
types.insert(f.type_of.clone());
recurse_type(config, &f.type_of, &mut types)
Expand Down
21 changes: 6 additions & 15 deletions tests/core/snapshots/test-scalars.md_4.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,11 @@ expression: response
"content-type": "application/json"
},
"body": {
"data": null,
"errors": [
{
"message": "internal: invalid value for scalar \"Email\", expected \"FieldValue::Value\"",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"email"
]
}
]
"data": {
"any1": {
"test": "abc"
},
"any2": "string-value"
}
}
}
4 changes: 2 additions & 2 deletions tests/core/snapshots/test-scalars.md_5.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ expression: response
"data": null,
"errors": [
{
"message": "internal: invalid value for scalar \"PhoneNumber\", expected \"FieldValue::Value\"",
"message": "internal: invalid value for scalar \"Email\", expected \"FieldValue::Value\"",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"phone"
"email"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/core/snapshots/test-scalars.md_7.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ expression: response
"data": null,
"errors": [
{
"message": "internal: invalid value for scalar \"Date\", expected \"FieldValue::Value\"",
"message": "internal: invalid value for scalar \"PhoneNumber\", expected \"FieldValue::Value\"",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"date"
"phone"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/core/snapshots/test-scalars.md_8.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ expression: response
"data": null,
"errors": [
{
"message": "internal: invalid value for scalar \"Url\", expected \"FieldValue::Value\"",
"message": "internal: invalid value for scalar \"Date\", expected \"FieldValue::Value\"",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"url"
"date"
]
}
]
Expand Down
27 changes: 27 additions & 0 deletions tests/core/snapshots/test-scalars.md_9.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/core/spec.rs
expression: response
---
{
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"data": null,
"errors": [
{
"message": "internal: invalid value for scalar \"Url\", expected \"FieldValue::Value\"",
"locations": [
{
"line": 1,
"column": 3
}
],
"path": [
"url"
]
}
]
}
}
3 changes: 3 additions & 0 deletions tests/core/snapshots/test-scalars.md_client.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
source: tests/core/spec.rs
expression: client
---
scalar AnyScalar

scalar Date

scalar Email
Expand All @@ -13,6 +15,7 @@ scalar JSON
scalar PhoneNumber

type Query {
any(value: AnyScalar!): AnyScalar
date(value: Date!): Date!
email(value: Email!): Email!
phone(value: PhoneNumber!): PhoneNumber!
Expand Down
7 changes: 2 additions & 5 deletions tests/core/snapshots/test-scalars.md_merged.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ schema @server(hostname: "localhost", port: 8000) @upstream {
query: Query
}

scalar Date
scalar AnyScalar

scalar Email

scalar PhoneNumber

scalar Url

type Query {
any(value: AnyScalar!): AnyScalar @expr(body: "{{.args.value}}")
date(value: Date!): Date! @expr(body: "{{.args.value}}")
email(value: Email!): Email! @expr(body: "{{.args.value}}")
phone(value: PhoneNumber!): PhoneNumber! @expr(body: "{{.args.value}}")
Expand Down
13 changes: 10 additions & 3 deletions tests/execution/test-scalars.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# test-scalar-email

```graphql @server
# this is already defined scalars in tailcall
scalar Email
scalar PhoneNumber
scalar Date
scalar Url

# this is custom scalars in config
scalar AnyScalar

schema @server(port: 8000, hostname: "localhost") {
query: Query
Expand All @@ -15,6 +16,7 @@ type Query {
phone(value: PhoneNumber!): PhoneNumber! @expr(body: "{{.args.value}}")
date(value: Date!): Date! @expr(body: "{{.args.value}}")
url(value: Url!): Url! @expr(body: "{{.args.value}}")
any(value: AnyScalar!): AnyScalar @expr(body: "{{.args.value}}")
}
```

Expand All @@ -39,6 +41,11 @@ type Query {
body:
query: '{ url(value: "https://tailcall.run/") }'

- method: POST
url: http://localhost:8000/graphql
body:
query: '{ any1: any(value: { test: "abc" } ), any2: any(value: "string-value") }'

# Invalid value test

- method: POST
Expand Down

1 comment on commit e72f0b0

@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.65ms 3.71ms 125.89ms 75.00%
Req/Sec 3.32k 261.84 3.65k 86.67%

396444 requests in 30.00s, 1.99GB read

Requests/sec: 13213.16

Transfer/sec: 67.82MB

Please sign in to comment.