Skip to content

Commit

Permalink
Merge pull request #186 from vijayakinbox/master
Browse files Browse the repository at this point in the history
hacktoberfest-2023 : Validate which directives can co-exist on a field definition
  • Loading branch information
CNAChino authored Nov 1, 2023
2 parents 97493ff + f819de4 commit ec8d92f
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.intuit.graphql.orchestrator.authorization;

import com.intuit.graphql.graphQL.Directive;
import com.intuit.graphql.orchestrator.stitching.InvalidDirectivePairingException;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ValidateMultipleDirectivesCoexist {

public ValidateMultipleDirectivesCoexist() {
}

public void validate(List<Directive> directives) {
List<String> directiveNames = directives.stream()
.map(d -> d.getDefinition().getName())
.collect(Collectors.toList());

if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "external"))) {
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
}

if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "provides"))) {
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
}

if (CollectionUtils.containsAll(directiveNames, Arrays.asList("resolver", "requires"))) {
throw new InvalidDirectivePairingException(Arrays.asList("resolver", "external"));
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.intuit.graphql.orchestrator.stitching;


import java.util.List;

public class InvalidDirectivePairingException extends StitchingException {

private static final String ERR_MSG = "Field %s in container type %s with resolver directive not allowed "
+ "to have argument definitions.";

public InvalidDirectivePairingException(List<String> directiveNames) {
super(String.format(ERR_MSG, directiveNames.get(0), directiveNames.get(1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.intuit.graphql.orchestrator.authorization

import com.intuit.graphql.orchestrator.stitching.InvalidDirectivePairingException
import spock.lang.Specification;
import com.intuit.graphql.graphQL.Directive

import com.intuit.graphql.graphQL.DirectiveDefinition
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirective
import static com.intuit.graphql.orchestrator.XtextObjectCreationUtil.buildDirectiveDefinition;

class ValidateMultipleDirectiveCoexistSpec extends Specification {
private Directive resolverDirective

private Directive providesDirective

private Directive externalDirective

private Directive requiresDirective

private Directive skipDirective

private Directive includesDirective



def setup() {
DirectiveDefinition resolverDirectiveDefinition = buildDirectiveDefinition("resolver")
DirectiveDefinition externalDirectiveDefinition = buildDirectiveDefinition("external")
DirectiveDefinition requiresDirectiveDefinition = buildDirectiveDefinition("requires")
DirectiveDefinition providesDirectiveDefinition = buildDirectiveDefinition("provides")
DirectiveDefinition skipDirectiveDefinition = buildDirectiveDefinition("skip")
DirectiveDefinition includesDirectiveDefinition = buildDirectiveDefinition("include")
resolverDirective = buildDirective(resolverDirectiveDefinition, Collections.emptyList())
providesDirective = buildDirective(providesDirectiveDefinition, Collections.emptyList())
externalDirective = buildDirective(externalDirectiveDefinition, Collections.emptyList())
requiresDirective = buildDirective(requiresDirectiveDefinition, Collections.emptyList())
skipDirective = buildDirective(skipDirectiveDefinition, Collections.emptyList())
includesDirective = buildDirective(includesDirectiveDefinition, Collections.emptyList())
}

def "should throw exception for invalid directive pairing: resolver and external"() {
given:
def directives = [
resolverDirective,
externalDirective
]

when:
new ValidateMultipleDirectivesCoexist().validate(directives)

then:
thrown InvalidDirectivePairingException.class
}

def "should throw exception for invalid directive pairing: resolver and provides"() {
given:
def directives = [
resolverDirective,
providesDirective
]

when:
new ValidateMultipleDirectivesCoexist().validate(directives)

then:
thrown InvalidDirectivePairingException.class
}

def "should throw exception for invalid directive pairing: resolver and requires"() {
given:
def directives = [
resolverDirective,
requiresDirective
]

when:
new ValidateMultipleDirectivesCoexist().validate(directives)

then:
thrown InvalidDirectivePairingException.class
}

def "should not throw exception for valid directives"() {
given:
def directives = [
requiresDirective,
skipDirective,
includesDirective
]

when:
new ValidateMultipleDirectivesCoexist().validate(directives)

then:
noExceptionThrown()
}
}


0 comments on commit ec8d92f

Please sign in to comment.