8000 directive filtering during printing by bbakerman · Pull Request #3387 · graphql-java/graphql-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
86 changes: 67 additions & 19 deletions src/main/java/graphql/schema/idl/SchemaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public static class Options {

private final boolean descriptionsAsHashComments;

private final Predicate<String> includeDirectiveDefinition;

private final Predicate<String> includeDirective;

private final Predicate<GraphQLSchemaElement> includeSchemaElement;
Expand All @@ -110,6 +112,7 @@ private Options(boolean includeIntrospectionTypes,
boolean includeScalars,
boolean includeSchemaDefinition,
boolean includeDirectiveDefinitions,
Predicate<String> includeDirectiveDefinition,
boolean useAstDefinitions,
boolean descriptionsAsHashComments,
Predicate<String> includeDirective,
Expand All @@ -120,6 +123,7 @@ private Options(boolean includeIntrospectionTypes,
this.includeScalars = includeScalars;
this.includeSchemaDefinition = includeSchemaDefinition;
this.includeDirectiveDefinitions = includeDirectiveDefinitions;
this.includeDirectiveDefinition = includeDirectiveDefinition;
this.includeDirective = includeDirective;
this.useAstDefinitions = useAstDefinitions;
this.descriptionsAsHashComments = descriptionsAsHashComments;
Expand All @@ -144,6 +148,10 @@ public boolean isIncludeDirectiveDefinitions() {
return includeDirectiveDefinitions;
}

public Predicate<String> getIncludeDirectiveDefinition() {
return includeDirectiveDefinition;
}

public Predicate<String> getIncludeDirective() {
return includeDirective;
}
Expand All @@ -164,14 +172,16 @@ public boolean isUseAstDefinitions() {
return useAstDefinitions;
}

public boolean isIncludeAstDefinitionComments() { return includeAstDefinitionComments; }
public boolean isIncludeAstDefinitionComments() {
return includeAstDefinitionComments;
}

public static Options defaultOptions() {
return new Options(false,
true,
false,
true,
false,
directive -> true, false,
false,
directive -> true,
element -> true,
Expand All @@ -191,7 +201,7 @@ public Options includeIntrospectionTypes(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.useAstDefinitions,
this.includeDirectiveDefinition, this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
this.includeSchemaElement,
Expand All @@ -211,7 +221,7 @@ public Options includeScalarTypes(boolean flag) {
flag,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.useAstDefinitions,
this.includeDirectiveDefinition, this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
this.includeSchemaElement,
Expand All @@ -234,6 +244,7 @@ public Options includeSchemaDefinition(boolean flag) {
this.includeScalars,
flag,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
Expand All @@ -259,6 +270,29 @@ public Options includeDirectiveDefinitions(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
flag,
directive -> flag,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
this.includeSchemaElement,
this.comparatorRegistry,
this.includeAstDefinitionComments);
}


/**
* This is a Predicate that decides whether a directive definition is printed.
*
* @param includeDirectiveDefinition the predicate to decide of a directive defintion is printed
*
* @return new instance of options
*/
public Options includeDirectiveDefinition(Predicate<String> includeDirectiveDefinition) {
return new Options(this.includeIntrospectionTypes,
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
Expand All @@ -280,6 +314,7 @@ public Options includeDirectives(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
directive -> flag,
Expand All @@ -300,6 +335,7 @@ public Options includeDirectives(Predicate<String> includeDirective) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
includeDirective,
Expand All @@ -308,6 +344,7 @@ public Options includeDirectives(Predicate<String> includeDirective) {
this.includeAstDefinitionComments);
}


/**
* This is a general purpose Predicate that decides whether a schema element is printed ever.
*
Expand All @@ -321,6 +358,7 @@ public Options includeSchemaElement(Predicate<GraphQLSchemaElement> includeSchem
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
Expand All @@ -342,6 +380,7 @@ public Options useAstDefinitions(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
flag,
this.descriptionsAsHashComments,
this.includeDirective,
Expand All @@ -365,6 +404,7 @@ public Options descriptionsAsHashComments(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
flag,
this.includeDirective,
Expand All @@ -387,6 +427,7 @@ public Options setComparators(GraphqlTypeComparatorRegistry comparatorRegistry)
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
Expand All @@ -409,6 +450,7 @@ public Options includeAstDefinitionComments(boolean flag) {
this.includeScalars,
this.includeSchemaDefinition,
this.includeDirectiveDefinitions,
this.includeDirectiveDefinition,
this.useAstDefinitions,
this.descriptionsAsHashComments,
this.includeDirective,
Expand Down Expand Up @@ -638,7 +680,9 @@ private SchemaElementPrinter<GraphQLUnionType> unionPrinter() {

private SchemaElementPrinter<GraphQLDirective> directivePrinter() {
return (out, directive, visibility) -> {
if (options.isIncludeDirectiveDefinitions()) {
boolean isOnEver = options.isIncludeDirectiveDefinitions();
boolean specificTest = options.getIncludeDirectiveDefinition().test(directive.getName());
if (isOnEver && specificTest) {
String s = directiveDefinition(directive);
out.format("%s", s);
out.print("\n\n");
Expand Down Expand Up @@ -876,7 +920,7 @@ String directivesString(Class<? extends GraphQLSchemaElement> parentType, boolea
private String directivesString(Class<? extends GraphQLSchemaElement> parentType, List<GraphQLAppliedDirective> directives) {
directives = directives.stream()
// @deprecated is special - we always print it if something is deprecated
.filter(directive -> options.getIncludeDirective().test(directive.getName()) || isDeprecatedDirective(directive))
.filter(directive -> options.getIncludeDirective().test(directive.getName()))
.filter(options.getIncludeSchemaElement())
.collect(toList());

Expand Down Expand Up @@ -909,10 +953,7 @@ private String directiveString(GraphQLAppliedDirective directive) {
return "";
}
if (!options.getIncludeDirective().test(directive.getName())) {
// @deprecated is special - we always print it if something is deprecated
if (!isDeprecatedDirective(directive)) {
return "";
}
return "";
}

StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -948,6 +989,13 @@ private String directiveString(GraphQLAppliedDirective directive) {
return sb.toString();
}

private boolean isDeprecatedDirectiveAllowed() {
// we ask if the special deprecated directive,
// which can be programmatically on a type without an applied directive,
// should be printed or not
return options.getIncludeDirective().test(DeprecatedDirective.getName());
}

private boolean isDeprecatedDirective(GraphQLAppliedDirective directive) {
return directive.getName().equals(DeprecatedDirective.getName());
}
Expand All @@ -960,14 +1008,14 @@ private boolean hasDeprecatedDirective(List<GraphQLAppliedDirective> directives)

private List<GraphQLAppliedDirective> addDeprecatedDirectiveIfNeeded(GraphQLDirectiveContainer directiveContainer) {
List<GraphQLAppliedDirective> directives = DirectivesUtil.toAppliedDirectives(directiveContainer);
if (!hasDeprecatedDirective(directives)) {
if (!hasDeprecatedDirective(directives) && isDeprecatedDirectiveAllowed()) {
directives = new ArrayList<>(directives);
String reason = getDeprecationReason(directiveContainer);
GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument()
.name("reason")
.valueProgrammatic(reason)
.type(GraphQLString)
.build();
String reason = getDeprecationReason(directiveContainer);
GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument()
.name("reason")
.valueProgrammatic(reason)
.type(GraphQLString)
.build();
GraphQLAppliedDirective directive = GraphQLAppliedDirective.newDirective()
.name("deprecated")
.argument(arg)
Expand Down 86EF Expand Up @@ -1104,7 +1152,7 @@ private void printComments(PrintWriter out, Object graphQLType, String prefix) {
if (options.isIncludeAstDefinitionComments()) {
String commentsText = getAstDefinitionComments(graphQLType);
if (!isNullOrEmpty(commentsText)) {
List<String> lines = Arrays.asList(commentsText.split("\n") );
List<String> lines = Arrays.asList(commentsText.split("\n"));
if (!lines.isEmpty()) {
printMultiLineHashDescription(out, prefix, lines);
}
Expand Down Expand Up @@ -1179,7 +1227,7 @@ private String getAstDefinitionComments(Object commentHolder) {
}

private String comments(List<Comment> comments) {
if ( comments == null || comments.isEmpty() ) {
if (comments == null || comments.isEmpty()) {
return null;
}
String s = comments.stream().map(c -> c.getContent()).collect(joining("\n", "", "\n"));
Expand Down
Loading
0