Skip to content

Commit

Permalink
Allow mapping scalars to simple generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Sep 20, 2024
1 parent fddfd7e commit f50e605
Show file tree
Hide file tree
Showing 19 changed files with 536 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal class JavaResolver(
val result = resolve(ResolverKey(kind, id))

check(result != null) {
"Cannot resolve $kind($id). " +
"Cannot resolve $kind($id). " +
"Have you set up an 'opposite link' on the downstream project to the schema module as a isADependencyOf(..)?"
}
return result
Expand Down Expand Up @@ -239,9 +239,25 @@ internal class JavaResolver(
}
}

private fun resolveScalarTarget(name: String): ClassName? {
private fun resolveScalarTarget(name: String): TypeName? {
return scalarMapping[name]?.targetName?.let {
ClassName.bestGuess(it)
bestGuess(it)
}
}

/**
* Best guess a type name. Handles simple generics like `Map<String, Integer>`, but no variance or wildcards.
*/
private fun bestGuess(name: String): TypeName? {
val className = ClassName.bestGuess(name.substringBefore('<'))
val typeArgs = name.substringAfter('<', "").substringBefore('>', "")
.split(',')
.filterNot { it.isEmpty() }
.map { it.trim() }
return if (typeArgs.isEmpty()) {
className
} else {
ParameterizedTypeName.get(className, *typeArgs.map { bestGuess(it) }.toTypedArray())
}
}

Expand Down Expand Up @@ -427,7 +443,8 @@ internal class JavaResolver(
}


internal fun ResolverClassName.toJavaPoetClassName(): ClassName = ClassName.get(packageName, simpleNames[0], *simpleNames.drop(1).toTypedArray())
internal fun ResolverClassName.toJavaPoetClassName(): ClassName =
ClassName.get(packageName, simpleNames[0], *simpleNames.drop(1).toTypedArray())


private val primitiveTypeNames = setOf(TypeName.DOUBLE, TypeName.INT, TypeName.BOOLEAN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal class KotlinResolver(
private val scalarMapping: Map<String, ScalarInfo>,
private val requiresOptInAnnotation: String?,
) {
fun resolve(key: ResolverKey): ClassName? {
fun resolve(key: ResolverKey): ClassName? {
return classNames[key] ?: next?.resolve(key)
}

Expand All @@ -60,7 +60,7 @@ internal class KotlinResolver(

check(result != null) {
"Cannot resolve $kind($id). " +
"Have you set up an 'opposite link' on the downstream project to the schema module as a isADependencyOf(..)?"
"Have you set up an 'opposite link' on the downstream project to the schema module as a isADependencyOf(..)?"
}
return result
}
Expand All @@ -85,6 +85,7 @@ internal class KotlinResolver(
type.optional -> {
KotlinSymbols.Optional.parameterizedBy(resolveIrType(type.optional(false), jsExport, isInterface))
}

type.catchTo != IrCatchTo.NoCatch -> {
resolveIrType(type.catchTo(IrCatchTo.NoCatch), jsExport, isInterface).let {
when (type.catchTo) {
Expand All @@ -94,9 +95,11 @@ internal class KotlinResolver(
}
}
}

type.nullable -> {
resolveIrType(type.nullable(false), jsExport, isInterface).copy(nullable = true)
}

else -> {
when (type) {
is IrListType -> resolveIrType(type.ofType, jsExport, isInterface).wrapInList(jsExport, isInterface)
Expand Down Expand Up @@ -139,12 +142,30 @@ internal class KotlinResolver(
return listType.parameterizedBy(param)
}

private fun resolveScalarTarget(name: String): ClassName? {
private fun resolveScalarTarget(name: String): TypeName? {
return scalarMapping[name]?.targetName?.let {
ClassName.bestGuess(it)
bestGuess(it)
}
}

/**
* Best guess a type name. Handles simple generics like `Map<String, Int?>`, but no variance or wildcards.
*/
private fun bestGuess(name: String): TypeName {
val isNullable = name.endsWith('?')
val className = ClassName.bestGuess(name.substringBeforeLast('?').substringBefore('<'))
val typeArgs = name.substringAfter('<', "").substringBefore('>', "")
.split(',')
.filterNot { it.isEmpty() }
.map { it.trim() }
return if (typeArgs.isEmpty()) {
className
} else {
className.parameterizedBy(typeArgs.map { bestGuess(it) })
}
.copy(nullable = isNullable)
}

internal fun resolveIrType2(type: IrType2): TypeName {
return when (type) {
is IrNonNullType2 -> resolveIrType2(type.ofType).copy(nullable = false)
Expand Down Expand Up @@ -203,6 +224,7 @@ internal class KotlinResolver(
val presentFun = MemberName("com.apollographql.apollo.api", "present")
CodeBlock.of("%L.%M()", adapterInitializer(type.optional(false), requiresBuffering, jsExport), presentFun)
}

type.catchTo != IrCatchTo.NoCatch -> {
adapterInitializer(type.catchTo(IrCatchTo.NoCatch), requiresBuffering, jsExport).let {
val member = when (type.catchTo) {
Expand All @@ -213,11 +235,13 @@ internal class KotlinResolver(
CodeBlock.of("%L.%M()", it, member)
}
}

type.maybeError -> {
adapterInitializer(type.maybeError(false), requiresBuffering, jsExport).let {
CodeBlock.of("%L.%M()", it, KotlinSymbols.errorAware)
}
}

type.nullable -> {
// Don't hardcode the adapter when the scalar is mapped to a user-defined type
val scalarWithoutCustomMapping = type is IrScalarType && !scalarMapping.containsKey(type.name)
Expand All @@ -237,6 +261,7 @@ internal class KotlinResolver(
}
}
}

else -> {
when (type) {
is IrListType -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ query TestQuery {
links
}
}

query ScalarWithGenericType {
listOfString
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f50e605

Please sign in to comment.