Skip to content

Commit 85c444b

Browse files
tobias-fsmyrick
authored and
smyrick
committed
Changes to sdl field of _service object to make it compliant with the… (ExpediaGroup#304)
* Changes to sdl field of _service object to make it compliant with the federation specification - if the Query type is present, it should have the @extends directive, since it extends the query type - the default schema definition should not be included in the sdl - empty queries should not be present in the SDL
1 parent 0868943 commit 85c444b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

graphql-kotlin-federation/src/main/kotlin/com/expedia/graphql/federation/FederatedSchemaGeneratorHooks.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlin.reflect.full.findAnnotation
2525
open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: FederatedTypeRegistry) : SchemaGeneratorHooks {
2626
private val directiveRegex = "(^#.+$[\\r\\n])?^directive @\\w+.+$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
2727
private val scalarRegex = "(^#.+$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
28+
private val emptyQuery = "^type Query \\{$\\s+^\\}$\\s+".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
2829
private val validator = FederatedSchemaValidator()
2930

3031
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
@@ -58,10 +59,16 @@ open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: Fede
5859
val entityField = generateEntityFieldDefinition(entityTypeNames)
5960
federatedQuery.field(entityField)
6061

61-
// SDL returned by _service query should not contain directives or new scalars
62-
val sdl = originalSchema.print()
62+
// SDL returned by _service query should NOT contain
63+
// - default schema definition
64+
// - empty Query type
65+
// - directives
66+
// - new scalars
67+
val sdl = originalSchema.print(includeDefaultSchemaDefinition = false)
6368
.replace(directiveRegex, "")
6469
.replace(scalarRegex, "")
70+
.replace(emptyQuery, "")
71+
.replace("type ${originalQuery.name}", "type ${originalQuery.name} @extends")
6572
.trim()
6673
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, SERVICE_FIELD_DEFINITION.name), DataFetcher { _Service(sdl) })
6774
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, entityField.name), DataFetcher {

graphql-kotlin-federation/src/test/kotlin/com/expedia/graphql/federation/execution/ServiceQueryResolverTest.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import kotlin.test.assertEquals
1111
import kotlin.test.assertNotNull
1212

1313
// SDL is returned without _entity and _service queries
14-
const val FEDERATED_SERVICE_SDL = """schema {
15-
query: Query
16-
}
17-
14+
const val FEDERATED_SERVICE_SDL = """
1815
interface Product @extends @key(fields : "id") {
1916
id: String! @external
2017
reviews: [Review!]!
@@ -28,9 +25,6 @@ type Book implements Product @extends @key(fields : "id") {
2825
weight: Float! @external
2926
}
3027
31-
type Query {
32-
}
33-
3428
type Review {
3529
body: String!
3630
id: String!
@@ -69,6 +63,6 @@ class ServiceQueryResolverTest {
6963
val queryResult = data["_service"] as? Map<*, *>
7064
assertNotNull(queryResult)
7165
val sdl = queryResult["sdl"] as? String
72-
assertEquals(FEDERATED_SERVICE_SDL, sdl)
66+
assertEquals(FEDERATED_SERVICE_SDL.trim(), sdl)
7367
}
7468
}

0 commit comments

Comments
 (0)