-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable support for specifying custom DataFetchers (#152)
* Enable coroutine support in default KotlinDataFetcher * Enable support for specifying custom DataFetchers * fix compilation errors * simplify KotlinDataFetcherFactoryProvider * add accidentally removed data fetcher tests * renaming variable name
- Loading branch information
1 parent
4d230fc
commit 92c58be
Showing
19 changed files
with
144 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...c/main/kotlin/com/expedia/graphql/sample/dataFetchers/CustomDataFetcherFactoryProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.expedia.graphql.sample.dataFetchers | ||
|
||
import com.expedia.graphql.execution.KotlinDataFetcherFactoryProvider | ||
import com.expedia.graphql.hooks.SchemaGeneratorHooks | ||
import graphql.schema.DataFetcherFactory | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* Custom DataFetcherFactory provider that returns custom Spring based DataFetcherFactory for resolving lateinit properties. | ||
*/ | ||
class CustomDataFetcherFactoryProvider( | ||
private val springDataFetcherFactory: SpringDataFetcherFactory, | ||
hooks: SchemaGeneratorHooks | ||
) : KotlinDataFetcherFactoryProvider(hooks) { | ||
|
||
override fun propertyDataFetcherFactory(kClazz: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory<Any> = | ||
if (kProperty.isLateinit) { | ||
springDataFetcherFactory | ||
} else { | ||
super.propertyDataFetcherFactory(kClazz, kProperty) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
example/src/main/kotlin/com/expedia/graphql/sample/extension/CustomSchemaGeneratorHooks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...le/src/main/kotlin/com/expedia/graphql/sample/validation/DataFetcherExecutionValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ql/hooks/DataFetcherExecutionPredicate.kt → ...xecution/DataFetcherExecutionPredicate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/com/expedia/graphql/execution/KotlinDataFetcherFactoryProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.expedia.graphql.execution | ||
|
||
import com.expedia.graphql.hooks.SchemaGeneratorHooks | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import graphql.schema.DataFetcherFactories | ||
import graphql.schema.DataFetcherFactory | ||
import graphql.schema.PropertyDataFetcher | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* DataFetcherFactoryProvider is used during schema construction to obtain [DataFetcherFactory] that should be used | ||
* for target function and property resolution. | ||
*/ | ||
open class KotlinDataFetcherFactoryProvider(private val hooks: SchemaGeneratorHooks) { | ||
|
||
private val defaultObjectMapper = jacksonObjectMapper() | ||
|
||
/** | ||
* Retrieve instance of [DataFetcherFactory] that will be used to resolve target function. | ||
* | ||
* @param target target object that performs the data fetching or NULL if target object should be dynamically | ||
* retrieved during data fetcher execution from [graphql.schema.DataFetchingEnvironment] | ||
* @param kFunction Kotlin function being invoked | ||
*/ | ||
open fun functionDataFetcherFactory(target: Any?, kFunction: KFunction<*>): DataFetcherFactory<Any> = | ||
DataFetcherFactories.useDataFetcher( | ||
FunctionDataFetcher( | ||
target = target, | ||
fn = kFunction, | ||
objectMapper = defaultObjectMapper, | ||
executionPredicate = hooks.dataFetcherExecutionPredicate)) | ||
|
||
/** | ||
* Retrieve instance of [DataFetcherFactory] that will be used to resolve target property. | ||
* | ||
* @param kClass parent class that contains this property | ||
* @param kProperty Kotlin property that should be resolved | ||
*/ | ||
open fun propertyDataFetcherFactory(kClass: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory<Any> = | ||
DataFetcherFactories.useDataFetcher(PropertyDataFetcher(kProperty.name)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...l/dataFetchers/DataFetchPredicateTests.kt → ...phql/execution/DataFetchPredicateTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.