Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support excluding directives in FederationSdlPrinter #79 #80

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public final class SchemaTransformer {
private DataFetcher entitiesDataFetcher = null;
private DataFetcherFactory entitiesDataFetcherFactory = null;
private Coercing coercingForAny = _Any.defaultCoercing;
private Set<String> excludedDirectives = null;

SchemaTransformer(GraphQLSchema originalSchema) {
this.originalSchema = originalSchema;
Expand Down Expand Up @@ -62,6 +63,18 @@ public SchemaTransformer coercingForAny(Coercing coercing) {
return this;
}

public SchemaTransformer excludeDirectives(@NotNull Set<String> excludedDirectives){
FederationDirectives.allNames
.stream()
.filter(federationDirectiveName -> excludedDirectives.contains(federationDirectiveName))
.findAny()
.ifPresent(federationDirectiveName -> {
throw new FederationError("Cannot exclude "+federationDirectiveName + " directive");
});
this.excludedDirectives = excludedDirectives;
return this;
}

@NotNull
public final GraphQLSchema build() throws SchemaProblem {
final List<GraphQLError> errors = new ArrayList<>();
Expand All @@ -75,7 +88,7 @@ public final GraphQLSchema build() throws SchemaProblem {
GraphQLCodeRegistry.newCodeRegistry(originalSchema.getCodeRegistry());

// Print the original schema as sdl and expose it as query { _service { sdl } }
final String sdl = sdl(originalSchema);
final String sdl = sdl(originalSchema,excludedDirectives);
final GraphQLObjectType.Builder newQueryType = GraphQLObjectType.newObject(originalQueryType)
.field(_Service.field);
newCodeRegistry.dataFetcher(FieldCoordinates.coordinates(
Expand Down Expand Up @@ -143,7 +156,7 @@ public final GraphQLSchema build() throws SchemaProblem {
.build();
}

public static String sdl(GraphQLSchema schema) {
public static String sdl(GraphQLSchema schema,Set<String> excludedDirectives) {
// Gather directive definitions to hide.
final Set<String> hiddenDirectiveDefinitions = new HashSet<>();
hiddenDirectiveDefinitions.addAll(STANDARD_DIRECTIVES);
Expand All @@ -167,7 +180,7 @@ public static String sdl(GraphQLSchema schema) {
final FederationSdlPrinter.Options options = FederationSdlPrinter.Options.defaultOptions()
.includeScalarTypes(true)
.includeSchemaDefinition(true)
.includeDirectives(true)
.includeDirectives(directive -> excludedDirectives == null || !excludedDirectives.contains(directive.getName()))
.includeDirectiveDefinitions(def -> !hiddenDirectiveDefinitions.contains(def.getName()))
.includeTypeDefinitions(def -> !hiddenTypeDefinitions.contains(def.getName()));
return new FederationSdlPrinter(options).print(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ class FederationTest {
private final String printerEscapingExpectedSDL = TestUtils.readResource("schemas/printerEscapingExpected.graphql");
private final String printerFilterSDL = TestUtils.readResource("schemas/printerFilter.graphql");
private final String printerFilterExpectedSDL = TestUtils.readResource("schemas/printerFilterExpected.graphql");
private final String printerFilterDirectiveSDL = TestUtils.readResource("schemas/printerFilterDirective.graphql");
private final String printerFilterDirectiveExpectedSDL = TestUtils.readResource("schemas/printerFilterDirectiveExpected.graphql");
private final Set<String> standardDirectives =
new HashSet<>(Arrays.asList("deprecated", "include", "skip", "specifiedBy"));

private final Set<String> excludedDirectives =
new HashSet<>(Arrays.asList("directive2"));
@Test
void testEmpty() {
final GraphQLSchema federated = Federation.transform(emptySDL)
Expand Down Expand Up @@ -234,4 +237,43 @@ void testPrinterFilter() {
).print(graphQLSchema).trim()
);
}

@Test
void testPrinterFilterExcluded() {
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(printerFilterDirectiveSDL);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Interface1", typeWiring -> typeWiring
.typeResolver(env -> null)
)
.type("Interface2", typeWiring -> typeWiring
.typeResolver(env -> null)
)
.scalar(GraphQLScalarType.newScalar()
.name("Scalar1")
.coercing(Scalars.GraphQLString.getCoercing())
.build()
)
.scalar(GraphQLScalarType.newScalar()
.name("Scalar2")
.coercing(Scalars.GraphQLString.getCoercing())
.build()
)
.build();
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(
typeDefinitionRegistry,
runtimeWiring
);
Assertions.assertEquals(
printerFilterDirectiveExpectedSDL.trim(),
new FederationSdlPrinter(FederationSdlPrinter.Options.defaultOptions()
.includeScalarTypes(true)
.includeDirectives(def ->
!excludedDirectives.contains(def.getName()))
.includeDirectiveDefinitions(def ->
!def.getName().endsWith("1") && !def.getName().endsWith("2") && !standardDirectives.contains(def.getName())
)
.includeTypeDefinitions(def -> !def.getName().endsWith("1"))
).print(graphQLSchema).trim()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
directive @directive1 on FIELD_DEFINITION

directive @directive2 on OBJECT

interface Interface1 {
dummy: Enum2 @directive1
}

interface Interface2 {
dummy: Interface1 @directive1
}

type Object1 @directive2 {
dummy: Scalar2 @directive1
}

type Object2 @directive2 {
dummy: Object1 @directive1
}

type Query {
dummyEnum: Enum1 @directive1
dummyScalar: Scalar1 @directive1
}

enum Enum1 {
DUMMY
}

enum Enum2 {
DUMMY
}

scalar Scalar1

scalar Scalar2

input InputObject1 {
dummy: String
}

input InputObject2 {
dummy: InputObject1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
interface Interface2 {
dummy: Interface1 @directive1
}

type Object2 {
dummy: Object1 @directive1
}

type Query {
dummyEnum: Enum1 @directive1
dummyScalar: Scalar1 @directive1
}

enum Enum2 {
DUMMY
}

scalar Scalar2

input InputObject2 {
dummy: InputObject1
}