All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.nadel.util.NamespacedUtil.kt Maven / Gradle / Ivy

Go to download

Nadel is a Java library that combines multiple GrahpQL services together into one API.

There is a newer version: 2024-12-10T04-34-06-f2ee9344
Show newest version
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