|
16 | 16 |
|
17 | 17 | package com.expediagroup.graphql.generator.federation
|
18 | 18 |
|
| 19 | +import com.apollographql.federation.graphqljava.printer.ServiceSDLPrinter.generateServiceSDL |
| 20 | +import com.apollographql.federation.graphqljava.printer.ServiceSDLPrinter.generateServiceSDLV2 |
19 | 21 | import com.expediagroup.graphql.generator.annotations.GraphQLName
|
20 |
| -import com.expediagroup.graphql.generator.directives.DEPRECATED_DIRECTIVE_NAME |
21 | 22 | import com.expediagroup.graphql.generator.directives.DirectiveMetaInformation
|
22 |
| -import com.expediagroup.graphql.generator.extensions.print |
23 | 23 | import com.expediagroup.graphql.generator.federation.directives.EXTENDS_DIRECTIVE_TYPE
|
24 | 24 | import com.expediagroup.graphql.generator.federation.directives.EXTERNAL_DIRECTIVE_TYPE
|
25 | 25 | import com.expediagroup.graphql.generator.federation.directives.FEDERATION_SPEC_URL
|
@@ -61,18 +61,13 @@ import graphql.schema.GraphQLDirective
|
61 | 61 | import graphql.schema.GraphQLObjectType
|
62 | 62 | import graphql.schema.GraphQLSchema
|
63 | 63 | import graphql.schema.GraphQLType
|
64 |
| -import java.util.function.Predicate |
65 | 64 | import kotlin.reflect.KType
|
66 | 65 | import kotlin.reflect.full.findAnnotation
|
67 | 66 |
|
68 | 67 | /**
|
69 | 68 | * Hooks for generating federated GraphQL schema.
|
70 | 69 | */
|
71 | 70 | open class FederatedSchemaGeneratorHooks(private val resolvers: List<FederatedTypeResolver<*>>, private val optInFederationV2: Boolean = false) : SchemaGeneratorHooks {
|
72 |
| - private val scalarDefinitionRegex = "(^\".+\"$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) |
73 |
| - private val emptyQueryRegex = "^type Query @extends \\s*\\{\\s*}\\s*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) |
74 |
| - private val serviceFieldRegex = "\\s*_service: _Service!".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) |
75 |
| - private val serviceTypeRegex = "^type _Service\\s*\\{\\s*sdl: String!\\s*}\\s*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) |
76 | 71 | private val validator = FederatedSchemaValidator()
|
77 | 72 |
|
78 | 73 | private val federationV2OnlyDirectiveNames: Set<String> = setOf(
|
@@ -206,17 +201,11 @@ open class FederatedSchemaGeneratorHooks(private val resolvers: List<FederatedTy
|
206 | 201 | * https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#query_service
|
207 | 202 | */
|
208 | 203 | private fun getFederatedServiceSdl(schema: GraphQLSchema): String {
|
209 |
| - val directivesToInclude: List<String> = federatedDirectiveList().map { it.name }.plus(DEPRECATED_DIRECTIVE_NAME) |
210 |
| - val customDirectivePredicate: Predicate<String> = Predicate { directivesToInclude.contains(it) } |
211 |
| - return schema.print( |
212 |
| - includeDefaultSchemaDefinition = optInFederationV2, |
213 |
| - includeDirectiveDefinitions = false, |
214 |
| - includeDirectivesFilter = customDirectivePredicate |
215 |
| - ).replace(scalarDefinitionRegex, "") |
216 |
| - .replace(serviceFieldRegex, "") |
217 |
| - .replace(serviceTypeRegex, "") |
218 |
| - .replace(emptyQueryRegex, "") |
219 |
| - .trim() |
| 204 | + return if (optInFederationV2) { |
| 205 | + generateServiceSDLV2(schema) |
| 206 | + } else { |
| 207 | + generateServiceSDL(schema, false) |
| 208 | + } |
220 | 209 | }
|
221 | 210 |
|
222 | 211 | /**
|
|
0 commit comments