Skip to content

Commit af202f0

Browse files
aarestusmyrick
authored andcommitted
Remove custom directives from Federated GraphQL schema _service{sdl} (#374)
* Remove Declared Directive from _service{sdl} * don't remove `deprecated` directive from SDL update name variables regex * add todo to simplify code, once this issue is solved: apollographql/apollo-server#3334
1 parent b02bc91 commit af202f0

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ import kotlin.reflect.full.findAnnotation
4242
* Hooks for generating federated GraphQL schema.
4343
*/
4444
open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: FederatedTypeRegistry) : SchemaGeneratorHooks {
45-
private val directiveRegex = "(^#.+$[\\r\\n])?^directive @\\w+.+$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
46-
private val scalarRegex = "(^#.+$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
47-
private val emptyQuery = "^type Query \\{$\\s+^}$\\s+".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
45+
private val directiveDefinitionRegex = "(^#.+$[\\r\\n])?^directive @\\w+.+$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
46+
private val customDirectivesRegex = "@(?!extends)(?!external)(?!key)(?!provides)(?!requires)(?!deprecated)\\w+(?:\\(.*(?=\\))\\))?".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
47+
private val scalarDefinitionRegex = "(^#.+$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
48+
private val emptyQueryRegex = "^type Query \\{$\\s+^\\}$\\s+".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
4849
private val validator = FederatedSchemaValidator()
4950

5051
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
@@ -79,15 +80,22 @@ open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: Fede
7980
val entityField = generateEntityFieldDefinition(entityTypeNames)
8081
federatedQuery.field(entityField)
8182

82-
// SDL returned by _service query should NOT contain
83-
// - default schema definition
84-
// - empty Query type
85-
// - directives
86-
// - new scalars
83+
/**
84+
* SDL returned by _service query should NOT contain
85+
* - default schema definition
86+
* - empty Query type
87+
* - any directive definitions
88+
* - any custom directives
89+
* - new federated scalars
90+
*/
8791
val sdl = originalSchema.print(includeDefaultSchemaDefinition = false)
88-
.replace(directiveRegex, "")
89-
.replace(scalarRegex, "")
90-
.replace(emptyQuery, "")
92+
/**
93+
* TODO: this can be simplified once this is solved: apollographql/apollo-server#3334
94+
*/
95+
.replace(directiveDefinitionRegex, "")
96+
.replace(scalarDefinitionRegex, "")
97+
.replace(emptyQueryRegex, "")
98+
.replace(customDirectivesRegex, "")
9199
.trim()
92100

93101
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, SERVICE_FIELD_DEFINITION.name), DataFetcher { _Service(sdl) })

0 commit comments

Comments
 (0)