From 57112ca676fb13eb8a1ed422b0b8bcb810a27555 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 15 Jul 2021 23:45:35 +0200 Subject: [PATCH] Be robust to SDL schemas that already contain builtin definitions (#3241) --- .../apollographql/apollo3/ast/gqldocument.kt | 9 +++++++- .../ast/internal/SchemaValidationScope.kt | 2 +- .../src/main/resources/apollo.graphqls | 0 .../src/main/resources/builtins.graphqls | 0 .../apollo3/graphql/ast/test/SchemaTest.kt | 23 +++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) rename {apollo-compiler => apollo-graphql-ast}/src/main/resources/apollo.graphqls (100%) rename {apollo-compiler => apollo-graphql-ast}/src/main/resources/builtins.graphqls (100%) create mode 100644 apollo-graphql-ast/src/test/kotlin/com/apollographql/apollo3/graphql/ast/test/SchemaTest.kt diff --git a/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/gqldocument.kt b/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/gqldocument.kt index 1e984a9f015..b02108f57cc 100644 --- a/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/gqldocument.kt +++ b/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/gqldocument.kt @@ -61,8 +61,15 @@ private fun GQLDocument.withoutDefinitions(definitions: List): GQ ) } +/** + * Adds [definitions] to the [GQLDocument] + * + * If a definition alreay exists, it is kept as is and a warning is logged + * + * See https://spec.graphql.org/draft/#sel-FAHnBPLCAACCcooU + */ private fun GQLDocument.withDefinitions(definitions: List): GQLDocument { - val mergedDefinitions = definitions.toMutableList() + val mergedDefinitions = this.definitions.toMutableList() definitions.forEach { builtInTypeDefinition -> check(builtInTypeDefinition is GQLNamed) { diff --git a/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/internal/SchemaValidationScope.kt b/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/internal/SchemaValidationScope.kt index 66a7cb8ec93..1b3835763ea 100644 --- a/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/internal/SchemaValidationScope.kt +++ b/apollo-graphql-ast/src/main/kotlin/com/apollographql/apollo3/ast/internal/SchemaValidationScope.kt @@ -12,7 +12,7 @@ internal class SchemaValidationScope(document: GQLDocument) : ValidationScope { * The builtin definitions are required to validate directives amongst other * things so add them early in the validation proccess. */ - val allDefinitions = document.definitions + builtinDefinitions() + val allDefinitions = document.withBuiltinDefinitions().definitions override val typeDefinitions = getTypeDefinitions(allDefinitions) override val directiveDefinitions = getDirectives(allDefinitions) diff --git a/apollo-compiler/src/main/resources/apollo.graphqls b/apollo-graphql-ast/src/main/resources/apollo.graphqls similarity index 100% rename from apollo-compiler/src/main/resources/apollo.graphqls rename to apollo-graphql-ast/src/main/resources/apollo.graphqls diff --git a/apollo-compiler/src/main/resources/builtins.graphqls b/apollo-graphql-ast/src/main/resources/builtins.graphqls similarity index 100% rename from apollo-compiler/src/main/resources/builtins.graphqls rename to apollo-graphql-ast/src/main/resources/builtins.graphqls diff --git a/apollo-graphql-ast/src/test/kotlin/com/apollographql/apollo3/graphql/ast/test/SchemaTest.kt b/apollo-graphql-ast/src/test/kotlin/com/apollographql/apollo3/graphql/ast/test/SchemaTest.kt new file mode 100644 index 00000000000..6b60824ed38 --- /dev/null +++ b/apollo-graphql-ast/src/test/kotlin/com/apollographql/apollo3/graphql/ast/test/SchemaTest.kt @@ -0,0 +1,23 @@ +package com.apollographql.apollo3.graphql.ast.test + +import com.apollographql.apollo3.ast.toSchema +import org.junit.Test + +class SchemaTest { + @Test + fun schemaMayContainBuiltinDirectives() { + val schemaString = """ + "Directs the executor to include this field or fragment only when the `if` argument is true" + directive @include( + "Included when true." + if: Boolean! + ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT + + type Query { + foo: Int + } + """.trimIndent() + + schemaString.toSchema() + } +} \ No newline at end of file