Skip to content
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

Support interface on interface #2887

Merged
merged 3 commits into from Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
}
}