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;
}
Aggregations