Skip to content

Commit

Permalink
Support interface on interface (#2887)
Browse files Browse the repository at this point in the history
* add interfaces on interfaces to the antlr grammar

* implement possibleTypes

* added a test case
  • Loading branch information
martinbonnin committed Jan 20, 2021
1 parent c72d089 commit 898d939
Show file tree
Hide file tree
Showing 10 changed files with 943 additions and 28 deletions.
Expand Up @@ -62,7 +62,7 @@ implementsInterface
;

interfaceTypeDefinition
: description? INTERFACE name directives? fieldsDefinition
: description? INTERFACE name implementsInterfaces? directives? fieldsDefinition
;

fieldsDefinition
Expand Down

This file was deleted.

Expand Up @@ -5,7 +5,6 @@ import com.apollographql.apollo.compiler.parser.antlr.GraphSDLLexer
import com.apollographql.apollo.compiler.parser.antlr.GraphSDLParser
import com.apollographql.apollo.compiler.parser.error.DocumentParseException
import com.apollographql.apollo.compiler.parser.error.ParseException
import com.apollographql.apollo.compiler.parser.sdl.GraphSDLSchemaParser.parse
import org.antlr.v4.runtime.ANTLRInputStream
import org.antlr.v4.runtime.BaseErrorListener
import org.antlr.v4.runtime.CommonTokenStream
Expand Down Expand Up @@ -100,7 +99,8 @@ object GraphSDLSchemaParser {
schema = GraphSdlSchema.Schema(
description = schemaDefinition?.description()?.parse(),
directives = schemaDefinition?.directives().parse(),
queryRootOperationType = rootOperationType(operationRootTypes, "query", typeDefinitions) ?: throw IllegalStateException("No query root operation type found"),
queryRootOperationType = rootOperationType(operationRootTypes, "query", typeDefinitions)
?: throw IllegalStateException("No query root operation type found"),
mutationRootOperationType = rootOperationType(operationRootTypes, "mutation", typeDefinitions),
subscriptionRootOperationType = rootOperationType(operationRootTypes, "subscription", typeDefinitions)),
typeDefinitions = typeDefinitions ?: emptyMap()
Expand All @@ -126,6 +126,7 @@ object GraphSDLSchemaParser {
null
}
}

private fun List<GraphSDLParser.TypeSystemExtensionContext>.parse(
typeDefinitions: Map<String, GraphSdlSchema.TypeDefinition>
): Map<String, GraphSdlSchema.TypeDefinition> {
Expand Down Expand Up @@ -386,7 +387,8 @@ object GraphSDLSchemaParser {
name = name().text,
description = description().parse(),
directives = directives().parse(),
fields = fieldsDefinition().parse()
fields = fieldsDefinition().parse(),
interfaces = implementsInterfaces().parse()
)
}

Expand Down
Expand Up @@ -5,6 +5,7 @@ import com.apollographql.apollo.compiler.parser.error.ParseException
import com.apollographql.apollo.compiler.parser.introspection.IntrospectionSchema
import com.apollographql.apollo.compiler.parser.sdl.GraphSDLSchemaParser.parse
import java.io.File
import java.lang.IllegalStateException

data class GraphSdlSchema(
val schema: Schema,
Expand Down Expand Up @@ -51,7 +52,8 @@ data class GraphSdlSchema(
override val name: String,
override val description: String,
override val directives: List<Directive>,
val fields: List<Field>
val fields: List<Field>,
val interfaces: List<TypeRef.Named>
) : TypeDefinition()

data class Field(
Expand Down Expand Up @@ -298,17 +300,23 @@ private fun List<GraphSdlSchema.Directive>.findDeprecatedDirective(): DeprecateD
}
}

private fun GraphSdlSchema.TypeDefinition.Interface.possibleTypes(schema: GraphSdlSchema): List<IntrospectionSchema.TypeRef> {
return schema.typeDefinitions.values
.filter { typeDefinition ->
typeDefinition is GraphSdlSchema.TypeDefinition.Object && typeDefinition.interfaces.firstOrNull { interfaceTypeRef ->
interfaceTypeRef.typeName == name
} != null
}
.map { typeDefinition ->
internal fun GraphSdlSchema.TypeDefinition.possibleTypes(schema: GraphSdlSchema): List<IntrospectionSchema.TypeRef> {
return when (this) {
is GraphSdlSchema.TypeDefinition.Union -> this.typeRefs.map { it.toIntrospectionType(schema) }
is GraphSdlSchema.TypeDefinition.Interface -> schema.typeDefinitions.values.filter { typeDefinition ->
typeDefinition is GraphSdlSchema.TypeDefinition.Object && typeDefinition.interfaces.map { it.typeName }.contains(name)
|| typeDefinition is GraphSdlSchema.TypeDefinition.Interface && typeDefinition.interfaces.map { it.typeName }.contains(name)
}.flatMap {
it.possibleTypes(schema)
}
is GraphSdlSchema.TypeDefinition.Object -> listOf(
IntrospectionSchema.TypeRef(
kind = IntrospectionSchema.Kind.OBJECT,
name = typeDefinition.name
name = name
)
}
)
else -> {
throw IllegalStateException("Cannot determine possibleTypes of $name")
}
}
}

0 comments on commit 898d939

Please sign in to comment.