Created
February 14, 2021 20:49
-
-
Save kilink/9ff125664108565b83e1f04f54602086 to your computer and use it in GitHub Desktop.
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
import graphql.parser.InvalidSyntaxException | |
import graphql.parser.Parser | |
import graphql.schema.GraphQLSchema | |
import graphql.schema.idl.RuntimeWiring | |
import graphql.schema.idl.SchemaGenerator | |
import graphql.schema.idl.SchemaParser | |
import graphql.validation.ValidationError | |
import graphql.validation.Validator | |
import org.junit.jupiter.api.Test | |
import org.junit.jupiter.api.fail | |
class QueryValidationTest { | |
@Test | |
fun `query with no color field is valid`() { | |
val schema = createSchema(""" | |
input ColorFilter { | |
color: String = "red" | |
} | |
type Query { | |
things(filter: ColorFilter!): [String!]! | |
} | |
""".trimMargin()) | |
val query = """ | |
query { | |
things(filter: {}) | |
} | |
""".trimIndent() | |
assertValid(query, schema) | |
} | |
@Test | |
fun `query with explicit null color is valid`() { | |
val schema = createSchema(""" | |
input ColorFilter { | |
color: String = "red" | |
} | |
type Query { | |
things(filter: ColorFilter!): [String!]! | |
} | |
""".trimMargin()) | |
val query = """ | |
query { | |
things(filter: {color: null}) | |
} | |
""".trimIndent() | |
assertValid(query, schema) | |
} | |
@Test | |
fun `query with no color field is valid when color is non-nullable`() { | |
val schema = createSchema(""" | |
input ColorFilter { | |
color: String! = "red" | |
} | |
type Query { | |
things(filter: ColorFilter!): [String!]! | |
} | |
""".trimMargin()) | |
val query = """ | |
query { | |
things(filter: {}) | |
} | |
""".trimIndent() | |
assertValid(query, schema) | |
} | |
@Test | |
fun `query with explicit null value is not valid when color is non-nullable`() { | |
val schema = createSchema(""" | |
input ColorFilter { | |
color: String! = "red" | |
} | |
type Query { | |
things(filter: ColorFilter!): [String!]! | |
} | |
""".trimMargin()) | |
val query = """ | |
query { | |
things(filter: {color: null}) | |
} | |
""".trimIndent() | |
assertInvalid(query, schema) | |
} | |
private fun createSchema(input: String): GraphQLSchema { | |
val schema = SchemaParser().parse(input) | |
return SchemaGenerator().makeExecutableSchema(schema, RuntimeWiring.newRuntimeWiring().build()) | |
} | |
private fun assertValid(input: String, schema: GraphQLSchema) { | |
val errors = validate(input, schema) | |
if (errors.isNotEmpty()) { | |
fail("query has validation errors: ${errors.joinToString()}") | |
} | |
} | |
private fun assertInvalid(input: String, schema: GraphQLSchema) { | |
val errors = validate(input, schema) | |
if (errors.isEmpty()) { | |
fail("query was valid, but expected it not to be") | |
} | |
} | |
private fun validate(input: String, schema: GraphQLSchema): List<ValidationError> { | |
val document = try { | |
Parser().parseDocument(input) | |
} catch (exc: InvalidSyntaxException) { | |
fail("query has invalid syntax: $exc") | |
} | |
val validator = Validator() | |
return validator.validateDocument(schema, document) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment