@@ -0,0 +1,4078 @@
+package com.google.api.graphql.rejoiner;%0A%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.ImmutableSet;%0Aimport graphql.schema.GraphQLEnumType;%0Aimport graphql.schema.GraphQLInputObjectType;%0Aimport graphql.schema.GraphQLInterfaceType;%0Aimport graphql.schema.GraphQLNamedType;%0Aimport graphql.schema.GraphQLObjectType;%0Aimport graphql.schema.GraphQLSchema;%0Aimport graphql.schema.GraphQLSchemaElement;%0Aimport graphql.schema.GraphQLType;%0Aimport graphql.schema.GraphQLTypeReference;%0Aimport graphql.schema.GraphQLTypeVisitorStub;%0Aimport graphql.schema.SchemaTraverser;%0Aimport graphql.util.TraversalControl;%0Aimport graphql.util.TraverserContext;%0A%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.function.Consumer;%0A%0A/** Common GraphQL Schema transformations. */%0Apublic final class SchemaTransformations %7B%0A%0A  private SchemaTransformations() %7B%7D%0A%0A  /** Removes all named types that aren't reachable from additionalTypes. */%0A  public static Consumer%3CGraphQLSchema.Builder%3E removeUnreachableTypes() %7B%0A    return builder -%3E %7B%0A      GraphQLSchema inputSchema = builder.build();%0A      ImmutableList%3CGraphQLType%3E graphQLTypes =%0A          ImmutableList.copyOf(inputSchema.getAdditionalTypes());%0A      Set%3CString%3E reachableTypes = new HashSet%3C%3E();%0A      new SchemaTraverser()%0A          .depthFirst(%0A              new GraphQLTypeVisitorStub() %7B%0A                @Override%0A                public TraversalControl visitGraphQLInterfaceType(%0A                    GraphQLInterfaceType node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLInterfaceType(node, context);%0A                %7D%0A%0A                @Override%0A                public TraversalControl visitGraphQLEnumType(%0A                    GraphQLEnumType node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLEnumType(node, context);%0A                %7D%0A%0A                @Override%0A                public TraversalControl visitGraphQLInputObjectType(%0A                    GraphQLInputObjectType node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLInputObjectType(node, context);%0A                %7D%0A%0A                @Override%0A                public TraversalControl visitGraphQLObjectType(%0A                    GraphQLObjectType node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLObjectType(node, context);%0A                %7D%0A%0A                @Override%0A                public TraversalControl visitGraphQLTypeReference(%0A                    GraphQLTypeReference node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLTypeReference(node, context);%0A                %7D%0A%0A                @Override%0A                protected TraversalControl visitGraphQLType(%0A                    GraphQLSchemaElement node, TraverserContext%3CGraphQLSchemaElement%3E context) %7B%0A                  reachableTypes.add(((GraphQLNamedType) node).getName());%0A                  return super.visitGraphQLType(node, context);%0A                %7D%0A              %7D,%0A              inputSchema.isSupportingMutations()%0A                  ? ImmutableList.of(inputSchema.getQueryType(), inputSchema.getMutationType())%0A                  : ImmutableList.of(inputSchema.getQueryType()));%0A%0A      builder.clearAdditionalTypes();%0A      builder.additionalTypes(%0A          graphQLTypes.stream()%0A              .filter(%0A                  type -%3E%0A                      !(type instanceof GraphQLNamedType)%0A                          %7C%7C reachableTypes.contains(((GraphQLNamedType) type).getName()))%0A              .collect(ImmutableSet.toImmutableSet()));%0A    %7D;%0A  %7D%0A%7D%0A

Add utility to remove unreachable additionalTypes from a schema.