Skip to content

Commit 4fa29ef

Browse files
author
BURJA Lucian
committed
Clarify test about relay connections, by moving to another class the code that checks if the @uppercase directive is applied correctly.
1 parent 43713f6 commit 4fa29ef

File tree

2 files changed

+92
-29
lines changed

2 files changed

+92
-29
lines changed

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,75 @@ package graphql.kickstart.tools
22

33
import graphql.GraphQL
44
import graphql.execution.AsyncExecutionStrategy
5+
import graphql.relay.Connection
6+
import graphql.relay.SimpleListConnection
7+
import graphql.schema.DataFetcherFactories
8+
import graphql.schema.DataFetchingEnvironment
59
import graphql.schema.GraphQLFieldDefinition
610
import graphql.schema.idl.SchemaDirectiveWiring
711
import graphql.schema.idl.SchemaDirectiveWiringEnvironment
12+
import org.junit.Assert
813
import org.junit.Ignore
914
import org.junit.Test
15+
import java.util.function.BiFunction
1016

1117
class DirectiveTest {
18+
@Test
19+
fun `should apply correctly the @uppercase directive`() {
20+
val schema = SchemaParser.newParser().schemaString("""
21+
directive @uppercase on FIELD_DEFINITION
22+
23+
type Query {
24+
users: UserConnection
25+
}
26+
27+
type UserConnection {
28+
edges: [UserEdge!]!
29+
}
30+
31+
type UserEdge {
32+
node: User!
33+
}
34+
35+
type User {
36+
id: ID!
37+
name: String @uppercase
38+
}
39+
""")
40+
.resolvers(UsersQueryResolver())
41+
.directive("uppercase", UppercaseDirective())
42+
.build()
43+
.makeExecutableSchema()
44+
45+
val gql = GraphQL.newGraphQL(schema)
46+
.queryExecutionStrategy(AsyncExecutionStrategy())
47+
.build()
48+
49+
val result = gql.execute("""
50+
query {
51+
users {
52+
edges {
53+
node {
54+
id
55+
name
56+
}
57+
}
58+
}
59+
}
60+
""")
61+
62+
val expected = mapOf(
63+
"users" to mapOf(
64+
"edges" to listOf(
65+
mapOf("node" to
66+
mapOf("id" to "1", "name" to "LUKE")
67+
)
68+
)
69+
)
70+
)
71+
72+
Assert.assertEquals(expected, result.getData<Map<String, List<*>>>())
73+
}
1274

1375
@Test
1476
@Ignore("Ignore until enums work in directives")
@@ -65,4 +127,32 @@ class DirectiveTest {
65127
return field
66128
}
67129
}
130+
131+
private class UppercaseDirective : SchemaDirectiveWiring {
132+
133+
override fun onField(environment: SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition>): GraphQLFieldDefinition {
134+
val field = environment.element
135+
val parentType = environment.fieldsContainer
136+
137+
val originalDataFetcher = environment.codeRegistry.getDataFetcher(parentType, field)
138+
val wrappedDataFetcher = DataFetcherFactories.wrapDataFetcher(originalDataFetcher, BiFunction { env, value ->
139+
(value as? String)?.toUpperCase()
140+
})
141+
142+
environment.codeRegistry.dataFetcher(parentType, field, wrappedDataFetcher)
143+
144+
return field
145+
}
146+
}
147+
148+
private class UsersQueryResolver : GraphQLQueryResolver {
149+
fun users(env: DataFetchingEnvironment): Connection<User> {
150+
return SimpleListConnection(listOf(User(1L, "Luke"))).get(env)
151+
}
152+
153+
private data class User(
154+
val id: Long,
155+
val name: String
156+
)
157+
}
68158
}

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@ import graphql.GraphQL
44
import graphql.execution.AsyncExecutionStrategy
55
import graphql.relay.Connection
66
import graphql.relay.SimpleListConnection
7-
import graphql.schema.DataFetcherFactories
87
import graphql.schema.DataFetchingEnvironment
9-
import graphql.schema.GraphQLFieldDefinition
10-
import graphql.schema.idl.SchemaDirectiveWiring
11-
import graphql.schema.idl.SchemaDirectiveWiringEnvironment
128
import groovy.lang.Closure
139
import org.junit.Assert
1410
import org.junit.Test
15-
import java.util.function.BiFunction
1611

1712
class RelayConnectionTest {
1813

1914
@Test
2015
fun `should compile relay schema when not using @connection directive`() {
21-
// Note: in addition to testing that a relay schema compiles without the @connection directive,
22-
// this test also verifies that a custom @uppercase directive works as expected
2316
val schema = SchemaParser.newParser().schemaString("""
24-
directive @uppercase on FIELD_DEFINITION
25-
2617
type Query {
2718
users(first: Int, after: String): UserConnection
2819
otherTypes: AnotherTypeConnection
@@ -39,7 +30,7 @@ class RelayConnectionTest {
3930
4031
type User {
4132
id: ID!
42-
name: String @uppercase
33+
name: String
4334
}
4435
4536
type PageInfo {
@@ -59,7 +50,6 @@ class RelayConnectionTest {
5950
}
6051
""")
6152
.resolvers(QueryResolver())
62-
.directive("uppercase", UppercaseDirective())
6353
.build()
6454
.makeExecutableSchema()
6555

@@ -91,7 +81,7 @@ class RelayConnectionTest {
9181
"users" to mapOf(
9282
"edges" to listOf(
9383
mapOf("node" to
94-
mapOf("id" to "1", "name" to "LUKE")
84+
mapOf("id" to "1", "name" to "Luke")
9585
)
9686
)
9787
),
@@ -163,21 +153,4 @@ class RelayConnectionTest {
163153
private data class AnotherType(
164154
val echo: String
165155
)
166-
167-
private class UppercaseDirective : SchemaDirectiveWiring {
168-
169-
override fun onField(environment: SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition>): GraphQLFieldDefinition {
170-
val field = environment.element
171-
val parentType = environment.fieldsContainer
172-
173-
val originalDataFetcher = environment.codeRegistry.getDataFetcher(parentType, field)
174-
val wrappedDataFetcher = DataFetcherFactories.wrapDataFetcher(originalDataFetcher, BiFunction { env, value ->
175-
(value as? String)?.toUpperCase()
176-
})
177-
178-
environment.codeRegistry.dataFetcher(parentType, field, wrappedDataFetcher)
179-
180-
return field
181-
}
182-
}
183156
}

0 commit comments

Comments
 (0)