Skip to content
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

Remove withBuiltInDefinitions() and add flag to dump scalar definitions in SDL #6389

Merged
merged 3 commits into from
Feb 19, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

# Next version

* 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).

# Version 4.1.1

_2025-01-24_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ annotation class ApolloDeprecatedSince(val version: Version) {
v4_0_0,
v4_0_1,
v4_0_2,
v4_1_2,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Schema internal constructor(
@ApolloInternal
fun toMap(): Map<String, Any> {
return mapOf(
"sdl" to GQLDocument(definitions, sourceLocation = null).toSDL(),
"sdl" to GQLDocument(definitions, sourceLocation = null).toSDL(indent = "", includeBuiltInScalarDefinitions = true),
"keyFields" to keyFields.mapValues { it.value.toList().sorted() },
"foreignNames" to foreignNames,
"directivesToStrip" to directivesToStrip,
Expand Down Expand Up @@ -233,7 +233,7 @@ class Schema internal constructor(
@ApolloInternal
fun fromMap(map: Map<String, Any>): Schema {
return Schema(
definitions = combineDefinitions((map["sdl"] as String).parseAsGQLDocument().getOrThrow().definitions, builtinDefinitions(), ConflictResolution.TakeLeft),
definitions = (map["sdl"] as String).parseAsGQLDocument().getOrThrow().definitions,
keyFields = (map["keyFields"]!! as Map<String, Collection<String>>).mapValues { it.value.toSet() },
foreignNames = map["foreignNames"]!! as Map<String, String>,
directivesToStrip = map["directivesToStrip"]!! as List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import okio.Buffer
* Scalars: https://spec.graphql.org/draft/#sel-GAHXJHABAB_D4G
* Directives: https://spec.graphql.org/draft/#sel-FAHnBPLCAACCcooU
*/
@Deprecated("use toFullSchemaGQLDocument instead", ReplaceWith("toFullSchemaGQLDocument()"))
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_1_2)
fun GQLDocument.withBuiltinDefinitions(): GQLDocument {
return withDefinitions(builtinDefinitions())
}
Expand Down Expand Up @@ -201,22 +203,18 @@ private fun GQLDocument.withDefinitions(definitions: List<GQLDefinition>): GQLDo
* See https://spec.graphql.org/draft/#sel-GAHXJHABAB_D4G
*/
@ApolloExperimental
fun GQLDocument.toSDL(indent: String = " "): String {
fun GQLDocument.toSDL(indent: String = " ", includeBuiltInScalarDefinitions: Boolean = false): String {
val buffer = Buffer()
val writer = SDLWriter(buffer, indent)

definitions.forEachIndexed { index, definition ->
when {
definition is GQLScalarTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes -> {
definition is GQLScalarTypeDefinition
&& definition.name in GQLTypeDefinition.builtInTypes
&& !includeBuiltInScalarDefinitions -> {
// Always skip scalar definitions, it's a must in the spec
return@forEachIndexed
}

definition is GQLTypeDefinition && definition.name in GQLTypeDefinition.builtInTypes ||
definition is GQLDirectiveDefinition && definition.name in GQLDirectiveDefinition.builtInDirectives -> {
writer.write(definition)
}

else -> {
writer.write(definition)
}
Expand All @@ -227,3 +225,8 @@ fun GQLDocument.toSDL(indent: String = " "): String {
}
return buffer.readUtf8()
}

@ApolloExperimental
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_1_2)
@Deprecated("This is only kept for backward compatibility reasons. Use the overload instead.", level = DeprecationLevel.HIDDEN)
fun GQLDocument.toSDL(indent: String = " ") = toSDL(indent, false)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.apollo.graphql.ast.test

import com.apollographql.apollo.ast.parseAsGQLDocument
import com.apollographql.apollo.ast.toFullSchemaGQLDocument
import com.apollographql.apollo.ast.toGQLDocument
import com.apollographql.apollo.ast.toSDL
import com.apollographql.apollo.ast.withBuiltinDefinitions
Expand All @@ -18,15 +19,6 @@ class SDLWriterTest {
}
}

@Test
fun typeRedefinitionInspectionIsIgnored() {
val sdlSchema = File("${CWD}/test-fixtures/sdl/type_redefinitions.graphqls")

checkExpected(sdlSchema) {
it.parseAsGQLDocument().getOrThrow().withBuiltinDefinitions().toSDL(" ")
}
}
Comment on lines -21 to -28
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not 100% sure what this was testing. Removed it. Might add new ones later on.


@Test
fun introspectionSchema() {
val jsonSchema = File("${CWD}/test-fixtures/sdl/introspection.json")
Expand Down
157 changes: 0 additions & 157 deletions libraries/apollo-ast/test-fixtures/sdl/type_redefinitions.expected

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.apollographql.apollo.ast.introspection.toIntrospectionSchema
import com.apollographql.apollo.ast.introspection.writeTo
import com.apollographql.apollo.ast.toFullSchemaGQLDocument
import com.apollographql.apollo.ast.toGQLDocument
import com.apollographql.apollo.ast.toSDL
import com.apollographql.apollo.ast.toUtf8
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -46,7 +47,9 @@ abstract class ApolloConvertSchemaTask : DefaultTask() {
}

if (from.isIntrospection()) {
from.toIntrospectionSchema().toGQLDocument().toUtf8(to)
from.toIntrospectionSchema().toGQLDocument().toSDL(includeBuiltInScalarDefinitions = true).let {
to.writeText(it)
}
} else {
from.toGQLDocument().toFullSchemaGQLDocument().toIntrospectionSchema().writeTo(to)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ object SchemaDownloader {
if (sdlSchema == null) {
check(introspectionSchema != null)
// Convert from JSON to SDL
schema.writeText(introspectionSchema.toGQLDocument().toSDL(indent = " "))
schema.writeText(introspectionSchema.toGQLDocument().toSDL(indent = " ", includeBuiltInScalarDefinitions = true))
} else {
// Copy SDL verbatim
schema.writeText(sdlSchema)
Expand Down
Loading