graphql.nadel.util.NamespacedUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nadel Show documentation
Show all versions of nadel Show documentation
Nadel is a Java library that combines multiple GrahpQL services together into one API.
package graphql.nadel.util
import graphql.language.ObjectTypeDefinition
import graphql.language.ObjectTypeExtensionDefinition
import graphql.nadel.Service
import graphql.nadel.engine.util.operationTypes
import graphql.nadel.engine.util.unwrapAll
import graphql.nadel.schema.NadelDirectives.namespacedDirectiveDefinition
import graphql.normalized.ExecutableNormalizedField
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLNamedOutputType
import graphql.schema.GraphQLNamedType
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLSchema
object NamespacedUtil {
fun serviceOwnsNamespacedField(namespacedObjectType: GraphQLObjectType, service: Service): Boolean {
return serviceOwnsNamespacedField(namespacedObjectType.name, service)
}
fun serviceOwnsNamespacedField(namespacedObjectTypeName: String, service: Service): Boolean {
return service
.definitionRegistry
.getDefinitionsOfType()
.stream() // the type can't be an extension in the owning service
.filter { it !is ObjectTypeExtensionDefinition }
.anyMatch { it.name == namespacedObjectTypeName }
}
fun isNamespacedField(overallField: ExecutableNormalizedField, schema: GraphQLSchema): Boolean {
return overallField
.getFieldDefinitions(schema)
.any {
isNamespacedField(it)
}
}
fun isNamespacedField(definition: GraphQLFieldDefinition): Boolean {
return definition.hasAppliedDirective(namespacedDirectiveDefinition.name)
}
/**
* Even if this returns "true", nothing prevents this type from being used in a "non-namespace" field
*/
fun isNamespaceType(type: GraphQLNamedType, schema: GraphQLSchema): Boolean {
val namespaceFieldsWithThisType = schema.operationTypes
.asSequence()
.flatMap { it.fieldDefinitions }
.filter { isNamespacedField(it) }
.map { it.type.unwrapAll() }
.filterIsInstance()
.filter { it.name == type.name }
.toList()
return namespaceFieldsWithThisType.size == 1
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy