Skip to content

Commit e5df2c8

Browse files
samuelAndalonsamvazquez
and
samvazquez
authored
feat: add GraphQLContext extension functions (ExpediaGroup#1369)
* feat: add GraphQLContext extension functions * feat: add ktDocs Co-authored-by: samvazquez <[email protected]>
1 parent 45d1981 commit e5df2c8

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

generator/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/execution/FunctionDataFetcher.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import com.expediagroup.graphql.generator.extensions.getOrDefault
1920
import com.expediagroup.graphql.generator.internal.extensions.getJavaClass
2021
import com.expediagroup.graphql.generator.internal.extensions.getName
2122
import com.expediagroup.graphql.generator.internal.extensions.getTypeOfFirstArgument
@@ -83,11 +84,10 @@ open class FunctionDataFetcher(
8384
* in a value for this optional parameter. This allows for the default Kotlin values to be used if no mapping is added.
8485
* You can override this behaviour by providing a value for a specific [KParameter] when [mapParameterToValue] is called.
8586
*/
86-
protected open fun getParameters(fn: KFunction<*>, environment: DataFetchingEnvironment): Map<KParameter, Any?> {
87-
return fn.valueParameters
87+
protected open fun getParameters(fn: KFunction<*>, environment: DataFetchingEnvironment): Map<KParameter, Any?> =
88+
fn.valueParameters
8889
.mapNotNull { mapParameterToValue(it, environment) }
8990
.toMap()
90-
}
9191

9292
/**
9393
* Retrieves the provided parameter value in the operation input to pass to the function to execute.
@@ -168,26 +168,22 @@ open class FunctionDataFetcher(
168168
protected open fun runSuspendingFunction(
169169
environment: DataFetchingEnvironment,
170170
parameterValues: Map<KParameter, Any?>
171-
): CompletableFuture<Any?> {
172-
val scope = environment.graphQlContext.getOrDefault(CoroutineScope::class, CoroutineScope(EmptyCoroutineContext))
173-
return scope.future {
171+
): CompletableFuture<Any?> =
172+
environment.graphQlContext.getOrDefault(CoroutineScope(EmptyCoroutineContext)).future {
174173
try {
175174
fn.callSuspendBy(parameterValues)
176175
} catch (exception: InvocationTargetException) {
177176
throw exception.cause ?: exception
178177
}
179178
}
180-
}
181179

182180
/**
183181
* Once all parameters values are properly converted, this function will be called to run a simple blocking function.
184182
* If you need to override the exception handling you can override this method.
185183
*/
186-
protected open fun runBlockingFunction(parameterValues: Map<KParameter, Any?>): Any? {
187-
try {
188-
return fn.callBy(parameterValues)
189-
} catch (exception: InvocationTargetException) {
190-
throw exception.cause ?: exception
191-
}
184+
protected open fun runBlockingFunction(parameterValues: Map<KParameter, Any?>): Any? = try {
185+
fn.callBy(parameterValues)
186+
} catch (exception: InvocationTargetException) {
187+
throw exception.cause ?: exception
192188
}
193189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.extensions
18+
19+
import graphql.GraphQLContext
20+
21+
/**
22+
* Returns a value in the context by KClass key
23+
* @return a value or null
24+
*/
25+
inline fun <reified T> GraphQLContext.get(): T? = get(T::class)
26+
27+
/**
28+
* Returns a value in the context by KClass key
29+
* @param defaultValue the default value to use if there is no KClass key entry
30+
* @return a value or default value
31+
*/
32+
inline fun <reified T> GraphQLContext.getOrDefault(defaultValue: T): T = getOrDefault(T::class, defaultValue)
33+
34+
/**
35+
* Returns a value in the context by KClass key
36+
* @param defaultValue function to invoke if there is no KClass key entry
37+
* @return a value or result of [defaultValue] function
38+
*/
39+
inline fun <reified T> GraphQLContext.getOrElse(defaultValue: () -> T): T = get(T::class) ?: defaultValue.invoke()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 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.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 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.

0 commit comments

Comments
 (0)