-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
817 additions
and
37 deletions.
There are no files selected for viewing
72 changes: 38 additions & 34 deletions
72
lib/src/main/java/graphql/nadel/validation/NadelUnionValidation.kt
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
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() | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...in/graphql/nadel/tests/next/fixtures/execution/AllUnionTypeConditionsUnionTestSnapshot.kt
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
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( | ||
), | ||
) | ||
} |
113 changes: 113 additions & 0 deletions
113
test/src/test/kotlin/graphql/nadel/tests/next/fixtures/execution/BasicUnionExecutionTest.kt
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
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", | ||
), | ||
) | ||
} | ||
} | ||
}, | ||
), | ||
), | ||
) |
Oops, something went wrong.