Skip to content

Add GraphQLIgnore annotation #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ApolloProcessor(
done = true

val query = getRootSymbol(resolver, KotlinSymbols.GraphQLQuery.canonicalName)

if (query == null) {
logger.error("No '@GraphQLQuery' class found")
return emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal object KotlinSymbols {
val GraphQLQuery = ClassName(annotationPackageName, "GraphQLQuery")
val GraphQLMutation = ClassName(annotationPackageName, "GraphQLMutation")
val GraphQLSubscription = ClassName(annotationPackageName, "GraphQLSubscription")
val GraphQLIgnore = ClassName(annotationPackageName, "GraphQLIgnore")

val AstDocument = ClassName(apolloAstPackageName, "GQLDocument")
val AstScalarTypeDefinition = ClassName(apolloAstPackageName, "GQLScalarTypeDefinition")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.execution.processor

import com.apollographql.apollo.ast.*
import com.apollographql.execution.processor.codegen.KotlinSymbols.GraphQLIgnore
import com.apollographql.execution.processor.sir.*
import com.apollographql.execution.processor.sir.Instantiation
import com.apollographql.execution.processor.sir.SirArgumentDefinition
Expand All @@ -19,10 +20,7 @@ import com.apollographql.execution.processor.sir.SirObjectDefinition
import com.apollographql.execution.processor.sir.SirType
import com.apollographql.execution.processor.sir.SirTypeDefinition
import com.apollographql.execution.processor.sir.SirUnionDefinition
import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.isConstructor
import com.google.devtools.ksp.isPublic
import com.google.devtools.ksp.*
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.*

Expand Down Expand Up @@ -172,11 +170,6 @@ private class TypeDefinitionContext(
}
usedTypeNames.add(name)

if (declaration.typeParameters.isNotEmpty()) {
logger.error("Generic classes are not supported")
return null
}

if (declaration is KSTypeAlias) {
return declaration.toSirScalarDefinition(qualifiedName)
}
Expand Down Expand Up @@ -378,10 +371,13 @@ private class TypeDefinitionContext(
logger.error("Cannot map to a GraphQL output type", this)
return null
}

val usedNames = mutableSetOf<String>()
val allFields = declarations.filter { it.isPublic() }.mapNotNull {
val name = it.graphqlName()
val hasIsIgnoreAnnotation = this.annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString() == "com.apollographql.execution.annotation.GraphQLIgnore"
}
if (hasIsIgnoreAnnotation) return@mapNotNull null
if (usedNames.contains(name)) {
logger.error("Duplicate field '$name'. Either rename the declaration or use @GraphQLName.", it)
return@mapNotNull null
Expand Down Expand Up @@ -434,11 +430,6 @@ private class TypeDefinitionContext(
}

ClassKind.INTERFACE -> {
if (!modifiers.contains(Modifier.SEALED)) {
logger.error("Interfaces and unions must be sealed interfaces", this)
return null
}

getSealedSubclasses().forEach {
/**
* We go depth first on the superclasses but need to escape the callstack and
Expand Down Expand Up @@ -503,10 +494,11 @@ private class TypeDefinitionContext(
private fun KSClassDeclaration.interfaces(objectName: String?): List<String> {
return getAllSuperTypes().mapNotNull {
val declaration = it.declaration
if (it.arguments.isNotEmpty()) {
logger.error("Generic interfaces are not supported", this)
null
} else if (declaration is KSClassDeclaration) {
val hasIsIgnoreAnnotation = this.annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString() == "com.apollographql.execution.annotation.GraphQLIgnore"
}
if (hasIsIgnoreAnnotation) return@mapNotNull null
if (declaration is KSClassDeclaration) {
if (declaration.asClassName().asString() == "kotlin.Any") {
// kotlin.Any is a super type of everything, just ignore it
null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.apollographql.execution.annotation

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
annotation class GraphQLIgnore
11 changes: 9 additions & 2 deletions tests/federation/src/main/kotlin/graphql.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import com.apollographql.execution.annotation.GraphQLQuery
import com.apollographql.execution.annotation.GraphQLIgnore
import com.apollographql.execution.subgraph.GraphQLKey

@GraphQLIgnore
interface SomeGenericInterface<T>

@GraphQLQuery
class Query {
class Query : SomeGenericInterface<String>{
fun products(): List<Product> {
return products
}

@GraphQLIgnore
fun internalFields(): Int = 1
}

class Product(
Expand All @@ -23,4 +30,4 @@ class Product(
val products = listOf(
Product("1", "foo"),
Product("2", "bar")
)
)
Loading