Skip to content

Changes to sdl field of _service object to make it compliant with the… #1

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

Merged
merged 1 commit into from
Aug 20, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlin.reflect.full.findAnnotation
open class FederatedSchemaGeneratorHooks(private val federatedTypeRegistry: FederatedTypeRegistry) : SchemaGeneratorHooks {
private val directiveRegex = "(^#.+$[\\r\\n])?^directive @\\w+.+$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
private val scalarRegex = "(^#.+$[\\r\\n])?^scalar (_FieldSet|_Any)$[\\r\\n]*".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
private val emptyQuery = "^type Query \\{$\\s+^\\}$\\s+".toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE))
private val validator = FederatedSchemaValidator()

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

// SDL returned by _service query should not contain directives or new scalars
val sdl = originalSchema.print()
// SDL returned by _service query should NOT contain
// - default schema definition
// - empty Query type
// - directives
// - new scalars
val sdl = originalSchema.print(includeDefaultSchemaDefinition = false)
.replace(directiveRegex, "")
.replace(scalarRegex, "")
.replace(emptyQuery, "")
.replace("type ${originalQuery.name}", "type ${originalQuery.name} @extends")
.trim()
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, SERVICE_FIELD_DEFINITION.name), DataFetcher { _Service(sdl) })
federatedCodeRegistry.dataFetcher(FieldCoordinates.coordinates(originalQuery.name, entityField.name), DataFetcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertNotNull

// SDL is returned without _entity and _service queries
const val FEDERATED_SERVICE_SDL = """schema {
query: Query
}

const val FEDERATED_SERVICE_SDL = """
interface Product @extends @key(fields : "id") {
id: String! @external
reviews: [Review!]!
Expand All @@ -28,9 +25,6 @@ type Book implements Product @extends @key(fields : "id") {
weight: Float! @external
}

type Query {
}

type Review {
body: String!
id: String!
Expand Down Expand Up @@ -69,6 +63,6 @@ class ServiceQueryResolverTest {
val queryResult = data["_service"] as? Map<*, *>
assertNotNull(queryResult)
val sdl = queryResult["sdl"] as? String
assertEquals(FEDERATED_SERVICE_SDL, sdl)
assertEquals(FEDERATED_SERVICE_SDL.trim(), sdl)
}
}