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

graphql.schema.impl.SchemaUtil Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.schema.impl;


import com.google.common.collect.ImmutableMap;
import graphql.Internal;
import graphql.schema.GraphQLImplementingType;
import graphql.schema.GraphQLNamedOutputType;
import graphql.schema.GraphQLNamedType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLSchemaElement;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeResolvingVisitor;
import graphql.schema.GraphQLTypeVisitor;
import graphql.schema.SchemaTraverser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Internal
public class SchemaUtil {

    /**
     * Called to visit a partially build schema (during {@link GraphQLSchema} build phases) with a set of visitors
     *
     * Each visitor is expected to hold its own side effects that might be last used to construct a full schema
     *
     * @param partiallyBuiltSchema the partially built schema
     * @param visitors             the visitors to call
     */

    public static void visitPartiallySchema(final GraphQLSchema partiallyBuiltSchema, GraphQLTypeVisitor... visitors) {
        List roots = new ArrayList<>();
        roots.add(partiallyBuiltSchema.getQueryType());

        if (partiallyBuiltSchema.isSupportingMutations()) {
            roots.add(partiallyBuiltSchema.getMutationType());
        }

        if (partiallyBuiltSchema.isSupportingSubscriptions()) {
            roots.add(partiallyBuiltSchema.getSubscriptionType());
        }

        if (partiallyBuiltSchema.getAdditionalTypes() != null) {
            roots.addAll(partiallyBuiltSchema.getAdditionalTypes());
        }

        if (partiallyBuiltSchema.getDirectives() != null) {
            roots.addAll(partiallyBuiltSchema.getDirectives());
        }

        roots.add(partiallyBuiltSchema.getIntrospectionSchemaType());

        GraphQLTypeVisitor visitor = new MultiReadOnlyGraphQLTypeVisitor(Arrays.asList(visitors));
        SchemaTraverser traverser;
        traverser = new SchemaTraverser(schemaElement -> schemaElement.getChildrenWithTypeReferences().getChildrenAsList());
        traverser.depthFirst(visitor, roots);
    }

    public static ImmutableMap> groupInterfaceImplementationsByName(List allTypesAsList) {
        Map> result = new LinkedHashMap<>();
        for (GraphQLType type : allTypesAsList) {
            if (type instanceof GraphQLObjectType) {
                List interfaces = ((GraphQLObjectType) type).getInterfaces();
                for (GraphQLNamedOutputType interfaceType : interfaces) {
                    List myGroup = result.computeIfAbsent(interfaceType.getName(), k -> new ArrayList<>());
                    myGroup.add((GraphQLObjectType) type);
                }
            }
        }
        return ImmutableMap.copyOf(new TreeMap<>(result));
    }

    public Map> groupImplementationsForInterfacesAndObjects(GraphQLSchema schema) {
        Map> result = new LinkedHashMap<>();
        for (GraphQLType type : schema.getAllTypesAsList()) {
            if (type instanceof GraphQLImplementingType) {
                List interfaces = ((GraphQLImplementingType) type).getInterfaces();
                for (GraphQLNamedOutputType interfaceType : interfaces) {
                    List myGroup = result.computeIfAbsent(interfaceType.getName(), k -> new ArrayList<>());
                    myGroup.add((GraphQLImplementingType) type);
                }
            }
        }
        return ImmutableMap.copyOf(new TreeMap<>(result));
    }

    public static void replaceTypeReferences(GraphQLSchema schema) {
        final Map typeMap = schema.getTypeMap();
        List roots = new ArrayList<>(typeMap.values());
        roots.addAll(schema.getDirectives());
        SchemaTraverser schemaTraverser = new SchemaTraverser(schemaElement -> schemaElement.getChildrenWithTypeReferences().getChildrenAsList());
        schemaTraverser.depthFirst(new GraphQLTypeResolvingVisitor(typeMap), roots);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy