1
- import graphql .Directives ;
2
1
import graphql .schema .GraphQLSchema ;
3
2
import graphql .schema .idl .DirectiveInfo ;
4
3
import graphql .schema .idl .SchemaGenerator ;
8
7
import graphql .util .Anonymizer ;
9
8
import picocli .CommandLine ;
10
9
11
- import java .io .BufferedReader ;
12
10
import java .io .File ;
13
- import java .io .InputStreamReader ;
14
11
import java .nio .file .Files ;
15
12
import java .util .ArrayList ;
13
+ import java .util .Collections ;
16
14
import java .util .List ;
17
15
import java .util .Scanner ;
18
- import java .util .StringTokenizer ;
19
16
import java .util .concurrent .Callable ;
20
17
21
- import static picocli .CommandLine .*;
18
+ import static picocli .CommandLine .Command ;
19
+ import static picocli .CommandLine .Option ;
22
20
23
21
@ Command (name = "graphql-anonymizer" , mixinStandardHelpOptions = true , version = "graphql-anonymizer 1.0" ,
24
22
description = "Anonymize GraphQL schemas and queries" )
@@ -28,38 +26,57 @@ public class Main implements Callable<String> {
28
26
@ Option (names = {"-s" , "--schema" }, description = "The GraphQL schema file" , paramLabel = "schema-file" )
29
27
private File schemaFile ;
30
28
29
+ @ Option (names = {"-q" , "--query" }, description = "A GraphQL query file" , paramLabel = "query-file" )
30
+ private File queryFile ;
31
+
31
32
@ Option (names = {"-v" , "--verbose" }, description = "print out more details" , defaultValue = "false" )
32
33
private boolean verbose ;
33
34
34
35
@ Override
35
36
public String call () throws Exception {
36
37
String sdl ;
37
38
if (schemaFile != null ) {
39
+ logVerbose ("Loading schema from file %s%n" , schemaFile );
38
40
sdl = Files .readString (schemaFile .toPath ());
39
41
} else {
42
+ logVerbose ("Loading schema from stdin%n" );
40
43
List <String > lines = new ArrayList <>();
41
- BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
42
44
Scanner scanner = new Scanner (System .in );
43
45
while (scanner .hasNext ()) {
44
46
lines .add (scanner .nextLine ());
45
47
}
46
48
sdl = String .join ("\n " , lines );
47
49
}
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 );
50
56
}
51
57
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser ().parse (sdl );
52
58
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
+
54
63
SchemaPrinter .Options options = SchemaPrinter .Options .defaultOptions ();
55
64
options = options .includeDirectives (graphQLDirective -> !DirectiveInfo .isGraphqlSpecifiedDirective (graphQLDirective ));
56
- String printedSchema = new SchemaPrinter (options ).print (anonSchema );
65
+ String printedSchema = new SchemaPrinter (options ).print (anonymizeResult . getSchema () );
57
66
System .out .println (printedSchema );
67
+ System .out .println ();
68
+ if (anonymizeResult .getQueries ().size () > 0 ) {
69
+ System .out .println (anonymizeResult .getQueries ().get (0 ));
70
+ }
58
71
return printedSchema ;
59
72
}
60
73
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
+
63
80
public static void main (String ... args ) {
64
81
int exitCode = new CommandLine (new Main ()).execute (args );
65
82
System .exit (exitCode );
0 commit comments