Hi, We are having a problem implementing a separa...
# graphql-kotlin
h
Hi, We are having a problem implementing a separate data fetcher for a property. This documentation says to use a function instead of a property for that. How would you do this using spring beans in a spring-MVC environment? As this is a POJO still you cannot really inject any repote clients here. Any ideas how we can achieve what we wanna do?
☑️ 1
s
Hello @huehnerlady! First, if you are using
graphql-kotlin-spring-server
, you unfortunately can not use Spring MVC as we run on top of Spring WebFlux and the two implementations are not compatible: https://expediagroup.github.io/graphql-kotlin/docs/spring-server/spring-overview However if you are using just
graphql-kotlin-schema-generator
and running your own server on Spring MVC, or if you migrate to
graphql-kotlin-spring-server
, you can still have
private
properties as member of your classes and they will not get exposed as schema fields. Then you can use these clients in your functions You can see some examples in the docs: https://expediagroup.github.io/graphql-kotlin/docs/spring-server/spring-schema
h
@Shane Myrick as I said I cannot use spring-server as we run on Spring MVC. so how can I achieve the described behaviour using just
graphql-kotlin-schema-generator
and not spring-server? Unfortunately the docs you linked are all about spring-server
d
if you want to have custom resolver logic for given field then i’d definitely recommend to expose those fields through functions - you can then have a data fetcher that could autowire your spring dependencies directly into the methods (see: https://github.com/ExpediaGroup/graphql-kotlin/pull/813)
another alternative you can look into is to use
lateinit
properties (but it implies they have to be non-nullable) and then use custom data fetcher factory provider that can provide custom resolver for the lateinit field as well
h
As I do not use the Spring dependency of graphql-kotlin I do not have the spring-data-fefetcher to hand, but I found a solution now to indeed use a custom data fetcher factory. For that I found out that you do not have to change anything in your data class, just override the property datafetcher factory:
Copy code
override fun propertyDataFetcherFactory(kClass: KClass<*>, kProperty: KProperty<*>): DataFetcherFactory<Any?> =

      DataFetcherFactory<Any?> {
        when (kProperty) {
          Class::property -> fetching { it: Class ->
            doMyCustomLogik(it.id)
          }
          else -> PropertyDataFetcher(kProperty.name) //this is done in the SimpleDataFetcher
        }
      }
}
This feels a bit hacky, but is the best solution I could find so far 🙂
d
Thats actually the purpose of the data fetcher factory so you are using it correctly
h
ah super cool, thanks 🙂