Skip to content

Commit 898e8b8

Browse files
authored
feat(compiler): allow SDL to contain built-in scalar definitions (#990)
Adds new `ignore_builtin_redefinitions` option on the `SchemaBuilder` to allow SDL to contain built-in scalar re-definitions. Re-definitions are going to be effectively ignored and compiler will continue to use built-in GraphQL spec definitions. While [GraphQL spec](https://spec.graphql.org/October2021/#sec-Scalars) explicitly specifies following ``` When representing a GraphQL schema using the type system definition language, all built-in scalars must be omitted for brevity. ``` `graphql-js` reference implementation does not enforce this and does not report any errors even if built-in scalars are present in SDL. This change allows users to skip this validation to match `graphql-js` behavior.
1 parent c007c5e commit 898e8b8

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

crates/apollo-compiler/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717
## Maintenance
1818
## Documentation-->
1919

20+
# [x.x.x] (unreleased) - 2025-mm-dd
21+
22+
## Features
23+
24+
- **Adds `ignore_builtin_redefinitions` option to `SchemaBuilder` to allow SDL to contain built-in
25+
scalar definitions - [dariuszkuc], [pull/990]**
26+
27+
2028
# [1.29.0](https://crates.io/crates/apollo-compiler/1.29.0) - 2025-08-08
2129

2230
## Features

crates/apollo-compiler/src/schema/from_ast.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::Arc;
77
#[derive(Clone)]
88
pub struct SchemaBuilder {
99
adopt_orphan_extensions: bool,
10+
ignore_builtin_redefinitions: bool,
1011
pub(crate) schema: Schema,
1112
schema_definition: SchemaDefinitionStatus,
1213
orphan_type_extensions: IndexMap<Name, Vec<ast::Definition>>,
@@ -33,6 +34,7 @@ impl SchemaBuilder {
3334
BUILT_IN.get_or_init(|| {
3435
let mut builder = SchemaBuilder {
3536
adopt_orphan_extensions: false,
37+
ignore_builtin_redefinitions: false,
3638
schema: Schema {
3739
sources: Default::default(),
3840
schema_definition: Node::new(SchemaDefinition {
@@ -76,6 +78,14 @@ impl SchemaBuilder {
7678
self
7779
}
7880

81+
/// Configure the builder to allow SDL to contain scalar built-in types re-definitions.
82+
/// Re-definitions are going to be effectively ignored and compiler will continue to use
83+
/// built-in GraphQL spec definitions.
84+
pub fn ignore_builtin_redefinitions(mut self) -> Self {
85+
self.ignore_builtin_redefinitions = true;
86+
self
87+
}
88+
7989
/// Parse an input file with the default configuration as an additional input for this schema.
8090
///
8191
/// Create a [`Parser`] to use different parser configuration.
@@ -125,10 +135,14 @@ impl SchemaBuilder {
125135
Entry::Occupied(entry) => {
126136
let previous = entry.get();
127137
if $is_scalar && previous.is_built_in() {
128-
self.errors.push(
129-
$def.location(),
130-
BuildError::BuiltInScalarTypeRedefinition,
131-
)
138+
if self.ignore_builtin_redefinitions {
139+
continue;
140+
} else {
141+
self.errors.push(
142+
$def.location(),
143+
BuildError::BuiltInScalarTypeRedefinition,
144+
)
145+
}
132146
} else {
133147
self.errors.push(
134148
$def.name.location(),
@@ -264,6 +278,7 @@ impl SchemaBuilder {
264278
pub(crate) fn build_inner(self) -> (Schema, DiagnosticList) {
265279
let SchemaBuilder {
266280
adopt_orphan_extensions,
281+
ignore_builtin_redefinitions: _allow_builtin_redefinitions,
267282
mut schema,
268283
schema_definition,
269284
orphan_type_extensions,

crates/apollo-compiler/tests/validation/types.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Ported from graphql-js, 2023-11-16
22
//! https://github.com/graphql/graphql-js/blob/0b7590f0a2b65e6210da2e49be0d8e6c27781af2/src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts
3+
use apollo_compiler::schema::SchemaBuilder;
34
use apollo_compiler::validation::Valid;
45
use apollo_compiler::ExecutableDocument;
56
use apollo_compiler::Schema;
7+
use expect_test::expect;
68
use expect_test::Expect;
79
use std::sync::OnceLock;
810
use unindent::unindent;
@@ -2016,3 +2018,34 @@ mod variable_default_values {
20162018
);
20172019
}
20182020
}
2021+
2022+
#[test]
2023+
fn handles_built_in_type_redefinition() {
2024+
let schema = r#"
2025+
scalar String
2026+
2027+
type Query {
2028+
foo: String
2029+
}
2030+
"#;
2031+
2032+
let errors = Schema::parse_and_validate(schema, "schema.graphql")
2033+
.expect_err("invalid schema")
2034+
.errors;
2035+
let expected = expect![[r#"
2036+
Error: built-in scalar definitions must be omitted
2037+
╭─[ schema.graphql:2:1 ]
2038+
2039+
2 │ scalar String
2040+
│ ──────┬──────
2041+
│ ╰──────── remove this scalar definition
2042+
───╯
2043+
"#]];
2044+
expected.assert_eq(&errors.to_string());
2045+
2046+
let builder = SchemaBuilder::new().ignore_builtin_redefinitions();
2047+
let _ = builder
2048+
.parse(schema, "schema.graphql")
2049+
.build()
2050+
.expect("schema parsed successfully");
2051+
}

0 commit comments

Comments
 (0)