1- import graphql .Directives ;
21import graphql .schema .GraphQLSchema ;
32import graphql .schema .idl .DirectiveInfo ;
43import graphql .schema .idl .SchemaGenerator ;
87import graphql .util .Anonymizer ;
98import picocli .CommandLine ;
109
11- import java .io .BufferedReader ;
1210import java .io .File ;
13- import java .io .InputStreamReader ;
1411import java .nio .file .Files ;
1512import java .util .ArrayList ;
13+ import java .util .Collections ;
1614import java .util .List ;
1715import java .util .Scanner ;
18- import java .util .StringTokenizer ;
1916import java .util .concurrent .Callable ;
2017
21- import static picocli .CommandLine .*;
18+ import static picocli .CommandLine .Command ;
19+ import static picocli .CommandLine .Option ;
2220
2321@ Command (name = "graphql-anonymizer" , mixinStandardHelpOptions = true , version = "graphql-anonymizer 1.0" ,
2422 description = "Anonymize GraphQL schemas and queries" )
@@ -28,38 +26,57 @@ public class Main implements Callable<String> {
2826 @ Option (names = {"-s" , "--schema" }, description = "The GraphQL schema file" , paramLabel = "schema-file" )
2927 private File schemaFile ;
3028
29+ @ Option (names = {"-q" , "--query" }, description = "A GraphQL query file" , paramLabel = "query-file" )
30+ private File queryFile ;
31+
3132 @ Option (names = {"-v" , "--verbose" }, description = "print out more details" , defaultValue = "false" )
3233 private boolean verbose ;
3334
3435 @ Override
3536 public String call () throws Exception {
3637 String sdl ;
3738 if (schemaFile != null ) {
39+ logVerbose ("Loading schema from file %s%n" , schemaFile );
3840 sdl = Files .readString (schemaFile .toPath ());
3941 } else {
42+ logVerbose ("Loading schema from stdin%n" );
4043 List <String > lines = new ArrayList <>();
41- BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
4244 Scanner scanner = new Scanner (System .in );
4345 while (scanner .hasNext ()) {
4446 lines .add (scanner .nextLine ());
4547 }
4648 sdl = String .join ("\n " , lines );
4749 }
48- if (verbose ) {
49- System .out .printf ("Loaded schema: %s%n" , sdl );
50+ logVerbose ("Loaded schema: %s%n" , sdl );
51+ String query = null ;
52+ if (queryFile != null ) {
53+ logVerbose ("Loading query from file %s%n" , queryFile );
54+ query = Files .readString (queryFile .toPath ());
55+ logVerbose ("Loaded query %s%n" , query );
5056 }
5157 TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser ().parse (sdl );
5258 GraphQLSchema graphQLSchema = new SchemaGenerator ().makeExecutableSchema (typeDefinitionRegistry , MockedWiring .MOCKED_WIRING );
53- GraphQLSchema anonSchema = Anonymizer .anonymizeSchema (graphQLSchema );
59+
60+ Anonymizer .AnonymizeResult anonymizeResult = Anonymizer .anonymizeSchemaAndQueries (graphQLSchema , query != null ? Collections .singletonList (query ) : Collections .emptyList ());
61+
62+
5463 SchemaPrinter .Options options = SchemaPrinter .Options .defaultOptions ();
5564 options = options .includeDirectives (graphQLDirective -> !DirectiveInfo .isGraphqlSpecifiedDirective (graphQLDirective ));
56- String printedSchema = new SchemaPrinter (options ).print (anonSchema );
65+ String printedSchema = new SchemaPrinter (options ).print (anonymizeResult . getSchema () );
5766 System .out .println (printedSchema );
67+ System .out .println ();
68+ if (anonymizeResult .getQueries ().size () > 0 ) {
69+ System .out .println (anonymizeResult .getQueries ().get (0 ));
70+ }
5871 return printedSchema ;
5972 }
6073
61- // this example implements Callable, so parsing, error handling and handling user
62- // requests for usage help or version help can be done with one line of code.
74+ private void logVerbose (String string , Object ... args ) {
75+ if (verbose ) {
76+ System .out .printf (string , args );
77+ }
78+ }
79+
6380 public static void main (String ... args ) {
6481 int exitCode = new CommandLine (new Main ()).execute (args );
6582 System .exit (exitCode );
0 commit comments