Skip to content

Commit 7e6c51e

Browse files
author
Shane Myrick
committed
Revert deletion of annotation class
Undo the deletion of the old annotation and instead make these changes a minor feature addition
1 parent e6ed5c8 commit 7e6c51e

File tree

13 files changed

+55
-19
lines changed

13 files changed

+55
-19
lines changed

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/context/MyGraphQLContextFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Component
99
* [GraphQLContextFactory] that generates [MyGraphQLContext] that will be available when processing GraphQL requests.
1010
*/
1111
@Component
12-
class MyGraphQLContextFactory : GraphQLContextFactory {
12+
class MyGraphQLContextFactory : GraphQLContextFactory<MyGraphQLContext> {
1313

1414
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): MyGraphQLContext = MyGraphQLContext(
1515
myCustomValue = request.headers.getFirst("MyHeader") ?: "defaultContext",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.annotations
18+
19+
/**
20+
* Mark something for the GraphQL context.
21+
*/
22+
annotation class GraphQLContext

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/extensions/kParameterExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import com.expediagroup.graphql.exceptions.CouldNotGetNameOfKParameterException
2020
import com.expediagroup.graphql.execution.GraphQLContext
2121
import graphql.schema.DataFetchingEnvironment
2222
import kotlin.reflect.KParameter
23+
import kotlin.reflect.full.findAnnotation
2324
import kotlin.reflect.full.isSubclassOf
25+
import com.expediagroup.graphql.annotations.GraphQLContext as GraphQLContextAnnotation
2426

2527
internal fun KParameter.isInterface() = this.type.getKClass().isInterface()
2628

2729
internal fun KParameter.isList() = this.type.getKClass().isSubclassOf(List::class)
2830

29-
internal fun KParameter.isGraphQLContext() = this.type.getKClass().isSubclassOf(GraphQLContext::class)
31+
internal fun KParameter.isGraphQLContext() = this.type.getKClass().isSubclassOf(GraphQLContext::class) || this.findAnnotation<GraphQLContextAnnotation>() != null
3032

3133
internal fun KParameter.isDataFetchingEnvironment() = this.type.classifier == DataFetchingEnvironment::class
3234

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/execution/FunctionDataFetcherTest.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import kotlin.test.assertFalse
3030
import kotlin.test.assertNotNull
3131
import kotlin.test.assertNull
3232
import kotlin.test.assertTrue
33+
import com.expediagroup.graphql.annotations.GraphQLContext as GraphQLContextAnnotation
3334

3435
internal class FunctionDataFetcherTest {
3536

@@ -42,7 +43,9 @@ internal class FunctionDataFetcherTest {
4243

4344
fun printList(items: List<String>) = items.joinToString(separator = ":")
4445

45-
fun context(myContext: MyContext) = myContext.value
46+
fun context(@GraphQLContextAnnotation string: String) = string
47+
48+
fun contextClass(myContext: MyContext) = myContext.value
4649

4750
fun dataFetchingEnvironment(environment: DataFetchingEnvironment): String = environment.field.name
4851

@@ -93,9 +96,17 @@ internal class FunctionDataFetcherTest {
9396
}
9497

9598
@Test
96-
fun `valid target with context`() {
99+
fun `valid target with context annotation`() {
97100
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::context)
98101
val mockEnvironmet: DataFetchingEnvironment = mockk()
102+
every { mockEnvironmet.getContext<String>() } returns "foo"
103+
assertEquals(expected = "foo", actual = dataFetcher.get(mockEnvironmet))
104+
}
105+
106+
@Test
107+
fun `valid target with context class`() {
108+
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::contextClass)
109+
val mockEnvironmet: DataFetchingEnvironment = mockk()
99110
every { mockEnvironmet.getContext<MyContext>() } returns MyContext("foo")
100111
assertEquals(expected = "foo", actual = dataFetcher.get(mockEnvironmet))
101112
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/extensions/KFunctionExtensionsKtTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.expediagroup.graphql.execution.GraphQLContext
2121
import graphql.schema.DataFetchingEnvironment
2222
import org.junit.jupiter.api.Test
2323
import kotlin.test.assertEquals
24+
import com.expediagroup.graphql.annotations.GraphQLContext as GraphQLContextAnnotation
2425

2526
internal class KFunctionExtensionsKtTest {
2627

@@ -39,7 +40,7 @@ internal class KFunctionExtensionsKtTest {
3940
}
4041

4142
@Test
42-
fun `getValidArguments should ignore GraphQLContext`() {
43+
fun `getValidArguments should ignore GraphQLContext annotations and classes`() {
4344
val args = TestingClass::context.getValidArguments()
4445
assertEquals(expected = 1, actual = args.size)
4546
assertEquals(expected = "notContext", actual = args.first().getName())
@@ -57,7 +58,7 @@ internal class KFunctionExtensionsKtTest {
5758

5859
fun ignored(@GraphQLIgnore ignoredArg: String, notIgnored: String) = "$ignoredArg and $notIgnored"
5960

60-
fun context(contextArg: GraphQLContext, notContext: String) = "$contextArg and $notContext"
61+
fun context(@GraphQLContextAnnotation contextAnnation: String, contextClass: GraphQLContext, notContext: String) = "Value was $notContext"
6162

6263
fun dataFetchingEnvironment(environment: DataFetchingEnvironment, notEnvironment: String): String = "${environment.field.name} and $notEnvironment"
6364
}

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLAutoConfiguration.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ class GraphQLAutoConfiguration {
123123

124124
@Bean
125125
@ConditionalOnMissingBean
126-
fun graphQLContextFactory(): GraphQLContextFactory = EmptyContextFactory
126+
fun graphQLContextFactory(): GraphQLContextFactory<*> = EmptyContextFactory
127127

128128
@Bean
129129
@ConditionalOnMissingBean
130130
fun contextWebFilter(
131131
config: GraphQLConfigurationProperties,
132-
graphQLContextFactory: GraphQLContextFactory
132+
graphQLContextFactory: GraphQLContextFactory<*>
133133
): ContextWebFilter = ContextWebFilter(config, graphQLContextFactory)
134134
}

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/ContextWebFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const val GRAPHQL_CONTEXT_FILTER_ODER = 0
3333
/**
3434
* Default web filter that populates GraphQL context in the reactor subscriber context.
3535
*/
36-
open class ContextWebFilter(config: GraphQLConfigurationProperties, private val contextFactory: GraphQLContextFactory) : WebFilter, Ordered {
36+
open class ContextWebFilter(config: GraphQLConfigurationProperties, private val contextFactory: GraphQLContextFactory<Any>) : WebFilter, Ordered {
3737
private val graphQLRoute = enforceAbsolutePath(config.endpoint)
3838
private val subscriptionsRoute = enforceAbsolutePath(config.subscriptions.endpoint)
3939

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/execution/GraphQLContextFactory.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ const val GRAPHQL_CONTEXT_KEY = "graphQLContext"
2929
/**
3030
* Factory that generates GraphQL context.
3131
*/
32-
interface GraphQLContextFactory {
32+
interface GraphQLContextFactory<out T : Any> {
3333

3434
/**
3535
* Generate GraphQL context based on the incoming request and the corresponding response.
3636
*/
37-
suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): GraphQLContext
37+
suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): T
3838
}
3939

4040
/**
4141
* Default context factory that generates empty GraphQL context.
4242
*/
43-
internal object EmptyContextFactory : GraphQLContextFactory {
43+
internal object EmptyContextFactory : GraphQLContextFactory<GraphQLContext> {
4444

4545
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): EmptyGraphQLContext = EmptyGraphQLContext()
4646
}

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/SchemaConfigurationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ class SchemaConfigurationTest {
166166
.build()
167167

168168
@Bean
169-
fun myCustomContextFactory(): GraphQLContextFactory = mockk()
169+
fun myCustomContextFactory(): GraphQLContextFactory<Map<String, Any>> = mockk()
170170

171171
@Bean
172172
fun myDataLoaderRegistryFactory(): DataLoaderRegistryFactory = mockk()
173173

174174
@Bean
175175
fun myCustomContextWebFilter(
176176
config: GraphQLConfigurationProperties,
177-
graphQLContextFactory: GraphQLContextFactory
177+
graphQLContextFactory: GraphQLContextFactory<*>
178178
): ContextWebFilter = object : ContextWebFilter(config, graphQLContextFactory) {
179179
private val regex = config.endpoint.toRegex()
180180

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/context/ContextWebFilterTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ContextWebFilterTest {
5454
}
5555
}
5656

57-
val simpleFactory: GraphQLContextFactory = mockk {
57+
val simpleFactory: GraphQLContextFactory<Any> = mockk {
5858
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
5959
}
6060

@@ -90,7 +90,7 @@ class ContextWebFilterTest {
9090
}
9191
}
9292

93-
val simpleFactory: GraphQLContextFactory = mockk {
93+
val simpleFactory: GraphQLContextFactory<Any> = mockk {
9494
coEvery { generateContext(any(), any()) } returns EmptyGraphQLContext()
9595
}
9696

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/context/GraphQLContextFactoryIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class GraphQLContextFactoryIT(@Autowired private val testClient: WebTestClient)
6767

6868
@Bean
6969
@ExperimentalCoroutinesApi
70-
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
70+
fun customContextFactory(): GraphQLContextFactory<CustomContext> = object : GraphQLContextFactory<CustomContext> {
7171
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): CustomContext {
7272
val firstValue = coroutineContext[ReactorContext]?.context?.get<String>("firstFilterValue")
7373
return CustomContext(

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/execution/SubscriptionWebSocketHandlerIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SubscriptionWebSocketHandlerIT(@LocalServerPort private var port: Int) {
170170
fun subscription(): Subscription = SimpleSubscription()
171171

172172
@Bean
173-
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
173+
fun customContextFactory(): GraphQLContextFactory<SubscriptionContext> = object : GraphQLContextFactory<SubscriptionContext> {
174174
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): SubscriptionContext = SubscriptionContext(
175175
value = request.headers.getFirst("X-Custom-Header") ?: "default"
176176
)

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/routes/RouteConfigurationIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RouteConfigurationIT(@Autowired private val testClient: WebTestClient) {
5050
fun query(): Query = SimpleQuery()
5151

5252
@Bean
53-
fun customContextFactory(): GraphQLContextFactory = object : GraphQLContextFactory {
53+
fun customContextFactory(): GraphQLContextFactory<CustomContext> = object : GraphQLContextFactory<CustomContext> {
5454
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): CustomContext = CustomContext(
5555
value = request.headers.getFirst("X-Custom-Header") ?: "default"
5656
)

0 commit comments

Comments
 (0)