Anders Kirkeby
12/02/2020, 2:02 PMdata class TestClass(
val content: Set<String>
)
to remedy this, I’ve added a Hook with the following SchemaGeneratorHooks#willGenerateGrpahQLType method:
@ExperimentalStdlibApi
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when {
type.isSubtypeOf(typeOf<Set<*>?>()) -> {
type.arguments.first().type?.let { kType ->
val typeRef = (kType.classifier as KClass<*>)
GraphQLList.list(GraphQLTypeReference.typeRef(typeRef.simpleName))
}
}
else -> null
}
this works when creating a query that returns TestClass or List<TestClass>. However, when creating a Query that returns a Set<TestClass> it fails as the TestClass reference has not yet been created. From what I could gather, this method should just create the reference to TestClass::class.simpleName
and the generator should generate the class in “due-time” which should lead to Set<TestClass> resolving as a GQLList<TestClass> in the playground, or am I missing something?SchemaGenerator#generateAdditionalTypes
method is no longer open as indicated by the comments, and PRDariusz Kuc
12/02/2020, 6:56 PMTestClass
?Anders Kirkeby
12/02/2020, 7:39 PM/**
* A special type to allow a object/interface types to reference itself. It's replaced with the real type
* object when the schema is built.
*/
I initially assumed that the GraphQLTypeReference.typeRef(typeRef.simpleName) would just “defer” it’s creation time up the last possible moment (ie when the subclass would be part of the schema)override fun generateAdditionalTypes(types: Set<KType>): Set<GraphQLType> {
val newTypes = types.toMutableSet().add(MyNewType()::class.createType())
return super.generateAdditionalTypes(newTypes)
}
should work but the SchemaGenerator#generateAdditionalTypes
takes no args
/**
* Generate the GraphQL type for all the `additionalTypes`.
*
* If you need to provide more custom additional types that were not picked up from reflection of the schema objects,
* you can provide more types to be added through [generateSchema].
*
* This function loops because while generating the additionalTypes it is possible to create more additional types that need to be processed.
*/
protected fun generateAdditionalTypes(): Set<GraphQLType> {
val graphqlTypes = mutableSetOf<GraphQLType>()
while (this.additionalTypes.isNotEmpty()) {
val currentlyProcessedTypes = LinkedHashSet(this.additionalTypes)
this.additionalTypes.clear()
graphqlTypes.addAll(
currentlyProcessedTypes.map {
GraphQLTypeUtil.unwrapNonNull(generateGraphQLType(this, it.kType, it.inputType))
}
)
}
return graphqlTypes
}
I’m sure there’s something simple that I’m missing, but I cannot for the life of me find a way to make the resolver just override Collections with List which seems like the simplest thing 😛Dariusz Kuc
12/02/2020, 9:15 PMadditionalTypes
/`additionalInputTypes` as arguments to schema generation processAnders Kirkeby
12/03/2020, 7:29 AMDariusz Kuc
12/03/2020, 12:49 PMAnders Kirkeby
12/03/2020, 1:42 PMDariusz Kuc
12/03/2020, 1:43 PMAnders Kirkeby
12/03/2020, 1:45 PMdata class TestClass(val content: Set<OtherTestClass>)
which would require
data class TestClassQL(val content: List<OtherTestClass>)
Dariusz Kuc
12/03/2020, 1:48 PMAnders Kirkeby
12/03/2020, 1:50 PMDariusz Kuc
12/03/2020, 1:51 PMAnders Kirkeby
12/03/2020, 1:54 PMlistOf().unique()
we would use setOf() instead, and just expose it as a list of unique valuesDariusz Kuc
12/03/2020, 1:55 PMAnders Kirkeby
12/03/2020, 1:57 PM