How do I rename/hook fields out of my control? #882
-
I have a library written in Java that calls an API and deserialize the JSON result back using Jackson. Unfortunately this means all the access from Kotlin will have "get" prefixed, I don't like that and I want to get rid of it. Also it seems like graphql-kotlin does not recognize Java types as nullable so they are always thought to be non-nullable. This is also pretty infuriating. This is the type generated type Ohlc {
getClose: Point!
getHigh: BigDecimal!
getLow: BigDecimal!
getOpen: Point!
getSymbol: String!
getVolume: BigDecimal!
} This is where it is from: public String getSymbol() {
return symbol;
}
public Point getOpen() {
return open;
}
public Point getClose() {
return close;
}
public BigDecimal getHigh() {
return high;
}
public BigDecimal getLow() {
return low;
}
public BigDecimal getVolume() {
return volume;
} (So I think they should all be nullable?) Is there anyone who has a similar problem? How did you solve it? Also the library I'm using is https://github.com/WojciechZankowski/iextrading4j |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
After a brief introduction to the schema hook system I came up with this idea: override fun onRewireGraphQLType(
generatedType: GraphQLSchemaElement,
coordinates: FieldCoordinates?,
codeRegistry: GraphQLCodeRegistry.Builder?
): GraphQLSchemaElement {
var generatedType2 = generatedType
var coordinates2 = coordinates
var codeRegistry2 = codeRegistry
if (generatedType2 is GraphQLFieldDefinition && coordinates?.fieldName?.startsWith("get") == true) {
val newName = generatedType2.name.replace("get", "").let(String::decapitalize)
val newType = GraphQLTypeUtil.unwrapType(generatedType2.type)[1] as GraphQLOutputType
generatedType2 = GraphQLFieldDefinition.Builder(generatedType2)
.name(newName)
.type(newType)
.build()
coordinates2 = FieldCoordinates.coordinates(coordinates.typeName, newName)
codeRegistry2 =
codeRegistry2?.dataFetcher(coordinates2, codeRegistry2.getDataFetcher(coordinates, generatedType2))
}
return super.onRewireGraphQLType(generatedType2, coordinates2, codeRegistry2)
} I'm wishing to have a more elegant code, so as to maybe remove any redundant code, or just another way but shorter. Do keep in mind the caveat that this also doesn't account for package name yet so it will be applied on a global scope. |
Beta Was this translation helpful? Give feedback.
-
// i'd assume some of those fields should be non-null
data class Ohlc(val close: Point?, val high: BigDecimal?, val low: BigDecimal?, val open: Point?, val symbol: String?, val volume: BigDecimal?) |
Beta Was this translation helpful? Give feedback.
graphql-kotlin
was written to work natively with Kotlin due to (among other things) the strong type safety guaranteed by the language. Any reference in Java may be null so it basically breaks any guarantees from Kotlin (see: official documentation). I'd advise against using Java POJOs as you will be missing null safety information. Instead why not recreate those objects as Kotlin data classes where you can specify proper nullability (and names)?