Skip to content

Commit

Permalink
Be robust to SDL schemas that already contain builtin definitions (#3241
Browse files Browse the repository at this point in the history
)
  • Loading branch information
martinbonnin authored Jul 15, 2021
1 parent 6e2fd16 commit 57112ca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ private fun GQLDocument.withoutDefinitions(definitions: List<GQLDefinition>): 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<GQLDefinition>): GQLDocument {
val mergedDefinitions = definitions.toMutableList()
val mergedDefinitions = this.definitions.toMutableList()

definitions.forEach { builtInTypeDefinition ->
check(builtInTypeDefinition is GQLNamed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 57112ca

Please sign in to comment.