Skip to content

Commit ec8d92f

Browse files
authored
Merge pull request #186 from vijayakinbox/master
hacktoberfest-2023 : Validate which directives can co-exist on a field definition
2 parents 97493ff + f819de4 commit ec8d92f

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.intuit.graphql.orchestrator.authorization;
2+
3+
import com.intuit.graphql.graphQL.Directive;
4+
import com.intuit.graphql.orchestrator.stitching.InvalidDirectivePairingException;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
public class ValidateMultipleDirectivesCoexist {
12+
13+
public ValidateMultipleDirectivesCoexist() {
14+
}
15+
16+
public void validate(List<Directive> directives) {
17+
List<String> directiveNames = directives.stream()
18+
.map(d -> d.getDefinition().getName())
19+
.collect(Collectors.toList());
20+
21+
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "external"))) {
22+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
23+
}
24+
25+
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "provides"))) {
26+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
27+
}
28+
29+
if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "requires"))) {
30+
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
31+
}
32+
33+
}
34+
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.intuit.graphql.orchestrator.stitching;
2+
3+
4+
import java.util.List;
5+
6+
public class InvalidDirectivePairingException extends StitchingException {
7+
8+
private static final String ERR_MSG = "Field %s in container type %s with resolver directive not allowed "
9+
+ "to have argument definitions.";
10+
11+
public InvalidDirectivePairingException(List<String> directiveNames) {
12+
super(String.format(ERR_MSG, directiveNames.get(0), directiveNames.get(1)));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.intuit.graphql.orchestrator.authorization
2+
3+
import com.intuit.graphql.orchestrator.stitching.InvalidDirectivePairingException
4+
import spock.lang.Specification;
5+
import com.intuit.graphql.graphQL.Directive
6+
7+
import com.intuit.graphql.graphQL.DirectiveDefinition
8+
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirective
9+
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirectiveDefinition;
10+
11+
class ValidateMultipleDirectiveCoexistSpec extends Specification {
12+
private Directive resolverDirective
13+
14+
private Directive providesDirective
15+
16+
private Directive externalDirective
17+
18+
private Directive requiresDirective
19+
20+
private Directive skipDirective
21+
22+
private Directive includesDirective
23+
24+
25+
26+
def setup() {
27+
DirectiveDefinition resolverDirectiveDefinition = buildDirectiveDefinition("resolver")
28+
DirectiveDefinition externalDirectiveDefinition = buildDirectiveDefinition("external")
29+
DirectiveDefinition requiresDirectiveDefinition = buildDirectiveDefinition("requires")
30+
DirectiveDefinition providesDirectiveDefinition = buildDirectiveDefinition("provides")
31+
DirectiveDefinition skipDirectiveDefinition = buildDirectiveDefinition("skip")
32+
DirectiveDefinition includesDirectiveDefinition = buildDirectiveDefinition("include")
33+
resolverDirective = buildDirective(resolverDirectiveDefinition, Collections.emptyList())
34+
providesDirective = buildDirective(providesDirectiveDefinition, Collections.emptyList())
35+
externalDirective = buildDirective(externalDirectiveDefinition, Collections.emptyList())
36+
requiresDirective = buildDirective(requiresDirectiveDefinition, Collections.emptyList())
37+
skipDirective = buildDirective(skipDirectiveDefinition, Collections.emptyList())
38+
includesDirective = buildDirective(includesDirectiveDefinition, Collections.emptyList())
39+
}
40+
41+
def "should throw exception for invalid directive pairing: resolver and external"() {
42+
given:
43+
def directives = [
44+
resolverDirective,
45+
externalDirective
46+
]
47+
48+
when:
49+
new ValidateMultipleDirectivesCoexist().validate(directives)
50+
51+
then:
52+
thrown InvalidDirectivePairingException.class
53+
}
54+
55+
def "should throw exception for invalid directive pairing: resolver and provides"() {
56+
given:
57+
def directives = [
58+
resolverDirective,
59+
providesDirective
60+
]
61+
62+
when:
63+
new ValidateMultipleDirectivesCoexist().validate(directives)
64+
65+
then:
66+
thrown InvalidDirectivePairingException.class
67+
}
68+
69+
def "should throw exception for invalid directive pairing: resolver and requires"() {
70+
given:
71+
def directives = [
72+
resolverDirective,
73+
requiresDirective
74+
]
75+
76+
when:
77+
new ValidateMultipleDirectivesCoexist().validate(directives)
78+
79+
then:
80+
thrown InvalidDirectivePairingException.class
81+
}
82+
83+
def "should not throw exception for valid directives"() {
84+
given:
85+
def directives = [
86+
requiresDirective,
87+
skipDirective,
88+
includesDirective
89+
]
90+
91+
when:
92+
new ValidateMultipleDirectivesCoexist().validate(directives)
93+
94+
then:
95+
noExceptionThrown()
96+
}
97+
}
98+
99+

0 commit comments

Comments
 (0)