Skip to content

Commit 815fec9

Browse files
feat: DataFetchingEnvironment extensions to access to entries in GraphQLContext (ExpediaGroup#1552) (ExpediaGroup#1553)
* feat: DataFetchingEnvironment extensions to access to data in GraphQLContext * feat: add copyright
1 parent c65f2a1 commit 815fec9

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2022 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.generator.exceptions
18+
19+
import graphql.GraphQLContext
20+
import kotlin.reflect.KClass
21+
22+
/**
23+
* Thrown when [klazz] key was not found in a [GraphQLContext]
24+
*/
25+
class KeyNotFoundInGraphQLContextException(klazz: KClass<*>) : GraphQLKotlinException("GraphQLContext does not contain key ${klazz.simpleName}")

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,35 @@
1616

1717
package com.expediagroup.graphql.generator.extensions
1818

19+
import com.expediagroup.graphql.generator.exceptions.KeyNotFoundInGraphQLContextException
1920
import graphql.GraphQLContext
2021

2122
/**
2223
* Returns a value in the context by KClass key
2324
* @return a value or null
2425
*/
25-
inline fun <reified T> GraphQLContext.get(): T? = get(T::class)
26+
inline fun <reified T> GraphQLContext.get(): T? =
27+
get(T::class)
2628

2729
/**
2830
* Returns a value in the context by KClass key
2931
* @param defaultValue the default value to use if there is no KClass key entry
3032
* @return a value or default value
3133
*/
32-
inline fun <reified T> GraphQLContext.getOrDefault(defaultValue: T): T = getOrDefault(T::class, defaultValue)
34+
inline fun <reified T> GraphQLContext.getOrDefault(defaultValue: T): T =
35+
getOrDefault(T::class, defaultValue)
3336

3437
/**
3538
* Returns a value in the context by KClass key
3639
* @param defaultValue function to invoke if there is no KClass key entry
3740
* @return a value or result of [defaultValue] function
3841
*/
39-
inline fun <reified T> GraphQLContext.getOrElse(defaultValue: () -> T): T = get(T::class) ?: defaultValue.invoke()
42+
inline fun <reified T> GraphQLContext.getOrElse(defaultValue: () -> T): T =
43+
get(T::class) ?: defaultValue.invoke()
44+
45+
/**
46+
* Returns a value in the context by KClass key or [KeyNotFoundInGraphQLContextException] if key was not found
47+
* @return a value or [KeyNotFoundInGraphQLContextException]
48+
*/
49+
inline fun <reified T> GraphQLContext.getOrThrow(): T =
50+
get(T::class) ?: throw KeyNotFoundInGraphQLContextException(T::class)

servers/graphql-kotlin-server/src/main/kotlin/com/expediagroup/graphql/server/extensions/dataFetchingEnvironmentExtensions.kt

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Expedia, Inc
2+
* Copyright 2022 Expedia, Inc
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,11 @@
1616

1717
package com.expediagroup.graphql.server.extensions
1818

19+
import com.expediagroup.graphql.generator.exceptions.KeyNotFoundInGraphQLContextException
20+
import com.expediagroup.graphql.generator.extensions.get
21+
import com.expediagroup.graphql.generator.extensions.getOrDefault
22+
import com.expediagroup.graphql.generator.extensions.getOrElse
23+
import com.expediagroup.graphql.generator.extensions.getOrThrow
1924
import com.expediagroup.graphql.server.exception.MissingDataLoaderException
2025
import graphql.schema.DataFetchingEnvironment
2126
import java.util.concurrent.CompletableFuture
@@ -25,17 +30,44 @@ import java.util.concurrent.CompletableFuture
2530
* The provided key should be the cache key object used to save the value for that particular data loader.
2631
*/
2732
fun <K, V> DataFetchingEnvironment.getValueFromDataLoader(dataLoaderName: String, key: K): CompletableFuture<V> {
28-
val loader = getLoaderName<K, V>(dataLoaderName)
33+
val loader = getDataLoader<K, V>(dataLoaderName) ?: throw MissingDataLoaderException(dataLoaderName)
2934
return loader.load(key, this.getContext())
3035
}
3136

3237
/**
3338
* Helper method to get values from a registered DataLoader.
3439
*/
3540
fun <K, V> DataFetchingEnvironment.getValuesFromDataLoader(dataLoaderName: String, keys: List<K>): CompletableFuture<List<V>> {
36-
val loader = getLoaderName<K, V>(dataLoaderName)
41+
val loader = getDataLoader<K, V>(dataLoaderName) ?: throw MissingDataLoaderException(dataLoaderName)
3742
return loader.loadMany(keys, listOf(this.getContext()))
3843
}
3944

40-
private fun <K, V> DataFetchingEnvironment.getLoaderName(dataLoaderName: String) =
41-
this.getDataLoader<K, V>(dataLoaderName) ?: throw MissingDataLoaderException(dataLoaderName)
45+
/**
46+
* Returns a value from the graphQLContext by KClass key
47+
* @return a value or null
48+
*/
49+
inline fun <reified T> DataFetchingEnvironment.getFromContext(): T? =
50+
graphQlContext.get<T>()
51+
52+
/**
53+
* Returns a value from the graphQLContext by KClass key
54+
* @param defaultValue the default value to use if there is no KClass key entry
55+
* @return a value or default value
56+
*/
57+
inline fun <reified T> DataFetchingEnvironment.getFromContextOrDefault(defaultValue: T): T =
58+
graphQlContext.getOrDefault(defaultValue)
59+
60+
/**
61+
* Returns a value from the graphQLContext by KClass key
62+
* @param defaultValue function to invoke if there is no KClass key entry
63+
* @return a value or result of [defaultValue] function
64+
*/
65+
inline fun <reified T> DataFetchingEnvironment.getFromContextOrElse(defaultValue: () -> T): T =
66+
graphQlContext.getOrElse(defaultValue)
67+
68+
/**
69+
* Returns a value from the graphQLContext by KClass key or [KeyNotFoundInGraphQLContextException] if key was not found
70+
* @return a value or [KeyNotFoundInGraphQLContextException]
71+
*/
72+
inline fun <reified T> DataFetchingEnvironment.getFromContextOrThrow(): T =
73+
graphQlContext.getOrThrow()

0 commit comments

Comments
 (0)