Skip to content

Commit

Permalink
polishing a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
andimarek committed Feb 17, 2021
1 parent 7bd34d5 commit 9819cd8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Anonymize GraphQL schemas and queries

GraphQL anonymizer is CLI tool to anonymize schemas and queries by replacing all names with generic values
like `field22`.

For example this schema and query:

```graphql
type Query{
mySecret(arg: String): MySecret
}
type MySecret {
id: ID
dontExposeThat: String
}

query MySecretOp{mySecret(arg: "myValue") { id dontExposeThat } }
```

will be anonymized into:

```graphql
schema {
query: Object1
}

type Object1 {
field1(argument1: String): Object2
}

type Object2 {
field2: ID
field3: String
}

query {field1(argument1:"stringValue1") {field2 field3}}

```

## Installation and usage

The easiest way to install it is via `npm`:

```sh
npm i -g graphql-anonymizer
```

To anonymize a schema and query from a file:

```sh
graphql-anonymizer -s schema-file -q query-file
```

The `-q` option is optional, you can also just anonymize a schema:

```sh
graphql-anonymizer -s schema-file
```

Alternatively the schema file can be read from stdin:

```sh
graphql-anonymizer < schema-file
```

The full list of options is available via `--help`:

```sh
graphql-anonymizer --help
```

## Bugs of feedback

Please open an Issue to raise a bug or other feedback.

## Implementation details

GraphQL Anonymizer is a thin wrapper around a
[class inside GraphQL Java](https://github.com/graphql-java/graphql-java/blob/master/src/main/java/graphql/util/Anonymizer.java)
It is compiled via [GraalVM](graalvm.org) to a native binary and distributed via NPM.

The cross-platform compiling, releasing and testing is done fully automatically via GitHub Actions. Have a look
at [release.yml](./.github/workflows/release.yml) and [npm](/npm) if you want to know all the details.
41 changes: 29 additions & 12 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import graphql.Directives;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.DirectiveInfo;
import graphql.schema.idl.SchemaGenerator;
Expand All @@ -8,17 +7,16 @@
import graphql.util.Anonymizer;
import picocli.CommandLine;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.concurrent.Callable;

import static picocli.CommandLine.*;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;

@Command(name = "graphql-anonymizer", mixinStandardHelpOptions = true, version = "graphql-anonymizer 1.0",
description = "Anonymize GraphQL schemas and queries")
Expand All @@ -28,38 +26,57 @@ public class Main implements Callable<String> {
@Option(names = {"-s", "--schema"}, description = "The GraphQL schema file", paramLabel = "schema-file")
private File schemaFile;

@Option(names = {"-q", "--query"}, description = "A GraphQL query file", paramLabel = "query-file")
private File queryFile;

@Option(names = {"-v", "--verbose"}, description = "print out more details", defaultValue = "false")
private boolean verbose;

@Override
public String call() throws Exception {
String sdl;
if (schemaFile != null) {
logVerbose("Loading schema from file %s%n", schemaFile);
sdl = Files.readString(schemaFile.toPath());
} else {
logVerbose("Loading schema from stdin%n");
List<String> lines = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}
sdl = String.join("\n", lines);
}
if (verbose) {
System.out.printf("Loaded schema: %s%n", sdl);
logVerbose("Loaded schema: %s%n", sdl);
String query = null;
if (queryFile != null) {
logVerbose("Loading query from file %s%n", queryFile);
query = Files.readString(queryFile.toPath());
logVerbose("Loaded query %s%n", query);
}
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(sdl);
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, MockedWiring.MOCKED_WIRING);
GraphQLSchema anonSchema = Anonymizer.anonymizeSchema(graphQLSchema);

Anonymizer.AnonymizeResult anonymizeResult = Anonymizer.anonymizeSchemaAndQueries(graphQLSchema, query != null ? Collections.singletonList(query) : Collections.emptyList());


SchemaPrinter.Options options = SchemaPrinter.Options.defaultOptions();
options = options.includeDirectives(graphQLDirective -> !DirectiveInfo.isGraphqlSpecifiedDirective(graphQLDirective));
String printedSchema = new SchemaPrinter(options).print(anonSchema);
String printedSchema = new SchemaPrinter(options).print(anonymizeResult.getSchema());
System.out.println(printedSchema);
System.out.println();
if (anonymizeResult.getQueries().size() > 0) {
System.out.println(anonymizeResult.getQueries().get(0));
}
return printedSchema;
}

// this example implements Callable, so parsing, error handling and handling user
// requests for usage help or version help can be done with one line of code.
private void logVerbose(String string, Object... args) {
if (verbose) {
System.out.printf(string, args);
}
}

public static void main(String... args) {
int exitCode = new CommandLine(new Main()).execute(args);
System.exit(exitCode);
Expand Down
1 change: 1 addition & 0 deletions test.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{mySecret(arg: "myValue") { id dontExposeThat } }
7 changes: 7 additions & 0 deletions test.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Query{
mySecret(arg: String): MySecret
}
type MySecret {
id: ID
dontExposeThat: String
}

0 comments on commit 9819cd8

Please sign in to comment.