Skip to content

Commit d54745b

Browse files
committed
Bad init of class and also fixed a test
1 parent 573a61d commit d54745b

File tree

5 files changed

+27
-134
lines changed

5 files changed

+27
-134
lines changed

src/main/java/graphql/Directives.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static graphql.introspection.Introspection.DirectiveLocation.INLINE_FRAGMENT;
1717
import static graphql.introspection.Introspection.DirectiveLocation.SCALAR;
1818
import static graphql.language.DirectiveLocation.newDirectiveLocation;
19+
import static graphql.language.InputValueDefinition.*;
1920
import static graphql.language.NonNullType.newNonNullType;
2021
import static graphql.language.TypeName.newTypeName;
2122
import static graphql.schema.GraphQLArgument.newArgument;
@@ -27,18 +28,22 @@
2728
@PublicApi
2829
public class Directives {
2930

31+
private static final String SPECIFIED_BY = "specifiedBy";
32+
private static final String DEPRECATED = "deprecated";
33+
3034
public static final String NO_LONGER_SUPPORTED = "No longer supported";
3135
public static final DirectiveDefinition DEPRECATED_DIRECTIVE_DEFINITION;
3236
public static final DirectiveDefinition SPECIFIED_BY_DIRECTIVE_DEFINITION;
3337

38+
3439
static {
3540
DEPRECATED_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
36-
.name(Directives.DeprecatedDirective.getName())
41+
.name(DEPRECATED)
3742
.directiveLocation(newDirectiveLocation().name(FIELD_DEFINITION.name()).build())
3843
.directiveLocation(newDirectiveLocation().name(ENUM_VALUE.name()).build())
3944
.description(createDescription("Marks the field or enum value as deprecated"))
4045
.inputValueDefinition(
41-
InputValueDefinition.newInputValueDefinition()
46+
newInputValueDefinition()
4247
.name("reason")
4348
.description(createDescription("The reason for the deprecation"))
4449
.type(newTypeName().name("String").build())
@@ -47,18 +52,17 @@ public class Directives {
4752
.build();
4853

4954
SPECIFIED_BY_DIRECTIVE_DEFINITION = DirectiveDefinition.newDirectiveDefinition()
50-
.name(Directives.SpecifiedByDirective.getName())
55+
.name(SPECIFIED_BY)
5156
.directiveLocation(newDirectiveLocation().name(SCALAR.name()).build())
5257
.description(createDescription("Exposes a URL that specifies the behaviour of this scalar."))
5358
.inputValueDefinition(
54-
InputValueDefinition.newInputValueDefinition()
59+
newInputValueDefinition()
5560
.name("url")
5661
.description(createDescription("The URL that specifies the behaviour of this scalar."))
5762
.type(newNonNullType(newTypeName().name("String").build()).build())
5863
.build())
5964
.build();
6065
}
61-
6266
public static final GraphQLDirective IncludeDirective = GraphQLDirective.newDirective()
6367
.name("include")
6468
.description("Directs the executor to include this field or fragment only when the `if` argument is true")
@@ -86,7 +90,7 @@ public class Directives {
8690
* See https://graphql.github.io/graphql-spec/June2018/#sec--deprecated
8791
*/
8892
public static final GraphQLDirective DeprecatedDirective = GraphQLDirective.newDirective()
89-
.name("deprecated")
93+
.name(DEPRECATED)
9094
.description("Marks the field or enum value as deprecated")
9195
.argument(newArgument()
9296
.name("reason")
@@ -101,7 +105,7 @@ public class Directives {
101105
* The "specifiedBy" directive allows to provide a specification URL for a Scalar
102106
*/
103107
public static final GraphQLDirective SpecifiedByDirective = GraphQLDirective.newDirective()
104-
.name("specifiedBy")
108+
.name(SPECIFIED_BY)
105109
.description("Exposes a URL that specifies the behaviour of this scalar.")
106110
.argument(newArgument()
107111
.name("url")

src/main/java/graphql/schema/idl/SchemaTypeChecker.java

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
import graphql.language.Node;
1616
import graphql.language.ObjectTypeDefinition;
1717
import graphql.language.ObjectTypeExtensionDefinition;
18-
import graphql.language.StringValue;
1918
import graphql.language.Type;
2019
import graphql.language.TypeDefinition;
2120
import graphql.language.TypeName;
2221
import graphql.language.UnionTypeDefinition;
2322
import graphql.schema.idl.errors.DirectiveIllegalLocationError;
24-
import graphql.schema.idl.errors.InvalidDeprecationDirectiveError;
2523
import graphql.schema.idl.errors.MissingInterfaceTypeError;
2624
import graphql.schema.idl.errors.MissingScalarImplementationError;
2725
import graphql.schema.idl.errors.MissingTypeError;
@@ -43,10 +41,7 @@
4341
import java.util.function.Consumer;
4442
import java.util.function.Function;
4543
import java.util.function.Predicate;
46-
import java.util.function.Supplier;
4744

48-
import static graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE;
49-
import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION;
5045
import static graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION;
5146
import static java.util.stream.Collectors.toList;
5247

@@ -219,8 +214,6 @@ private void checkObjTypeFields(List<GraphQLError> errors, ObjectTypeDefinition
219214
(directiveName, directive) -> new NonUniqueDirectiveError(typeDefinition, fld, directiveName)));
220215

221216
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
222-
checkDeprecatedDirective(errors, directive, FIELD_DEFINITION,
223-
() -> new InvalidDeprecationDirectiveError(typeDefinition, fld));
224217

225218
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
226219
(argumentName, argument) -> new NonUniqueArgumentError(typeDefinition, fld, argumentName));
@@ -241,14 +234,9 @@ private void checkInterfaceFields(List<GraphQLError> errors, InterfaceTypeDefini
241234
fieldDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName,
242235
(directiveName, directive) -> new NonUniqueDirectiveError(interfaceType, fld, directiveName)));
243236

244-
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
245-
checkDeprecatedDirective(errors, directive, FIELD_DEFINITION,
246-
() -> new InvalidDeprecationDirectiveError(interfaceType, fld));
247-
248-
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
249-
(argumentName, argument) -> new NonUniqueArgumentError(interfaceType, fld, argumentName));
250-
251-
}));
237+
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive ->
238+
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
239+
(argumentName, argument) -> new NonUniqueArgumentError(interfaceType, fld, argumentName))));
252240
}
253241

