-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[federation] add support for Fed 2.1 (#1591)
By default, Supergraph schema excludes all custom directives. Federation 2.1 adds new `@composeDirective` that can be used to instruct composition logic to preserve custom directives in the Supergraph schema. ```graphql directive @composeDirective(name: String!) repeatable on SCHEMA ```
- Loading branch information
1 parent
533dc03
commit 7b40e7d
Showing
14 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../main/kotlin/com/expediagroup/graphql/generator/federation/directives/ComposeDirective.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.expediagroup.graphql.generator.federation.directives | ||
|
||
import com.expediagroup.graphql.generator.annotations.GraphQLDirective | ||
import graphql.Scalars | ||
import graphql.introspection.Introspection | ||
import graphql.schema.GraphQLArgument | ||
import graphql.schema.GraphQLNonNull | ||
|
||
/** | ||
* ```graphql | ||
* directive @composeDirective(name: String!) repeatable on SCHEMA | ||
* ``` | ||
* | ||
* By default, Supergraph schema excludes all custom directives. The `@composeDirective` is used to specify custom directives that should be exposed in the Supergraph schema. | ||
* | ||
* Example: | ||
* Given `@custom` directive we can preserve it in the Supergraph schema | ||
* | ||
* ```kotlin | ||
* @GraphQLDirective(name = "custom", locations = [Introspection.DirectiveLocation.FIELD_DEFINITION]) | ||
* annotation class CustomDirective | ||
* | ||
* @ComposeDirective(name = "custom") | ||
* class CustomSchema | ||
* | ||
* class SimpleQuery { | ||
* @CustomDirective | ||
* fun helloWorld(): String = "Hello World" | ||
* } | ||
* ``` | ||
* | ||
* it will generate following schema | ||
* | ||
* ```graphql | ||
* schema @composeDirective(name: "@myDirective") @link(import : ["composeDirective", "extends", "external", "inaccessible", "key", "override", "provides", "requires", "shareable", "tag", "FieldSet"], url : "https://specs.apollo.dev/federation/v2.1") { | ||
* query: Query | ||
* } | ||
* | ||
* directive @custom on FIELD_DEFINITION | ||
* | ||
* type Query { | ||
* helloWorld: String! @custom | ||
* } | ||
* ``` | ||
* | ||
* @see <a href="https://www.apollographql.com/docs/federation/federated-types/federated-directives/#composedirective">@composeDirective definition</a> | ||
*/ | ||
@Repeatable | ||
@GraphQLDirective( | ||
name = COMPOSE_DIRECTIVE_NAME, | ||
description = COMPOSE_DIRECTIVE_DESCRIPTION, | ||
locations = [Introspection.DirectiveLocation.SCHEMA] | ||
) | ||
annotation class ComposeDirective(val name: String) | ||
|
||
internal const val COMPOSE_DIRECTIVE_NAME = "composeDirective" | ||
private const val COMPOSE_DIRECTIVE_DESCRIPTION = "Marks underlying custom directive to be included in the Supergraph schema" | ||
|
||
internal val COMPOSE_DIRECTIVE_TYPE: graphql.schema.GraphQLDirective = graphql.schema.GraphQLDirective.newDirective() | ||
.name(COMPOSE_DIRECTIVE_NAME) | ||
.description(COMPOSE_DIRECTIVE_DESCRIPTION) | ||
.validLocations(Introspection.DirectiveLocation.SCHEMA) | ||
.argument( | ||
GraphQLArgument.newArgument() | ||
.name("name") | ||
.type(GraphQLNonNull.nonNull(Scalars.GraphQLString)) | ||
) | ||
.repeatable(true) | ||
.build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...pediagroup/graphql/generator/federation/data/integration/composeDirective/CustomSchema.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.expediagroup.graphql.generator.federation.data.integration.composeDirective | ||
|
||
import com.expediagroup.graphql.generator.annotations.GraphQLDirective | ||
import com.expediagroup.graphql.generator.federation.directives.ComposeDirective | ||
import graphql.introspection.Introspection | ||
|
||
@ComposeDirective(name = "custom") | ||
class CustomSchema | ||
|
||
@GraphQLDirective(name = "custom", locations = [Introspection.DirectiveLocation.FIELD_DEFINITION]) | ||
annotation class CustomDirective | ||
|
||
class SimpleQuery { | ||
@CustomDirective | ||
fun helloWorld(): String = "Hello World" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...om/expediagroup/graphql/generator/federation/validation/integration/ComposeDirectiveIT.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.expediagroup.graphql.generator.federation.validation.integration | ||
|
||
import com.expediagroup.graphql.generator.TopLevelObject | ||
import com.expediagroup.graphql.generator.extensions.print | ||
import com.expediagroup.graphql.generator.federation.data.integration.composeDirective.CustomSchema | ||
import com.expediagroup.graphql.generator.federation.data.integration.composeDirective.SimpleQuery | ||
import com.expediagroup.graphql.generator.federation.toFederatedSchema | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertDoesNotThrow | ||
|
||
class ComposeDirectiveIT { | ||
|
||
@Test | ||
fun `verifies applying @composeDirective generates valid schema`() { | ||
assertDoesNotThrow { | ||
val schema = toFederatedSchema( | ||
config = federatedTestConfig("com.expediagroup.graphql.generator.federation.data.integration.composeDirective"), | ||
queries = listOf(TopLevelObject(SimpleQuery())), | ||
schemaObject = TopLevelObject(CustomSchema()) | ||
) | ||
|
||
val expected = """ | ||
schema @composeDirective(name : "custom") @link(import : ["@composeDirective", "@extends", "@external", "@inaccessible", "@key", "@override", "@provides", "@requires", "@shareable", "@tag", "FieldSet"], url : "https://specs.apollo.dev/federation/v2.1"){ | ||
query: Query | ||
} | ||
"Marks underlying custom directive to be included in the Supergraph schema" | ||
directive @composeDirective(name: String!) repeatable on SCHEMA | ||
directive @custom on FIELD_DEFINITION | ||
"Links definitions within the document to external schemas." | ||
directive @link(import: [String], url: String!) repeatable on SCHEMA | ||
type Query { | ||
_service: _Service! | ||
helloWorld: String! @custom | ||
} | ||
type _Service { | ||
sdl: String! | ||
} | ||
""".trimIndent() | ||
val actual = schema.print( | ||
includeDirectivesFilter = { directive -> "link" == directive || "composeDirective" == directive || "custom" == directive }, | ||
includeScalarTypes = false | ||
).trim() | ||
Assertions.assertEquals(expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters