Skip to content

Commit 1a10708

Browse files
authored
Applied Directives now only show argument values IF there is a value set. This is a better reflection of the type intentions (graphql-java#2367)
``` _DirectiveArgument { name : String! value : String! } ```
1 parent 3dff821 commit 1a10708

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/main/java/graphql/introspection/IntrospectionWithDirectivesSupport.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import graphql.PublicSpi;
66
import graphql.execution.ValuesResolver;
77
import graphql.language.AstPrinter;
8+
import graphql.language.Node;
89
import graphql.schema.DataFetcher;
910
import graphql.schema.GraphQLArgument;
1011
import graphql.schema.GraphQLCodeRegistry;
@@ -191,16 +192,15 @@ private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, G
191192
};
192193
DataFetcher<?> argsDF = env -> {
193194
final GraphQLDirective directive = env.getSource();
194-
return directive.getArguments();
195+
// we only show directive arguments that have values set on them
196+
return directive.getArguments().stream()
197+
.filter(arg -> arg.getArgumentValue().isSet());
195198
};
196199
DataFetcher<?> argValueDF = env -> {
197200
final GraphQLArgument argument = env.getSource();
198-
if (argument.hasSetValue()) {
199-
InputValueWithState value = argument.getArgumentValue();
200-
return AstPrinter.printAst(ValuesResolver.valueToLiteral(value, argument.getType()));
201-
} else {
202-
return null;
203-
}
201+
InputValueWithState value = argument.getArgumentValue();
202+
Node<?> literal = ValuesResolver.valueToLiteral(value, argument.getType());
203+
return AstPrinter.printAst(literal);
204204
};
205205
codeRegistry.dataFetcher(coordinates(objectType, "appliedDirectives"), df);
206206
codeRegistry.dataFetcher(coordinates(appliedDirectiveType, "args"), argsDF);

src/test/java/graphql/introspection/IntrospectionWithDirectivesSupportTest.groovy

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@ package graphql.introspection
22

33
import graphql.GraphQL
44
import graphql.TestUtil
5-
import graphql.schema.idl.SchemaPrinter
65
import spock.lang.Specification
76

87
class IntrospectionWithDirectivesSupportTest extends Specification {
98

10-
def printer = new SchemaPrinter(SchemaPrinter.Options.defaultOptions().includeIntrospectionTypes(true))
11-
129
def "can find directives in introspection"() {
1310
def sdl = '''
1411
directive @example( argName : String = "default") on OBJECT | FIELD_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION | SCHEMA
1512
directive @secret( argName : String = "secret") on OBJECT
13+
directive @noDefault( arg1 : String, arg2 : String) on OBJECT
1614
1715
schema @example(argName : "onSchema") {
1816
query : Query
1917
}
2018
21-
type Query @example(argName : "onQuery") {
19+
type Query @example(argName : "onQuery") @noDefault(arg1 : "set") {
2220
hello : Hello @deprecated
2321
}
2422
25-
type Hello @example {
23+
type Hello @example @noDefault {
2624
world : String @deprecated
2725
}
2826
@@ -33,9 +31,6 @@ class IntrospectionWithDirectivesSupportTest extends Specification {
3331

3432
def schema = TestUtil.schema(sdl)
3533
schema = new IntrospectionWithDirectivesSupport().apply(schema)
36-
37-
println printer.print(schema)
38-
3934
def graphql = GraphQL.newGraphQL(schema).build()
4035

4136
def query = '''
@@ -94,15 +89,21 @@ class IntrospectionWithDirectivesSupportTest extends Specification {
9489

9590
def schemaType = er.data["__schema"]
9691

97-
schemaType["directives"] == [[name: "include"], [name: "skip"], [name: "example"], [name: "secret"], [name: "deprecated"], [name: "specifiedBy"]]
92+
schemaType["directives"] == [[name: "include"], [name: "skip"], [name: "example"], [name: "secret"], [name: "noDefault"], [name: "deprecated"], [name: "specifiedBy"]]
9893

9994
schemaType["appliedDirectives"] == [[name: "example", args: [[name: "argName", value: '"onSchema"']]]]
10095

10196
def queryType = er.data["__schema"]["types"].find({ type -> (type["name"] == "Query") })
102-
queryType["appliedDirectives"] == [[name: "example", args: [[name: "argName", value: '"onQuery"']]]]
97+
queryType["appliedDirectives"] == [
98+
[name: "example", args: [[name: "argName", value: '"onQuery"']]],
99+
[name: "noDefault", args: [[name: "arg1", value: '"set"']]]
100+
]
103101

104102
def helloType = er.data["__schema"]["types"].find({ type -> (type["name"] == "Hello") })
105-
helloType["appliedDirectives"] == [[name: "example", args: [[name: "argName", value: '"default"']]]]
103+
helloType["appliedDirectives"] == [
104+
[name: "example", args: [[name: "argName", value: '"default"']]],
105+
[name: "noDefault", args: []] // always empty list
106+
]
106107

107108
def worldField = helloType["fields"].find({ type -> (type["name"] == "world") })
108109
worldField["appliedDirectives"] == [[name: 'deprecated', args: [[name: 'reason', value: '"No longer supported"']]]]

0 commit comments

Comments
 (0)