|
| 1 | +package graphql.kickstart.tools |
| 2 | + |
| 3 | +import graphql.GraphQL |
| 4 | +import org.junit.Test |
| 5 | + |
| 6 | +class GenericObjectTypesTest { |
| 7 | + |
| 8 | + @Test |
| 9 | + fun `generic type arguments that are lists are not erased`() { |
| 10 | + val schema = SchemaParser.newParser() |
| 11 | + .schemaString( |
| 12 | + """ |
| 13 | + type Query { |
| 14 | + test: TestClass! |
| 15 | + } |
| 16 | +
|
| 17 | + type TestClass { |
| 18 | + match: StringListEntityMatch! |
| 19 | + single: StringEntityMatch! |
| 20 | + } |
| 21 | +
|
| 22 | + type StringListEntityMatch { |
| 23 | + text: String! |
| 24 | + value: [String!] |
| 25 | + } |
| 26 | +
|
| 27 | + type StringEntityMatch { |
| 28 | + text: String! |
| 29 | + value: String |
| 30 | + } |
| 31 | + """) |
| 32 | + .resolvers(EntityMatchQueryResolver()) |
| 33 | + .build() |
| 34 | + .makeExecutableSchema() |
| 35 | + val gql = GraphQL.newGraphQL(schema).build() |
| 36 | + |
| 37 | + val data = assertNoGraphQlErrors(gql) { |
| 38 | + """ |
| 39 | + query { |
| 40 | + test { |
| 41 | + match { text value } |
| 42 | + single { text value } |
| 43 | + } |
| 44 | + } |
| 45 | + """ |
| 46 | + } |
| 47 | + |
| 48 | + assertEquals(data["test"], mapOf( |
| 49 | + "match" to mapOf("text" to "x y", "value" to listOf("x", "y")), |
| 50 | + "single" to mapOf("text" to "z", "value" to "z") |
| 51 | + )) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `nested generic type arguments are resolved`() { |
| 56 | + val schema = SchemaParser.newParser() |
| 57 | + .schemaString( |
| 58 | + """ |
| 59 | + type Query { |
| 60 | + characterHit: CharacterHit! |
| 61 | + characterHitPage: CharacterHitPage! |
| 62 | + } |
| 63 | +
|
| 64 | + type CharacterHitPage { |
| 65 | + content: [CharacterHit!]! |
| 66 | + totalElements: Int! |
| 67 | + } |
| 68 | +
|
| 69 | + type CharacterHit { |
| 70 | + score: Float! |
| 71 | + source: Character! |
| 72 | + } |
| 73 | +
|
| 74 | + interface Character { |
| 75 | + name: String! |
| 76 | + } |
| 77 | +
|
| 78 | + type Human implements Character { |
| 79 | + name: String! |
| 80 | + homePlanet: String! |
| 81 | + } |
| 82 | +
|
| 83 | + type Droid implements Character { |
| 84 | + name: String! |
| 85 | + primaryFunction: String! |
| 86 | + } |
| 87 | + """) |
| 88 | + .resolvers(CharacterHitQueryResolver()) |
| 89 | + .dictionary(Human::class, Droid::class) |
| 90 | + .build() |
| 91 | + .makeExecutableSchema() |
| 92 | + val gql = GraphQL.newGraphQL(schema).build() |
| 93 | + |
| 94 | + val data = assertNoGraphQlErrors(gql) { |
| 95 | + """ |
| 96 | + query { |
| 97 | + characterHit { |
| 98 | + score |
| 99 | + source { name } |
| 100 | + } |
| 101 | + characterHitPage { |
| 102 | + totalElements |
| 103 | + content { |
| 104 | + source { |
| 105 | + name |
| 106 | + ... on Human { homePlanet } |
| 107 | + ... on Droid { primaryFunction } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + """ |
| 113 | + } |
| 114 | + |
| 115 | + assertEquals(data["characterHit"], mapOf("score" to 0.5, "source" to mapOf("name" to "Luke"))) |
| 116 | + assertEquals(data["characterHitPage"], mapOf( |
| 117 | + "totalElements" to 2, |
| 118 | + "content" to listOf( |
| 119 | + mapOf("source" to mapOf("name" to "Luke", "homePlanet" to "Tatooine")), |
| 120 | + mapOf("source" to mapOf("name" to "R2-D2", "primaryFunction" to "Astromech")) |
| 121 | + ) |
| 122 | + )) |
| 123 | + } |
| 124 | + |
| 125 | + class EntityMatchQueryResolver : GraphQLQueryResolver { |
| 126 | + fun test(): TestClass = TestClass( |
| 127 | + match = EntityMatch("x y", listOf("x", "y")), |
| 128 | + single = EntityMatch("z", "z") |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + data class TestClass(val match: EntityMatch<List<String>>, val single: EntityMatch<String>) |
| 133 | + |
| 134 | + data class EntityMatch<T>(val text: String, val value: T?) |
| 135 | + |
| 136 | + class CharacterHitQueryResolver : GraphQLQueryResolver { |
| 137 | + fun characterHit(): Hit<Character> = Hit(Human("Luke", "Tatooine"), 0.5) |
| 138 | + fun characterHitPage(): Page<Hit<Character>> = Page( |
| 139 | + listOf(Hit(Human("Luke", "Tatooine"), 1.0), Hit(Droid("R2-D2", "Astromech"), 1.0)), |
| 140 | + 2 |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + class Page<T>(val content: List<T>, val totalElements: Int) |
| 145 | + |
| 146 | + class Hit<T>(val source: T, val score: Double) |
| 147 | + |
| 148 | + interface Character { |
| 149 | + val name: String |
| 150 | + } |
| 151 | + |
| 152 | + class Human(override val name: String, val homePlanet: String) : Character |
| 153 | + |
| 154 | + class Droid(override val name: String, val primaryFunction: String) : Character |
| 155 | +} |
0 commit comments