Skip to content

Commit

Permalink
feat: DataFetchingEnvironment extensions to access to entries in Grap…
Browse files Browse the repository at this point in the history
…hQLContext (#1552) (#1553)

* feat: DataFetchingEnvironment extensions to access to data in GraphQLContext

* feat: add copyright
  • Loading branch information
samuelAndalon authored Sep 15, 2022
1 parent c65f2a1 commit 815fec9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.generator.exceptions

import graphql.GraphQLContext
import kotlin.reflect.KClass

/**
* Thrown when [klazz] key was not found in a [GraphQLContext]
*/
class KeyNotFoundInGraphQLContextException(klazz: KClass<*>) : GraphQLKotlinException("GraphQLContext does not contain key ${klazz.simpleName}")
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,35 @@

package com.expediagroup.graphql.generator.extensions

import com.expediagroup.graphql.generator.exceptions.KeyNotFoundInGraphQLContextException
import graphql.GraphQLContext

/**
* Returns a value in the context by KClass key
* @return a value or null
*/
inline fun <reified T> GraphQLContext.get(): T? = get(T::class)
inline fun <reified T> GraphQLContext.get(): T? =
get(T::class)

/**
* Returns a value in the context by KClass key
* @param defaultValue the default value to use if there is no KClass key entry
* @return a value or default value
*/
inline fun <reified T> GraphQLContext.getOrDefault(defaultValue: T): T = getOrDefault(T::class, defaultValue)
inline fun <reified T> GraphQLContext.getOrDefault(defaultValue: T): T =
getOrDefault(T::class, defaultValue)

/**
* Returns a value in the context by KClass key
* @param defaultValue function to invoke if there is no KClass key entry
* @return a value or result of [defaultValue] function
*/
inline fun <reified T> GraphQLContext.getOrElse(defaultValue: () -> T): T = get(T::class) ?: defaultValue.invoke()
inline fun <reified T> GraphQLContext.getOrElse(defaultValue: () -> T): T =
get(T::class) ?: defaultValue.invoke()

/**
* Returns a value in the context by KClass key or [KeyNotFoundInGraphQLContextException] if key was not found
* @return a value or [KeyNotFoundInGraphQLContextException]
*/
inline fun <reified T> GraphQLContext.getOrThrow(): T =
get(T::class) ?: throw KeyNotFoundInGraphQLContextException(T::class)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Expedia, Inc
* Copyright 2022 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,11 @@

package com.expediagroup.graphql.server.extensions

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

/**
* Helper method to get values from a registered DataLoader.
*/
fun <K, V> DataFetchingEnvironment.getValuesFromDataLoader(dataLoaderName: String, keys: List<K>): CompletableFuture<List<V>> {
val loader = getLoaderName<K, V>(dataLoaderName)
val loader = getDataLoader<K, V>(dataLoaderName) ?: throw MissingDataLoaderException(dataLoaderName)
return loader.loadMany(keys, listOf(this.getContext()))
}

private fun <K, V> DataFetchingEnvironment.getLoaderName(dataLoaderName: String) =
this.getDataLoader<K, V>(dataLoaderName) ?: throw MissingDataLoaderException(dataLoaderName)
/**
* Returns a value from the graphQLContext by KClass key
* @return a value or null
*/
inline fun <reified T> DataFetchingEnvironment.getFromContext(): T? =
graphQlContext.get<T>()

/**
* Returns a value from the graphQLContext by KClass key
* @param defaultValue the default value to use if there is no KClass key entry
* @return a value or default value
*/
inline fun <reified T> DataFetchingEnvironment.getFromContextOrDefault(defaultValue: T): T =
graphQlContext.getOrDefault(defaultValue)

/**
* Returns a value from the graphQLContext by KClass key
* @param defaultValue function to invoke if there is no KClass key entry
* @return a value or result of [defaultValue] function
*/
inline fun <reified T> DataFetchingEnvironment.getFromContextOrElse(defaultValue: () -> T): T =
graphQlContext.getOrElse(defaultValue)

/**
* Returns a value from the graphQLContext by KClass key or [KeyNotFoundInGraphQLContextException] if key was not found
* @return a value or [KeyNotFoundInGraphQLContextException]
*/
inline fun <reified T> DataFetchingEnvironment.getFromContextOrThrow(): T =
graphQlContext.getOrThrow()

0 comments on commit 815fec9

Please sign in to comment.