Skip to content

Commit

Permalink
Remove standard directives from SDL output
Browse files Browse the repository at this point in the history
  • Loading branch information
sachindshinde committed Mar 18, 2020
1 parent 7f02fdb commit 3e2da6a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public final class SchemaTransformer {
private static final Object DUMMY = new Object();
// Apollo Gateway will fail composition if it sees directive definitions for these
private static final Set<String> HIDDEN_DIRECTIVES =
new HashSet<>(Arrays.asList("deprecated", "include", "skip"));
private final GraphQLSchema originalSchema;
private TypeResolver entityTypeResolver = null;
private DataFetcher entitiesDataFetcher = null;
Expand Down Expand Up @@ -143,7 +148,9 @@ private String sdl() {
.includeScalarTypes(true)
.includeExtendedScalarTypes(true)
.includeSchemaDefinition(true)
.includeDirectives(true);
// Note that includeDirectives() will filter directive definitions and usages, with
// the exception of directive usages of @deprecated (which are never removed).
.includeDirectives(directive -> !HIDDEN_DIRECTIVES.contains(directive.getName()));
return new FederationSdlPrinter(options).print(originalSchema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void testEmpty() {
"\n" +
"type _Service {\n" +
" sdl: String!\n" +
"}\n", SchemaUtils.printSchema(federated));
"}\n", SchemaUtils.printWithoutStandardDirectiveDefinitions(federated));

final GraphQLType _Service = federated.getType("_Service");
assertNotNull(_Service, "_Service type present");
Expand Down Expand Up @@ -148,6 +148,9 @@ void testPrinter() {
typeDefinitionRegistry,
runtimeWiring
);
Assertions.assertEquals(printerSDL.trim(), new FederationSdlPrinter().print(graphQLSchema).trim());
Assertions.assertEquals(
printerSDL.trim(),
SchemaUtils.printWithoutStandardDirectiveDefinitions(graphQLSchema).trim()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
import graphql.ExecutionResult;
import graphql.schema.GraphQLSchema;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static graphql.ExecutionInput.newExecutionInput;
import static graphql.GraphQL.newGraphQL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

final class SchemaUtils {
static final Set<String> standardDirectives =
new HashSet<>(Arrays.asList("deprecated", "include", "skip"));

private SchemaUtils() {
}

static String printSchema(GraphQLSchema schema) {
return new FederationSdlPrinter().print(schema);
static String printWithoutStandardDirectiveDefinitions(GraphQLSchema schema) {
return new FederationSdlPrinter(FederationSdlPrinter.Options.defaultOptions()
.includeDirectives(directive -> !standardDirectives.contains(directive.getName()))
).print(schema);
}

static ExecutionResult execute(GraphQLSchema schema, String query) {
Expand Down

0 comments on commit 3e2da6a

Please sign in to comment.