Search in sources :

Example 6 with SchemaTraverser

use of graphql.schema.SchemaTraverser in project graphql-java by graphql-java.

the class FieldVisibilitySchemaTransformation method apply.

public final GraphQLSchema apply(GraphQLSchema schema) {
    Set<GraphQLType> observedBeforeTransform = new HashSet<>();
    Set<GraphQLType> observedAfterTransform = new HashSet<>();
    Set<GraphQLType> markedForRemovalTypes = new HashSet<>();
    // query, mutation, and subscription types should not be removed
    final Set<String> protectedTypeNames = getRootTypes(schema).stream().map(GraphQLObjectType::getName).collect(Collectors.toSet());
    beforeTransformationHook.run();
    new SchemaTraverser().depthFirst(new TypeObservingVisitor(observedBeforeTransform, schema), getRootTypes(schema));
    // remove fields
    GraphQLSchema interimSchema = transformSchema(schema, new FieldRemovalVisitor(visibleFieldPredicate, markedForRemovalTypes));
    new SchemaTraverser().depthFirst(new TypeObservingVisitor(observedAfterTransform, interimSchema), getRootTypes(interimSchema));
    // remove types that are not used after removing fields - (connected schema only)
    GraphQLSchema connectedSchema = transformSchema(interimSchema, new TypeVisibilityVisitor(protectedTypeNames, observedBeforeTransform, observedAfterTransform));
    // ensure markedForRemovalTypes are not referenced by other schema elements, and delete from the schema
    // the ones that aren't.
    GraphQLSchema finalSchema = removeUnreferencedTypes(markedForRemovalTypes, connectedSchema);
    afterTransformationHook.run();
    return finalSchema;
}
Also used : GraphQLType(graphql.schema.GraphQLType) SchemaTraverser(graphql.schema.SchemaTraverser) GraphQLSchema(graphql.schema.GraphQLSchema) HashSet(java.util.HashSet)

Aggregations

SchemaTraverser (graphql.schema.SchemaTraverser)6 GraphQLNamedType (graphql.schema.GraphQLNamedType)3 GraphQLSchema (graphql.schema.GraphQLSchema)3 GraphQLSchemaElement (graphql.schema.GraphQLSchemaElement)3 GraphQLType (graphql.schema.GraphQLType)3 GraphQLTypeVisitor (graphql.schema.GraphQLTypeVisitor)3 ArrayList (java.util.ArrayList)3 GraphQLNamedOutputType (graphql.schema.GraphQLNamedOutputType)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLTypeResolvingVisitor (graphql.schema.GraphQLTypeResolvingVisitor)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Assert.assertNotNull (graphql.Assert.assertNotNull)1 Internal (graphql.Internal)1 PublicApi (graphql.PublicApi)1 GraphQLArgument (graphql.schema.GraphQLArgument)1 GraphQLCodeRegistry (graphql.schema.GraphQLCodeRegistry)1 GraphQLDirective (graphql.schema.GraphQLDirective)1