Skip to content

Commit a2c86bd

Browse files
committed
Add GraphQLIgnore annotation
Add GraphQLIgnore to be able to ignore fields, classes and interfaces
1 parent c697c5e commit a2c86bd

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/ApolloProcessor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ApolloProcessor(
5353
done = true
5454

5555
val query = getRootSymbol(resolver, KotlinSymbols.GraphQLQuery.canonicalName)
56+
5657
if (query == null) {
5758
logger.error("No '@GraphQLQuery' class found")
5859
return emptyList()

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/codegen/KotlinSymbols.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal object KotlinSymbols {
2121
val GraphQLQuery = ClassName(annotationPackageName, "GraphQLQuery")
2222
val GraphQLMutation = ClassName(annotationPackageName, "GraphQLMutation")
2323
val GraphQLSubscription = ClassName(annotationPackageName, "GraphQLSubscription")
24+
val GraphQLIgnore = ClassName(annotationPackageName, "GraphQLIgnore")
2425

2526
val AstDocument = ClassName(apolloAstPackageName, "GQLDocument")
2627
val AstScalarTypeDefinition = ClassName(apolloAstPackageName, "GQLScalarTypeDefinition")

apollo-execution-processor/src/main/kotlin/com/apollographql/execution/processor/definitions.kt

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.apollographql.execution.processor
22

33
import com.apollographql.apollo.ast.*
4+
import com.apollographql.execution.processor.codegen.KotlinSymbols.GraphQLIgnore
45
import com.apollographql.execution.processor.sir.*
56
import com.apollographql.execution.processor.sir.Instantiation
67
import com.apollographql.execution.processor.sir.SirArgumentDefinition
@@ -19,10 +20,7 @@ import com.apollographql.execution.processor.sir.SirObjectDefinition
1920
import com.apollographql.execution.processor.sir.SirType
2021
import com.apollographql.execution.processor.sir.SirTypeDefinition
2122
import com.apollographql.execution.processor.sir.SirUnionDefinition
22-
import com.google.devtools.ksp.getAllSuperTypes
23-
import com.google.devtools.ksp.getDeclaredProperties
24-
import com.google.devtools.ksp.isConstructor
25-
import com.google.devtools.ksp.isPublic
23+
import com.google.devtools.ksp.*
2624
import com.google.devtools.ksp.processing.KSPLogger
2725
import com.google.devtools.ksp.symbol.*
2826

@@ -172,11 +170,6 @@ private class TypeDefinitionContext(
172170
}
173171
usedTypeNames.add(name)
174172

175-
if (declaration.typeParameters.isNotEmpty()) {
176-
logger.error("Generic classes are not supported")
177-
return null
178-
}
179-
180173
if (declaration is KSTypeAlias) {
181174
return declaration.toSirScalarDefinition(qualifiedName)
182175
}
@@ -378,10 +371,13 @@ private class TypeDefinitionContext(
378371
logger.error("Cannot map to a GraphQL output type", this)
379372
return null
380373
}
381-
382374
val usedNames = mutableSetOf<String>()
383375
val allFields = declarations.filter { it.isPublic() }.mapNotNull {
384376
val name = it.graphqlName()
377+
val hasIsIgnoreAnnotation = this.annotations.any {
378+
it.annotationType.resolve().declaration.qualifiedName?.asString() == "com.apollographql.execution.annotation.GraphQLIgnore"
379+
}
380+
if (hasIsIgnoreAnnotation) return@mapNotNull null
385381
if (usedNames.contains(name)) {
386382
logger.error("Duplicate field '$name'. Either rename the declaration or use @GraphQLName.", it)
387383
return@mapNotNull null
@@ -434,11 +430,6 @@ private class TypeDefinitionContext(
434430
}
435431

436432
ClassKind.INTERFACE -> {
437-
if (!modifiers.contains(Modifier.SEALED)) {
438-
logger.error("Interfaces and unions must be sealed interfaces", this)
439-
return null
440-
}
441-
442433
getSealedSubclasses().forEach {
443434
/**
444435
* We go depth first on the superclasses but need to escape the callstack and
@@ -503,10 +494,11 @@ private class TypeDefinitionContext(
503494
private fun KSClassDeclaration.interfaces(objectName: String?): List<String> {
504495
return getAllSuperTypes().mapNotNull {
505496
val declaration = it.declaration
506-
if (it.arguments.isNotEmpty()) {
507-
logger.error("Generic interfaces are not supported", this)
508-
null
509-
} else if (declaration is KSClassDeclaration) {
497+
val hasIsIgnoreAnnotation = this.annotations.any {
498+
it.annotationType.resolve().declaration.qualifiedName?.asString() == "com.apollographql.execution.annotation.GraphQLIgnore"
499+
}
500+
if (hasIsIgnoreAnnotation) return@mapNotNull null
501+
if (declaration is KSClassDeclaration) {
510502
if (declaration.asClassName().asString() == "kotlin.Any") {
511503
// kotlin.Any is a super type of everything, just ignore it
512504
null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.apollographql.execution.annotation
2+
3+
@Retention(AnnotationRetention.SOURCE)
4+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
5+
annotation class GraphQLIgnore

tests/federation/src/main/kotlin/graphql.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import com.apollographql.execution.annotation.GraphQLQuery
2+
import com.apollographql.execution.annotation.GraphQLIgnore
23
import com.apollographql.execution.subgraph.GraphQLKey
34

5+
@GraphQLIgnore
6+
interface SomeGenericInterface<T>
7+
48
@GraphQLQuery
5-
class Query {
9+
class Query : SomeGenericInterface<String>{
610
fun products(): List<Product> {
711
return products
812
}
13+
14+
@GraphQLIgnore
15+
fun internalFields(): Int = 1
916
}
1017

1118
class Product(
@@ -23,4 +30,4 @@ class Product(
2330
val products = listOf(
2431
Product("1", "foo"),
2532
Product("2", "bar")
26-
)
33+
)

0 commit comments

Comments
 (0)