Skip to content

Commit bd67fda

Browse files
authored
Allow multiple @link schema extensions (#6284)
* Allow multiple @link schema extensions * Make getImports return the foreign schema for @link
1 parent 578a74b commit bd67fda

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,20 @@ private fun List<GQLSchemaExtension>.getImports(
318318
val schemaExtensions = this
319319

320320
val imports = mutableListOf<Import>()
321-
321+
val linkForeignSchema = ForeignSchema("link", "v1.0", linkDefinitions())
322+
val linkImport = Import(linkForeignSchema, linkForeignSchema.definitions, mapOf("link" to "link"))
322323
schemaExtensions.forEach { schemaExtension ->
323324
schemaExtension.directives.forEach eachDirective@{ gqlDirective ->
324325
if (gqlDirective.name == "link") {
326+
if (!imports.contains(linkImport)) {
327+
imports.add(linkImport)
328+
}
329+
325330
/**
326331
* Validate `@link` using a very minimal schema.
327332
* This ensure we can safely cast the arguments below
328333
*/
329-
val minimalSchema = builtinDefinitions + linkDefinitions()
334+
val minimalSchema = builtinDefinitions + linkForeignSchema.definitions
330335
val scope = DefaultValidationScope(
331336
minimalSchema.filterIsInstance<GQLTypeDefinition>().associateBy { it.name },
332337
minimalSchema.filterIsInstance<GQLDirectiveDefinition>().associateBy { it.name },

libraries/apollo-ast/src/commonTest/kotlin/com/apollographql/apollo/graphql/ast/test/SchemaTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,40 @@ class SchemaTest {
7373
assertEquals("cacheControl", schema.originalDirectiveName("cacheControl"))
7474
}
7575

76+
@Test
77+
fun multipleImportDirectives() {
78+
// language=graphql
79+
val schemaString = """
80+
extend schema
81+
@link(url: "https://specs.apollo.dev/cache/v0.1/", import: ["@cacheControl"])
82+
@link(url: "https://example.com/example/v0.1/", import: ["@example"])
83+
extend schema
84+
@link(url: "https://example.com/example2/v0.1/", import: ["@example2"])
85+
86+
type Query {
87+
foo: Int @cacheControl(maxAge: 100) @example @example2
88+
}
89+
""".trimIndent()
90+
91+
val schema = schemaString.toGQLDocument().validateAsSchema(
92+
SchemaValidationOptions(
93+
addKotlinLabsDefinitions = false,
94+
foreignSchemas = listOf(
95+
cacheControlSchema,
96+
ForeignSchema("example", "v0.1",
97+
listOf("directive @example on FIELD_DEFINITION".parseAsGQLDocument().getOrThrow().definitions.single())
98+
),
99+
ForeignSchema("example2", "v0.1",
100+
listOf("directive @example2 on FIELD_DEFINITION".parseAsGQLDocument().getOrThrow().definitions.single())
101+
)
102+
)
103+
)
104+
).getOrThrow()
105+
106+
assertEquals("cacheControl", schema.originalDirectiveName("cacheControl"))
107+
assertEquals("example", schema.originalDirectiveName("example"))
108+
}
109+
76110
@Test
77111
fun unknownSchemaFails() {
78112
// language=graphql

0 commit comments

Comments
 (0)