Skip to content

Commit d4fe298

Browse files
committed
Added unit test to verify incorrect ID input when used in list
1 parent 74df363 commit d4fe298

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package graphql.kickstart.tools
2+
3+
import graphql.GraphQL
4+
import graphql.execution.AsyncExecutionStrategy
5+
import graphql.schema.GraphQLSchema
6+
import spock.lang.Shared
7+
import spock.lang.Specification
8+
9+
class BuiltInLongIdSpec extends Specification {
10+
11+
@Shared
12+
GraphQL gql
13+
14+
def setupSpec() {
15+
GraphQLSchema schema = SchemaParser.newParser().schemaString('''\
16+
type Query {
17+
itemByLongId(id: ID!): Item!
18+
itemsByLongIds(ids: [ID!]!): [Item!]!
19+
}
20+
21+
type Item {
22+
id: ID!
23+
}
24+
'''.stripIndent())
25+
.resolvers(new QueryWithLongItemResolver())
26+
.build()
27+
.makeExecutableSchema()
28+
gql = GraphQL.newGraphQL(schema)
29+
.queryExecutionStrategy(new AsyncExecutionStrategy())
30+
.build()
31+
}
32+
33+
def "supports Long as ID as input"() {
34+
when:
35+
def data = Utils.assertNoGraphQlErrors(gql) {
36+
'''
37+
{
38+
itemByLongId(id: 1) {
39+
id
40+
}
41+
}
42+
'''
43+
}
44+
45+
then:
46+
data.itemByLongId != null
47+
data.itemByLongId.id == "1"
48+
}
49+
50+
def "supports list of Long as ID as input"() {
51+
when:
52+
def data = Utils.assertNoGraphQlErrors(gql) {
53+
'''
54+
{
55+
itemsByLongIds(id: [1,2,3]) {
56+
id
57+
}
58+
}
59+
'''
60+
}
61+
62+
then:
63+
data.itemsByLongIds != null
64+
data.itemsByLongIds.size == 3
65+
data.itemsByLongIds[0].id == "1"
66+
}
67+
68+
class QueryWithLongItemResolver implements GraphQLQueryResolver {
69+
Item itemByLongId(Long id) {
70+
new Item(id: id)
71+
}
72+
73+
List<Item> itemsByLongIds(List<Long> ids) {
74+
ids.collect { new Item(id: it) }
75+
}
76+
}
77+
78+
class Item {
79+
Long id
80+
}
81+
}

src/test/groovy/graphql/kickstart/tools/EndToEndSpec.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,5 +664,4 @@ class EndToEndSpec extends Specification {
664664
data.arrayItems.collect { it.name } == ['item1', 'item2']
665665
}
666666
667-
668667
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package graphql.kickstart.tools
2+
3+
fun createLongSchema() = SchemaParser.newParser()
4+
.schemaString(schemaDefinition)
5+
.resolvers(LongQuery())
6+
.build()
7+
.makeExecutableSchema()
8+
9+
private const val schemaDefinition = """
10+
11+
"""
12+
13+
class LongQuery : GraphQLQueryResolver {
14+
fun itemByLongId(id: Long) = LongItem(id)
15+
fun itemsByListOfLongId(ids: List<Long>) = ids.map { id -> LongItem(id) }
16+
}
17+
18+
data class LongItem(val id: Long)

src/test/kotlin/graphql/kickstart/tools/EndToEndSpecHelper.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fun createSchema() = SchemaParser.newParser()
2626
.build()
2727
.makeExecutableSchema()
2828

29-
val schemaDefinition = """
29+
private const val schemaDefinition = """
3030
3131
## Private comment!
3232
scalar UUID
@@ -82,9 +82,6 @@ type Query {
8282
coroutineItems: [Item!]!
8383
8484
arrayItems: [Item!]!
85-
86-
byLongId(id: ID!): Item!
87-
byListOfLongId(ids: [ID!]!): [Item!]!
8885
}
8986
9087
type ExtendedType {

0 commit comments

Comments
 (0)