254242
private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumType, List<EnumValueDefinition> enumValueDefinitions) {
@@ -265,8 +253,6 @@ private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumT
265253
});
266254

267255
enumValueDefinitions.forEach(enumValue -> enumValue.getDirectives().forEach(directive -> {
268-
checkDeprecatedDirective(errors, directive, ENUM_VALUE,
269-
() -> new InvalidDeprecationDirectiveError(enumType, enumValue));
270256

271257
BiFunction<String, Argument, NonUniqueArgumentError> errorFunction = (argumentName, argument) -> new NonUniqueArgumentError(enumType, enumValue, argumentName);
272258
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, errorFunction);
@@ -290,53 +276,9 @@ private void checkInputValues(List<GraphQLError> errors, InputObjectTypeDefiniti
290276
inputValueDefinitions.forEach(inputValueDef -> checkNamedUniqueness(errors, inputValueDef.getDirectives(), Directive::getName,
291277
(directiveName, directive) -> new NonUniqueDirectiveError(inputType, inputValueDef, directiveName)));
292278

293-
inputValueDefinitions.forEach(inputValueDef -> inputValueDef.getDirectives().forEach(directive -> {
294-
checkDeprecatedDirective(errors, directive, directiveLocation,
295-
() -> new InvalidDeprecationDirectiveError(inputType, inputValueDef));
296-
297-
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
298-
(argumentName, argument) -> new NonUniqueArgumentError(inputType, inputValueDef, argumentName));
299-
}));
300-
}
301-
302-
303-
static Set<Introspection.DirectiveLocation> DEPRECATED_ALLOWED_LOCATIONS = new LinkedHashSet<>();
304-
305-
static {
306-
DEPRECATED_ALLOWED_LOCATIONS.add(FIELD_DEFINITION);
307-
DEPRECATED_ALLOWED_LOCATIONS.add(ENUM_VALUE);
308-
}
309-
310-
/**
311-
* A special check for the magic @deprecated directive
312-
*
313-
* @param errors the list of errors
314-
* @param directive the directive to check
315-
* @param errorSupplier the error supplier function
316-
*/
317-
static void checkDeprecatedDirective(List<GraphQLError> errors, Directive directive, Introspection.DirectiveLocation actualLocation, Supplier<InvalidDeprecationDirectiveError> errorSupplier) {
318-
if ("deprecated".equals(directive.getName())) {
319-
boolean ok = false;
320-
// it can have zero args
321-
List<Argument> arguments = directive.getArguments();
322-
if (arguments.size() == 0) {
323-
ok = true;
324-
}
325-
// but if has more than it must have 1 called "reason" of type StringValue
326-
else if (arguments.size() == 1) {
327-
Argument arg = arguments.get(0);
328-
if (("reason".equals(arg.getName()) && arg.getValue() instanceof StringValue)) {
329-
ok = true;
330-
}
331-
}
332-
if (ok && !DEPRECATED_ALLOWED_LOCATIONS.contains(actualLocation)) {
333-
ok = false;
334-
}
335-
// not valid
336-
if (!ok) {
337-
errors.add(errorSupplier.get());
338-
}
339-
}
279+
inputValueDefinitions.forEach(inputValueDef -> inputValueDef.getDirectives().forEach(directive ->
280+
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
281+
(argumentName, argument) -> new NonUniqueArgumentError(inputType, inputValueDef, argumentName))));
340282
}
341283

