Skip to content

Commit 4c2652f

Browse files
dariuszkucsmyrick
authored andcommitted
fix: federated _service query SDL should not contain directives (#302)
1 parent 42b2221 commit 4c2652f

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import kotlin.reflect.full.findAnnotation
2323
* Hooks for generating federated GraphQL schema.
2424
*/
2525
open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: FederatedTypeRegistry) : SchemaGeneratorHooks {
26-
26+
private val directiveRegex = "(^#.+$[\\r\\n])?^directive @\\w+.+$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
27+
private val scalarRegex = "(^#.+$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
2728
private val validator = FederatedSchemaValidator()
2829

2930
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
@@ -57,7 +58,11 @@ open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: Fede
5758
val entityField = generateEntityFieldDefinition(entityTypeNames)
5859
federatedQuery.field(entityField)
5960

61+
// SDL returned by _service query should not contain directives or new scalars
6062
val sdl = originalSchema.print()
63+
.replace(directiveRegex, "")
64+
.replace(scalarRegex, "")
65+
.trim()
6166
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, SERVICE_FIELD_DEFINITION.name), DataFetcher { _Service(sdl) })
6267
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, entityField.name), DataFetcher {
6368
val representations: List<Map<String, Any>> = it.getArgument("representations")

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ const val FEDERATED_SERVICE_SDL = """schema {
1515
query: Query
1616
}
1717
18-
#Marks target field as external meaning it will be resolved by federated schema
19-
directive @external on FIELD_DEFINITION
20-
21-
#Marks target object as part of the federated schema
22-
directive @extends on OBJECT | INTERFACE
23-
24-
#Specifies the base type field set that will be selectable by the gateway
25-
directive @provides(fields: _FieldSet!) on FIELD_DEFINITION
26-
27-
#Space separated list of primary keys needed to access federated object
28-
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
29-
30-
#Specifies required input field set from the base type for a resolver
31-
directive @requires(fields: _FieldSet!) on FIELD_DEFINITION
32-
3318
interface Product @extends @key(fields : "id") {
3419
id: String! @external
3520
reviews: [Review!]!
@@ -54,19 +39,7 @@ type Review {
5439
type User @extends @key(fields : "userId") {
5540
name: String! @external
5641
userId: Int! @external
57-
}
58-
59-
#Federation type representing set of fields
60-
scalar _FieldSet
61-
62-
#Directs the executor to include this field or fragment only when the `if` argument is true
63-
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
64-
65-
#Directs the executor to skip this field or fragment when the `if`'argument is true.
66-
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
67-
68-
#Marks the target field/enum value as deprecated
69-
directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITION | ENUM_VALUE"""
42+
}"""
7043

7144
class ServiceQueryResolverTest {
7245

0 commit comments

Comments
 (0)