|
| 1 | +package graphql.kickstart.tools |
| 2 | + |
| 3 | +import graphql.Directives |
| 4 | +import graphql.introspection.Introspection.DirectiveLocation |
| 5 | +import graphql.kickstart.tools.util.describe |
| 6 | +import graphql.kickstart.tools.util.getDocumentation |
| 7 | +import graphql.kickstart.tools.util.unwrap |
| 8 | +import graphql.language.* |
| 9 | +import graphql.schema.* |
| 10 | +import graphql.schema.idl.ScalarInfo |
| 11 | + |
| 12 | +internal class MergedDefinitions(definitions: List<Definition<*>>) { |
| 13 | + val directiveDefinitions: List<DirectiveDefinition> = definitions.filterIsInstance<DirectiveDefinition>() |
| 14 | + val rootInfo: RootTypeInfo = RootTypeInfo.fromSchemaDefinitions(definitions.filterIsInstance<SchemaDefinition>()) |
| 15 | + |
| 16 | + val scalarNames: Set<String> = ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.keys + definitions.filterIsInstance<ScalarTypeDefinition>().map { it.name } |
| 17 | + |
| 18 | + private val typeDefinitions = definitions.filterIsInstance<TypeDefinition<*>>() |
| 19 | + private val baseTypeNames = typeDefinitions.filter { it !is SDLExtensionDefinition }.map { it.name }.toSet() |
| 20 | + |
| 21 | + private val loneExtensions = typeDefinitions |
| 22 | + .filter { it is SDLExtensionDefinition && it !is ObjectTypeDefinition && it !is InputObjectTypeDefinition && it.name !in baseTypeNames } |
| 23 | + .distinctBy { it.name } |
| 24 | + .toSet() |
| 25 | + |
| 26 | + private val sources = typeDefinitions.filter { it !is SDLExtensionDefinition || it in loneExtensions } |
| 27 | + |
| 28 | + private val sourcesByName = sources.associateBy { it.name } |
| 29 | + |
| 30 | + private val extensionsByName = typeDefinitions |
| 31 | + .filter { it is SDLExtensionDefinition && it !in loneExtensions && !extendsOtherKind(it) } |
| 32 | + .groupBy { it.name } |
| 33 | + |
| 34 | + private val typesBySource: List<Pair<TypeDefinition<*>, MergedType<*>>> = sources.mapNotNull { source -> merge(source)?.let { source to it } } |
| 35 | + |
| 36 | + private val specificationScalars: List<Pair<ScalarTypeDefinition, MergedType.Scalar>> = ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.values.map { definition -> |
| 37 | + definition to MergedType.Scalar(definition, extensionsByName[definition.name].orEmpty().filterIsInstance<ScalarTypeExtensionDefinition>()) |
| 38 | + } |
| 39 | + |
| 40 | + private val specificationScalarsByName: Map<String, MergedType.Scalar> = specificationScalars.associate { (_, type) -> type.name to type } |
| 41 | + |
| 42 | + private val sourceOf: Map<MergedType<*>, TypeDefinition<*>> = (typesBySource + specificationScalars).associate { (source, type) -> type to source } |
| 43 | + |
| 44 | + val byName: Map<String, MergedType<*>> = typesBySource.associate { (_, type) -> type.name to type } |
| 45 | + |
| 46 | + val objectsWithRepeats: List<MergedType.Object> = typesBySource.map { it.second }.filterIsInstance<MergedType.Object>() |
| 47 | + |
| 48 | + val ignoredExtensionWarnings: List<String> = typeDefinitions.filter(::extendsOtherKind).map { extension -> |
| 49 | + "Type '${extension.name}' is defined as ${sourcesByName.getValue(extension.name).kind()}, but extended as ${extension.kind()}. The extension is ignored." |
| 50 | + } |
| 51 | + |
| 52 | + fun resolve(name: String?): MergedType<*>? = specificationScalarsByName[name] ?: byName[name] |
| 53 | + |
| 54 | + fun definitionsOf(types: Set<MergedType<*>>): Set<TypeDefinition<*>> = types.mapNotNullTo(linkedSetOf()) { sourceOf[it] } |
| 55 | + |
| 56 | + fun findUsage(reference: TypeName, types: Collection<MergedType<*>>): String? = |
| 57 | + (types.mapNotNull { sourceOf[it] } + directiveDefinitions + extensionsByName.values.flatten()).asSequence().flatMap { definition -> |
| 58 | + when (definition) { |
| 59 | + is ObjectTypeDefinition -> fieldUsages(definition.name, definition.fieldDefinitions) |
| 60 | + is InterfaceTypeDefinition -> fieldUsages(definition.name, definition.fieldDefinitions) |
| 61 | + is InputObjectTypeDefinition -> definition.inputValueDefinitions.map { "input field '${definition.name}.${it.name}'" to it.type } |
| 62 | + is DirectiveDefinition -> definition.inputValueDefinitions.map { "argument '${it.name}' of directive '@${definition.name}'" to it.type } |
| 63 | + else -> emptyList() |
| 64 | + } |
| 65 | + }.find { (_, type) -> type.unwrap() === reference }?.first |
| 66 | + |
| 67 | + private fun fieldUsages(typeName: String, fields: List<FieldDefinition>): List<Pair<String, Type<*>>> = fields.flatMap { field -> |
| 68 | + listOf("field '$typeName.${field.name}'" to field.type) + |
| 69 | + field.inputValueDefinitions.map { "argument '${it.name}' of field '$typeName.${field.name}'" to it.type } |
| 70 | + } |
| 71 | + |
| 72 | + private fun merge(source: TypeDefinition<*>): MergedType<*>? { |
| 73 | + val extensions = extensionsByName[source.name].orEmpty() |
| 74 | + return when (source) { |
| 75 | + is ObjectTypeDefinition -> MergedType.Object(source, extensions.filterIsInstance<ObjectTypeExtensionDefinition>()) |
| 76 | + is InterfaceTypeDefinition -> MergedType.Interface(source, extensions.filterIsInstance<InterfaceTypeExtensionDefinition>()) |
| 77 | + is UnionTypeDefinition -> MergedType.Union(source, extensions.filterIsInstance<UnionTypeExtensionDefinition>()) |
| 78 | + is EnumTypeDefinition -> MergedType.Enum(source, extensions.filterIsInstance<EnumTypeExtensionDefinition>()) |
| 79 | + is InputObjectTypeDefinition -> MergedType.Input(source, extensions.filterIsInstance<InputObjectTypeExtensionDefinition>()) |
| 80 | + is ScalarTypeDefinition -> MergedType.Scalar(source, extensions.filterIsInstance<ScalarTypeExtensionDefinition>()) |
| 81 | + else -> null |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun extendsOtherKind(definition: TypeDefinition<*>): Boolean { |
| 86 | + val type = sourcesByName[definition.name] |
| 87 | + return definition is SDLExtensionDefinition && type != null && type.kind() != definition.kind() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +private fun TypeDefinition<*>.kind(): String = when (this) { |
| 92 | + is ObjectTypeDefinition -> "an object type" |
| 93 | + is InterfaceTypeDefinition -> "an interface" |
| 94 | + is UnionTypeDefinition -> "a union" |
| 95 | + is EnumTypeDefinition -> "an enum" |
| 96 | + is InputObjectTypeDefinition -> "an input object type" |
| 97 | + is ScalarTypeDefinition -> "a scalar" |
| 98 | + else -> javaClass.simpleName |
| 99 | +} |
| 100 | + |
| 101 | +internal class TypeBuildContext( |
| 102 | + val options: SchemaParserOptions, |
| 103 | + val directives: SdlDirectives, |
| 104 | + val customScalars: CustomScalarMap, |
| 105 | +) |
| 106 | + |
| 107 | +internal sealed class MergedType<B : GraphqlDirectivesContainerTypeBuilder<B, B>>( |
| 108 | + private val definition: TypeDefinition<*>, |
| 109 | + extensions: List<TypeDefinition<*>>, |
| 110 | +) { |
| 111 | + val name: String get() = definition.name |
| 112 | + |
| 113 | + val definitionClassName: String get() = definition.javaClass.simpleName |
| 114 | + |
| 115 | + val kindPhrase: String get() = definition.kind() |
| 116 | + |
| 117 | + protected val directives: List<Directive> = definition.directives + extensions.flatMap { it.directives } |
| 118 | + |
| 119 | + protected abstract val location: DirectiveLocation |
| 120 | + |
| 121 | + protected abstract val declarations: List<NamedNode<*>> |
| 122 | + |
| 123 | + protected abstract fun newBuilder(context: TypeBuildContext): B |
| 124 | + |
| 125 | + fun prepare(context: TypeBuildContext): B { |
| 126 | + rejectRepeated() |
| 127 | + val builder = newBuilder(context).name(name) |
| 128 | + getDocumentation(definition as AbstractNode<*>, context.options)?.let { builder.description(it) } |
| 129 | + return with(context.directives) { builder.withSdlDirectives(directives, location) } |
| 130 | + } |
| 131 | + |
| 132 | + private fun rejectRepeated() { |
| 133 | + val noun = when (this) { |
| 134 | + is Object, is Interface, is Input -> "field" |
| 135 | + is Enum -> "value" |
| 136 | + is Union -> "member" |
| 137 | + is Scalar -> return |
| 138 | + } |
| 139 | + val names = mutableSetOf<String?>() |
| 140 | + val repeat = declarations.firstOrNull { !names.add(it.name) } ?: return |
| 141 | + throw SchemaError("Type '$name' declares $noun '${repeat.name}' more than once${repeat.sourceLocation.describe()}.") |
| 142 | + } |
| 143 | + |
| 144 | + sealed interface Implementing { |
| 145 | + val fields: List<FieldDefinition> |
| 146 | + val interfaces: List<Type<*>> |
| 147 | + } |
| 148 | + |
| 149 | + class Object( |
| 150 | + private val definition: ObjectTypeDefinition, |
| 151 | + private val extensions: List<ObjectTypeExtensionDefinition>, |
| 152 | + ) : MergedType<GraphQLObjectType.Builder>(definition, extensions), Implementing { |
| 153 | + override val fields: List<FieldDefinition> = definition.fieldDefinitions + extensions.flatMap { it.fieldDefinitions } |
| 154 | + override val interfaces: List<Type<*>> = definition.implements + extensions.flatMap { it.implements } |
| 155 | + override val location = DirectiveLocation.OBJECT |
| 156 | + override val declarations get() = fields |
| 157 | + override fun newBuilder(context: TypeBuildContext): GraphQLObjectType.Builder = |
| 158 | + GraphQLObjectType.newObject().definition(definition).extensionDefinitions(extensions) |
| 159 | + } |
| 160 | + |
| 161 | + class Interface( |
| 162 | + private val definition: InterfaceTypeDefinition, |
| 163 | + private val extensions: List<InterfaceTypeExtensionDefinition>, |
| 164 | + ) : MergedType<GraphQLInterfaceType.Builder>(definition, extensions), Implementing { |
| 165 | + override val fields: List<FieldDefinition> = definition.fieldDefinitions + extensions.flatMap { it.fieldDefinitions } |
| 166 | + override val interfaces: List<Type<*>> = definition.implements + extensions.flatMap { it.implements } |
| 167 | + override val location = DirectiveLocation.INTERFACE |
| 168 | + override val declarations get() = fields |
| 169 | + override fun newBuilder(context: TypeBuildContext): GraphQLInterfaceType.Builder = |
| 170 | + GraphQLInterfaceType.newInterface().definition(definition).extensionDefinitions(extensions) |
| 171 | + } |
| 172 | + |
| 173 | + class Union( |
| 174 | + private val definition: UnionTypeDefinition, |
| 175 | + private val extensions: List<UnionTypeExtensionDefinition>, |
| 176 | + ) : MergedType<GraphQLUnionType.Builder>(definition, extensions) { |
| 177 | + val members: List<Type<*>> = definition.memberTypes + extensions.flatMap { it.memberTypes } |
| 178 | + override val location = DirectiveLocation.UNION |
| 179 | + override val declarations get() = members.map { it as TypeName } |
| 180 | + override fun newBuilder(context: TypeBuildContext): GraphQLUnionType.Builder = |
| 181 | + GraphQLUnionType.newUnionType().definition(definition).extensionDefinitions(extensions) |
| 182 | + |
| 183 | + fun declarationTextOf(member: Type<*>): String = |
| 184 | + (listOf(definition) + extensions).first { declaration -> declaration.memberTypes.any { it === member } }.toString() |
| 185 | + } |
| 186 | + |
| 187 | + class Enum( |
| 188 | + private val definition: EnumTypeDefinition, |
| 189 | + private val extensions: List<EnumTypeExtensionDefinition>, |
| 190 | + ) : MergedType<GraphQLEnumType.Builder>(definition, extensions) { |
| 191 | + val values: List<EnumValueDefinition> = definition.enumValueDefinitions + extensions.flatMap { it.enumValueDefinitions } |
| 192 | + override val location = DirectiveLocation.ENUM |
| 193 | + override val declarations get() = values |
| 194 | + override fun newBuilder(context: TypeBuildContext): GraphQLEnumType.Builder = |
| 195 | + GraphQLEnumType.newEnum().definition(definition).extensionDefinitions(extensions) |
| 196 | + } |
| 197 | + |
| 198 | + class Input( |
| 199 | + private val definition: InputObjectTypeDefinition, |
| 200 | + private val extensions: List<InputObjectTypeExtensionDefinition>, |
| 201 | + ) : MergedType<GraphQLInputObjectType.Builder>(definition, extensions) { |
| 202 | + val fields: List<InputValueDefinition> = definition.inputValueDefinitions + extensions.flatMap { it.inputValueDefinitions } |
| 203 | + override val location = DirectiveLocation.INPUT_OBJECT |
| 204 | + override val declarations get() = fields |
| 205 | + override fun newBuilder(context: TypeBuildContext): GraphQLInputObjectType.Builder = |
| 206 | + GraphQLInputObjectType.newInputObject().definition(definition).extensionDefinitions(extensions) |
| 207 | + } |
| 208 | + |
| 209 | + class Scalar( |
| 210 | + private val definition: ScalarTypeDefinition, |
| 211 | + private val extensions: List<ScalarTypeExtensionDefinition>, |
| 212 | + ) : MergedType<GraphQLScalarType.Builder>(definition, extensions) { |
| 213 | + override val location = DirectiveLocation.SCALAR |
| 214 | + override val declarations = emptyList<NamedNode<*>>() |
| 215 | + |
| 216 | + // like graphql-java, only the schema's directives are applied |
| 217 | + override fun newBuilder(context: TypeBuildContext): GraphQLScalarType.Builder { |
| 218 | + val provided = context.customScalars.getValue(name) |
| 219 | + return GraphQLScalarType.newScalar(provided) |
| 220 | + .definition(definition) |
| 221 | + .extensionDefinitions(extensions) |
| 222 | + .specifiedByUrl(specifiedByUrl() ?: provided.specifiedByUrl) |
| 223 | + .clearDirectives() |
| 224 | + } |
| 225 | + |
| 226 | + private fun specifiedByUrl(): String? = |
| 227 | + (directives.find { it.name == Directives.SpecifiedByDirective.name }?.getArgument("url")?.value as? StringValue)?.value |
| 228 | + } |
| 229 | +} |
0 commit comments