Skip to content

Commit

Permalink
Enable union validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf committed Jul 30, 2024
1 parent fe02676 commit ac098b9
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 37 deletions.
72 changes: 38 additions & 34 deletions lib/src/main/java/graphql/nadel/validation/NadelUnionValidation.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
package graphql.nadel.validation

import graphql.nadel.validation.NadelSchemaValidationError.UnionHasExtraType
import graphql.nadel.validation.util.NadelSchemaUtil
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLUnionType

internal class NadelUnionValidation(
private val typeValidation: NadelTypeValidation,
) {
fun validate(
schemaElement: NadelServiceSchemaElement,
): List<NadelSchemaValidationError> {
return emptyList()
return if (schemaElement.overall is GraphQLUnionType && schemaElement.underlying is GraphQLUnionType) {
schemaElement.overall.types
.flatMap { memberOverallType ->
val memberUnderlyingType =
NadelSchemaUtil.getUnderlyingType(memberOverallType, schemaElement.service)

// return if (schemaElement.overall is GraphQLUnionType && schemaElement.underlying is GraphQLUnionType) {
// schemaElement.overall.types
// .flatMap { unionOverallType ->
// val unionUnderlyingType = NadelSchemaUtil.getUnderlyingType(unionOverallType, schemaElement.service)
//
// if (!schemaElement.underlying.types.contains(unionUnderlyingType)) {
// listOf(
// UnionHasExtraType(
// service = schemaElement.service,
// unionType = schemaElement.overall,
// extraType = unionOverallType as GraphQLObjectType,
// ),
// )
// } else if (unionUnderlyingType == null) {
// listOf(
// MissingUnderlyingType(
// service = schemaElement.service,
// overallType = unionOverallType,
// ),
// )
// } else {
// typeValidation.validate(
// NadelServiceSchemaElement(
// service = schemaElement.service,
// overall = unionOverallType,
// underlying = unionUnderlyingType,
// ),
// )
// }
// }
// } else {
// emptyList()
// }
if (memberUnderlyingType == null) {
listOf(
NadelSchemaValidationError.MissingUnderlyingType(
service = schemaElement.service,
overallType = memberOverallType,
),
)
} else if (!schemaElement.underlying.types.contains(memberUnderlyingType)) {
listOf(
UnionHasExtraType(
service = schemaElement.service,
unionType = schemaElement.overall,
extraType = memberOverallType as GraphQLObjectType,
),
)
} else {
typeValidation.validate(
NadelServiceSchemaElement(
service = schemaElement.service,
overall = memberOverallType,
underlying = memberUnderlyingType,
),
)
}
}
} else {
emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// @formatter:off
package graphql.nadel.tests.next.fixtures.execution

import graphql.nadel.tests.next.ExpectedNadelResult
import graphql.nadel.tests.next.ExpectedServiceCall
import graphql.nadel.tests.next.TestSnapshot
import graphql.nadel.tests.next.listOfJsonStrings
import kotlin.Suppress
import kotlin.collections.List
import kotlin.collections.listOf

private suspend fun main() {
graphql.nadel.tests.next.update<AllUnionTypeConditionsUnionTest>()
}

/**
* This class is generated. Do NOT modify.
*
* Refer to [graphql.nadel.tests.next.UpdateTestSnapshots
*/
@Suppress("unused")
public class AllUnionTypeConditionsUnionTestSnapshot : TestSnapshot() {
/**
* Query
*
* ```graphql
* query {
* abstract {
* ... on Issue {
* __typename
* key
* }
* ... on User {
* name
* }
* }
* }
* ```
*
* Variables
*
* ```json
* {}
* ```
*/
override val calls: List<ExpectedServiceCall> = listOf(
ExpectedServiceCall(
service = "abstract",
query = """
| {
| abstract {
| ... on Issue {
| __typename
| key
| }
| ... on User {
| name
| }
| }
| }
""".trimMargin(),
variables = "{}",
result = """
| {
| "data": {
| "abstract": [
| {
| "name": "Hello"
| },
| {
| "__typename": "Issue",
| "key": "HEL"
| }
| ]
| }
| }
""".trimMargin(),
delayedResults = listOfJsonStrings(
),
),
)

/**
* Combined Result
*
* ```json
* {
* "data": {
* "abstract": [
* {
* "name": "Hello"
* },
* {
* "__typename": "Issue",
* "key": "HEL"
* }
* ]
* }
* }
* ```
*/
override val result: ExpectedNadelResult = ExpectedNadelResult(
result = """
| {
| "data": {
| "abstract": [
| {
| "name": "Hello"
| },
| {
| "__typename": "Issue",
| "key": "HEL"
| }
| ]
| }
| }
""".trimMargin(),
delayedResults = listOfJsonStrings(
),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package graphql.nadel.tests.next.fixtures.execution

import graphql.nadel.tests.next.NadelIntegrationTest
import org.intellij.lang.annotations.Language

class TypenameInUnionSelectionTest : BasicUnionExecutionTestBase(
query = """
query {
abstract {
__typename
}
}
""".trimIndent(),
)

class PartialUnionTypeConditionsUnionTest : BasicUnionExecutionTestBase(
query = """
query {
abstract {
... on Issue {
__typename
key
}
}
}
""".trimIndent(),
)

class AllUnionTypeConditionsUnionTest : BasicUnionExecutionTestBase(
query = """
query {
abstract {
... on Issue {
__typename
key
}
... on User {
name
}
}
}
""".trimIndent(),
)


class UnionAliasToSameResultKeyTest : BasicUnionExecutionTestBase(
query = """
query {
abstract {
__typename
... on Issue {
id: key
}
... on User {
id: name
}
}
}
""".trimIndent(),
)

abstract class BasicUnionExecutionTestBase(
@Language("GraphQL") query: String,
) : NadelIntegrationTest(
query = query,
services = listOf(
Service(
name = "abstract",
overallSchema = """
type Query {
abstract: [Abstract]
}
union Abstract = User | Issue
type User {
name: String
}
type Issue {
key: String
}
""".trimIndent(),
runtimeWiring = { wiring ->
data class User(
val name: String,
)

data class Issue(
val key: String,
)

wiring
.type("Abstract") { type ->
type
.typeResolver { env ->
env.schema.getObjectType(env.getObject<Any>().javaClass.simpleName)
}
}
.type("Query") { type ->
type
.dataFetcher("abstract") { env ->
listOf(
User(
name = "Hello",
),
Issue(
key = "HEL",
),
)
}
}
},
),
),
)
Loading

0 comments on commit ac098b9

Please sign in to comment.