342284
/**

src/main/java/graphql/schema/idl/SchemaTypeExtensionsChecker.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import graphql.language.TypeDefinition;
1717
import graphql.language.TypeName;
1818
import graphql.language.UnionTypeDefinition;
19-
import graphql.schema.idl.errors.InvalidDeprecationDirectiveError;
2019
import graphql.schema.idl.errors.MissingTypeError;
2120
import graphql.schema.idl.errors.NonUniqueArgumentError;
2221
import graphql.schema.idl.errors.NonUniqueDirectiveError;
@@ -33,9 +32,6 @@
3332
import java.util.function.Consumer;
3433
import java.util.stream.Collectors;
3534

36-
import static graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE;
37-
import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION;
38-
import static graphql.schema.idl.SchemaTypeChecker.checkDeprecatedDirective;
3935
import static graphql.schema.idl.SchemaTypeChecker.checkNamedUniqueness;
4036
import static graphql.util.FpKit.mergeFirst;
4137

@@ -86,14 +82,9 @@ private void checkObjectTypeExtensions(List<GraphQLError> errors, TypeDefinition
8682
extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName,
8783
(directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
8884

89-
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
90-
checkDeprecatedDirective(errors, directive, FIELD_DEFINITION,
91-
() -> new InvalidDeprecationDirectiveError(extension, fld));
92-
93-
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
94-
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
95-
96-
}));
85+
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive ->
86+
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
87+
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName))));
9788

9889
//
9990
// fields must be unique within a type extension
@@ -140,14 +131,9 @@ private void checkInterfaceTypeExtensions(List<GraphQLError> errors, TypeDefinit
140131
extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName,
141132
(directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
142133

143-
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
144-
checkDeprecatedDirective(errors, directive, FIELD_DEFINITION,
145-
() -> new InvalidDeprecationDirectiveError(extension, fld));
146-
147-
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
148-
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
149-
150-
}));
134+
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive ->
135+
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
136+
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName))));
151137

152138
//
153139
// fields must be unique within a type extension
@@ -216,11 +202,6 @@ private void checkEnumTypeExtensions(List<GraphQLError> errors, TypeDefinitionRe
216202
checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName,
217203
(namedField, enumValue) -> new NonUniqueNameError(extension, enumValue));
218204

219-
// directives
220-
enumValueDefinitions.forEach(enumValueDef -> enumValueDef.getDirectives()
221-
.forEach(directive -> checkDeprecatedDirective(errors, directive, ENUM_VALUE,
222-
() -> new InvalidDeprecationDirectiveError(extension, enumValueDef))));
223-
224205
//
225206
// enum values must be unique within a type extension
226207
forEachBut(extension, extensions,
@@ -277,14 +258,9 @@ private void checkInputObjectTypeExtensions(List<GraphQLError> errors, TypeDefin
277258
inputValueDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName,
278259
(directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
279260

280-
inputValueDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
281-
checkDeprecatedDirective(errors, directive, ENUM_VALUE,
282-
() -> new InvalidDeprecationDirectiveError(extension, fld));
283-
284-
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
285-
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
286-
287-
}));
261+
inputValueDefinitions.forEach(fld -> fld.getDirectives().forEach(directive ->
262+
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName,
263+
(argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName))));
288264
//
289265
// fields must be unique within a type extension
290266
forEachBut(extension, extensions,

src/main/java/graphql/schema/idl/errors/InvalidDeprecationDirectiveError.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/groovy/graphql/schema/idl/SchemaPrinterTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ type Query {
17611761
type PrintMeQuery {
17621762
field : PrintMeType
17631763
fieldPrintMe : SomeType
1764-
fieldPrintMeWithArgs(arg1 : String, arg2PrintMe : String @deprecated @directivePrintMe) : SomeType @someOtherDirective
1764+
fieldPrintMeWithArgs(arg1 : String, arg2PrintMe : String @directivePrintMe) : SomeType @someOtherDirective
17651765
}
17661766
17671767
type PrintMeType {

0 commit comments

Comments
 (0)