Skip to content

Commit c14229e

Browse files
authored
Remove withBuiltInDefinitions() and add flag to dump scalar definitions in SDL (#6389)
* Remove `withBuiltInDefinitions()` and add flag to dump scalar definitions in SDL * add changelog entry * oops, I forgot to actually write the file
1 parent 5a7af90 commit c14229e

File tree

9 files changed

+24
-182
lines changed

9 files changed

+24
-182
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Change Log
22
==========
33

4+
# Next version
5+
6+
* Downloading or converting a SDL schema from introspection now includes scalar definitions. This is required for clients to get a [full view of the schema](https://github.com/graphql/graphql-wg/blob/main/rfcs/FullSchemas.md).
7+
48
# Version 4.1.1
59

610
_2025-01-24_

libraries/apollo-annotations/src/commonMain/kotlin/com/apollographql/apollo/annotations/ApolloDeprecatedSince.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ annotation class ApolloDeprecatedSince(val version: Version) {
3333
v4_0_0,
3434
v4_0_1,
3535
v4_0_2,
36+
v4_1_2,
3637
}
3738
}

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/Schema.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Schema internal constructor(
131131
@ApolloInternal
132132
fun toMap(): Map<String, Any> {
133133
return mapOf(
134-
"sdl" to GQLDocument(definitions, sourceLocation = null).toSDL(),
134+
"sdl" to GQLDocument(definitions, sourceLocation = null).toSDL(indent = "", includeBuiltInScalarDefinitions = true),
135135
"keyFields" to keyFields.mapValues { it.value.toList().sorted() },
136136
"foreignNames" to foreignNames,
137137
"directivesToStrip" to directivesToStrip,
@@ -233,7 +233,7 @@ class Schema internal constructor(
233233
@ApolloInternal
234234
fun fromMap(map: Map<String, Any>): Schema {
235235
return Schema(
236-
definitions = combineDefinitions((map["sdl"] as String).parseAsGQLDocument().getOrThrow().definitions, builtinDefinitions(), ConflictResolution.TakeLeft),
236+
definitions = (map["sdl"] as String).parseAsGQLDocument().getOrThrow().definitions,
237237
keyFields = (map["keyFields"]!! as Map<String, Collection<String>>).mapValues { it.value.toSet() },
238238
foreignNames = map["foreignNames"]!! as Map<String, String>,
239239
directivesToStrip = map["directivesToStrip"]!! as List<String>,

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/gqldocument.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import okio.Buffer
2222
* Scalars: https://spec.graphql.org/draft/#sel-GAHXJHABAB_D4G
2323
* Directives: https://spec.graphql.org/draft/#sel-FAHnBPLCAACCcooU
2424
*/
25+
@Deprecated("use toFullSchemaGQLDocument instead", ReplaceWith("toFullSchemaGQLDocument()"))
26+
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_1_2)
2527
fun GQLDocument.withBuiltinDefinitions(): GQLDocument {
2628
return withDefinitions(builtinDefinitions())
2729
}
@@ -201,22 +203,18 @@ private fun GQLDocument.withDefinitions(definitions: List<GQLDefinition>): GQLDo
201203
* See https://spec.graphql.org/draft/#sel-GAHXJHABAB_D4G
202204
*/
203205
@ApolloExperimental
204-
fun GQLDocument.toSDL(indent: String = " "): String {
206+
fun GQLDocument.toSDL(indent: String = " ", includeBuiltInScalarDefinitions: Boolean = false): String {
205207
val buffer = Buffer()
206208
val writer = SDLWriter(buffer, indent)
207209

208210
definitions.forEachIndexed { index, definition ->
209211
when {
210-
definition is GQLScalarTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes -> {
212+
definition is GQLScalarTypeDefinition
213+
&& definition.name in GQLTypeDefinition.builtInTypes
214+
&& !includeBuiltInScalarDefinitions -> {
211215
// Always skip scalar definitions, it's a must in the spec
212216
return@forEachIndexed
213217
}
214-
215-
definition is GQLTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes ||
216-
definition is GQLDirectiveDefinition && definition.name in GQLDirectiveDefinition.builtInDirectives -> {
217-
writer.write(definition)
218-
}
219-
220218
else -> {
221219
writer.write(definition)
222220
}
@@ -227,3 +225,8 @@ fun GQLDocument.toSDL(indent: String = " "): String {
227225
}
228226
return buffer.readUtf8()
229227
}
228+
229+
@ApolloExperimental
230+
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_1_2)
231+
@Deprecated("This is only kept for backward compatibility reasons. Use the overload instead.", level = DeprecationLevel.HIDDEN)
232+
fun GQLDocument.toSDL(indent: String = " ") = toSDL(indent, false)

libraries/apollo-ast/src/jvmTest/kotlin/com/apollographql/apollo/graphql/ast/test/SDLWriterTest.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.apollographql.apollo.graphql.ast.test
22

33
import com.apollographql.apollo.ast.parseAsGQLDocument
4+
import com.apollographql.apollo.ast.toFullSchemaGQLDocument
45
import com.apollographql.apollo.ast.toGQLDocument
56
import com.apollographql.apollo.ast.toSDL
67
import com.apollographql.apollo.ast.withBuiltinDefinitions
@@ -18,15 +19,6 @@ class SDLWriterTest {
1819
}
1920
}
2021

21-
@Test
22-
fun typeRedefinitionInspectionIsIgnored() {
23-
val sdlSchema = File("${CWD}/test-fixtures/sdl/type_redefinitions.graphqls")
24-
25-
checkExpected(sdlSchema) {
26-
it.parseAsGQLDocument().getOrThrow().withBuiltinDefinitions().toSDL(" ")
27-
}
28-
}
29-
3022
@Test
3123
fun introspectionSchema() {
3224
val jsonSchema = File("${CWD}/test-fixtures/sdl/introspection.json")

libraries/apollo-ast/test-fixtures/sdl/type_redefinitions.expected

Lines changed: 0 additions & 157 deletions
This file was deleted.

libraries/apollo-ast/test-fixtures/sdl/type_redefinitions.graphqls

Lines changed: 0 additions & 4 deletions
This file was deleted.

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/internal/ApolloConvertSchemaTask.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.apollographql.apollo.ast.introspection.toIntrospectionSchema
55
import com.apollographql.apollo.ast.introspection.writeTo
66
import com.apollographql.apollo.ast.toFullSchemaGQLDocument
77
import com.apollographql.apollo.ast.toGQLDocument
8+
import com.apollographql.apollo.ast.toSDL
89
import com.apollographql.apollo.ast.toUtf8
910
import org.gradle.api.DefaultTask
1011
import org.gradle.api.provider.Property
@@ -46,7 +47,9 @@ abstract class ApolloConvertSchemaTask : DefaultTask() {
4647
}
4748

4849
if (from.isIntrospection()) {
49-
from.toIntrospectionSchema().toGQLDocument().toUtf8(to)
50+
from.toIntrospectionSchema().toGQLDocument().toSDL(includeBuiltInScalarDefinitions = true).let {
51+
to.writeText(it)
52+
}
5053
} else {
5154
from.toGQLDocument().toFullSchemaGQLDocument().toIntrospectionSchema().writeTo(to)
5255
}

libraries/apollo-tooling/src/main/kotlin/com/apollographql/apollo/tooling/SchemaDownloader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ object SchemaDownloader {
127127
if (sdlSchema == null) {
128128
check(introspectionSchema != null)
129129
// Convert from JSON to SDL
130-
schema.writeText(introspectionSchema.toGQLDocument().toSDL(indent = " "))
130+
schema.writeText(introspectionSchema.toGQLDocument().toSDL(indent = " ", includeBuiltInScalarDefinitions = true))
131131
} else {
132132
// Copy SDL verbatim
133133
schema.writeText(sdlSchema)

0 commit comments

Comments
 (0)