All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.expediagroup.graphql.execution.KotlinDataFetcherFactoryProvider.kt Maven / Gradle / Ivy

/*
 * Copyright 2019 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.execution

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
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.
 */
interface KotlinDataFetcherFactoryProvider {

    /**
     * Retrieve an 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
     */
    fun functionDataFetcherFactory(target: Any?, kFunction: KFunction<*>): DataFetcherFactory

    /**
     * Retrieve an 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
     */
    fun propertyDataFetcherFactory(kClass: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory
}

/**
 * SimpleKotlinDataFetcherFactoryProvider is the default data fetcher factory provider that is used during schema construction
 * to obtain [DataFetcherFactory] that should be used for target function and property resolution.
 */
open class SimpleKotlinDataFetcherFactoryProvider(
    private val objectMapper: ObjectMapper = jacksonObjectMapper()
) : KotlinDataFetcherFactoryProvider {

    override fun functionDataFetcherFactory(target: Any?, kFunction: KFunction<*>) = DataFetcherFactory {
        FunctionDataFetcher(
            target = target,
            fn = kFunction,
            objectMapper = objectMapper
        )
    }

    override fun propertyDataFetcherFactory(kClass: KClass<*>, kProperty: KProperty<*>) = DataFetcherFactory {
        PropertyDataFetcher(kProperty.name)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy