use of com.graphql_java_generator.plugin.language.impl.DirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.
the class DocumentParser method postConstruct.
@PostConstruct
public void postConstruct() {
logger.debug("Starting DocumentParser's PostConstrut intialization");
// ////////////////////////////////////////////////////////////////////////////////////////
// Add of all GraphQL scalars: standard and customs depending on the use case
initScalarTypes(UUID.class);
// ////////////////////////////////////////////////////////////////////////////////////////
// Add of all GraphQL standard directives
//
// @skip
DirectiveImpl skip = new DirectiveImpl();
skip.setName("skip");
skip.getArguments().add(FieldImpl.builder().name("if").fieldTypeAST(//
FieldTypeAST.builder().graphQLTypeSimpleName("Boolean").mandatory(true).build()).build());
skip.getDirectiveLocations().add(DirectiveLocation.FIELD);
skip.getDirectiveLocations().add(DirectiveLocation.FRAGMENT_SPREAD);
skip.getDirectiveLocations().add(DirectiveLocation.INLINE_FRAGMENT);
skip.setStandard(true);
directives.add(skip);
//
// @include
DirectiveImpl include = new DirectiveImpl();
include.setName("include");
include.getArguments().add(FieldImpl.builder().name("if").fieldTypeAST(//
FieldTypeAST.builder().graphQLTypeSimpleName("Boolean").mandatory(true).build()).build());
include.getDirectiveLocations().add(DirectiveLocation.FIELD);
include.getDirectiveLocations().add(DirectiveLocation.FRAGMENT_SPREAD);
include.getDirectiveLocations().add(DirectiveLocation.INLINE_FRAGMENT);
include.setStandard(true);
directives.add(include);
//
// @defer
DirectiveImpl defer = new DirectiveImpl();
defer.setName("defer");
defer.getArguments().add(FieldImpl.builder().name("if").fieldTypeAST(//
FieldTypeAST.builder().graphQLTypeSimpleName("Boolean").mandatory(true).build()).build());
defer.getDirectiveLocations().add(DirectiveLocation.FIELD);
defer.setStandard(true);
directives.add(defer);
//
// @deprecated
DirectiveImpl deprecated = new DirectiveImpl();
deprecated.setName("deprecated");
deprecated.getArguments().add(FieldImpl.builder().name("reason").fieldTypeAST(FieldTypeAST.builder().graphQLTypeSimpleName("String").build()).defaultValue(new StringValue("No longer supported")).build());
deprecated.getDirectiveLocations().add(DirectiveLocation.FIELD_DEFINITION);
deprecated.getDirectiveLocations().add(DirectiveLocation.ENUM_VALUE);
deprecated.setStandard(true);
directives.add(deprecated);
logger.debug("Finished DocumentParser's PostConstrut intialization");
}
use of com.graphql_java_generator.plugin.language.impl.DirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method testAddRelayConnections_relayConnectionOnInputTypeField.
/**
* The <I>@RelayConnection</I> directive may not be set on a field of an input type. It's possible, as the
* <I>@RelayConnection</I> directive is defined in the input schema, so it can be badly defined
*
* @throws IOException
*/
@Test
@Execution(ExecutionMode.CONCURRENT)
void testAddRelayConnections_relayConnectionOnInputTypeField() throws IOException {
// Preparation
loadSpringContext(AllGraphQLCases_Client_SpringConfiguration.class, "testAddRelayConnections_relayConnectionOnInputTypeField", false);
// Let's parse (load) the GraphQL schemas, but not call the addRelayConnections() method, so that we can break
// the Connection interface compliance for the relay connection specification
((GenerateGraphQLSchemaConfigurationTestHelper) configuration).addRelayConnections = false;
documentParser.parseDocuments();
//
DirectiveImpl dir = new DirectiveImpl();
dir.setName("RelayConnection");
dir.getDirectiveLocations().add(DirectiveLocation.FIELD_DEFINITION);
//
AppliedDirectiveImpl d = new AppliedDirectiveImpl();
d.setDirective(dir);
//
getField("AllFieldCasesInput", "aliases").getAppliedDirectives().add(d);
// Go, go, go
RuntimeException e = assertThrows(RuntimeException.class, () -> addRelayConnections.addRelayConnections());
// Verification
assertTrue(e.getMessage().contains("input type may not have fields to which the @RelayConnection directive is applied"));
}
use of com.graphql_java_generator.plugin.language.impl.DirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method test_addEdgeConnectionAndApplyNodeInterface_step1RelayConnectionOnInputField.
@Test
@Execution(ExecutionMode.CONCURRENT)
void test_addEdgeConnectionAndApplyNodeInterface_step1RelayConnectionOnInputField() throws IOException {
// If a type's field is annotated by @RelayConnection, but this field is not a list, then an error should be
// thrown.
// Let's change the list attribute to false to one of this field
loadSpringContext(AllGraphQLCases_Client_SpringConfiguration.class, "test_addEdgeConnectionAndApplyNodeInterface_step2missingDirectiveOnInterfaceField", false);
// Let's parse (load) the GraphQL schemas, but not call the addRelayConnections() method, so that we can break
// the Node interface compliance for the relay connection specification
((GenerateGraphQLSchemaConfigurationTestHelper) configuration).addRelayConnections = false;
documentParser.parseDocuments();
//
FieldImpl f = (FieldImpl) getField("AllFieldCasesInput", "booleans");
//
DirectiveImpl dir = new DirectiveImpl();
dir.setName("RelayConnection");
dir.getDirectiveLocations().add(DirectiveLocation.FIELD_DEFINITION);
//
AppliedDirectiveImpl d = new AppliedDirectiveImpl();
d.setDirective(dir);
f.getAppliedDirectives().add(d);
// Go, go, go
RuntimeException e = assertThrows(RuntimeException.class, () -> addRelayConnections.addEdgeConnectionAndApplyNodeInterface());
// Verification
assertTrue(e.getMessage().contains("AllFieldCasesInput.booleans"));
assertTrue(e.getMessage().contains("input type may not have fields to which the @RelayConnection directive is applied"));
}
use of com.graphql_java_generator.plugin.language.impl.DirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.
the class DocumentParser method readDirectiveDefinition.
/**
* Reads a directive definition, and stores its informations into the {@link DirectiveImpl} for further processing
*
* @param node
* @return
*/
public Directive readDirectiveDefinition(DirectiveDefinition node) {
DirectiveImpl directive = new DirectiveImpl();
directive.setName(node.getName());
// Let's read all its input parameters
directive.setArguments(node.getInputValueDefinitions().stream().map(this::readFieldTypeDefinition).collect(Collectors.toList()));
// Let's store its comments
directive.setComments(node.getComments());
// and all its locations
for (graphql.language.DirectiveLocation dl : node.getDirectiveLocations()) {
DirectiveLocation dirLoc = DirectiveLocation.valueOf(DirectiveLocation.class, dl.getName());
directive.getDirectiveLocations().add(dirLoc);
}
return directive;
}
use of com.graphql_java_generator.plugin.language.impl.DirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method test_addEdgeConnectionAndApplyNodeInterface_step2missingDirectiveOnInterfaceField.
@Test
@Execution(ExecutionMode.CONCURRENT)
void test_addEdgeConnectionAndApplyNodeInterface_step2missingDirectiveOnInterfaceField() throws IOException {
// If a type's field is annotated by @RelayConnection, but this field is "inherited" from an interface, in which
// is not inherited by this directive, then an error should be thrown.
// Let's add the @RelayConnection directive to the AllFieldCasesInterfaceType.id field, and check that two
// errors are found (as id is in the AllFieldCasesInterface and the WithID interfaces)
loadSpringContext(AllGraphQLCases_Client_SpringConfiguration.class, "test_addEdgeConnectionAndApplyNodeInterface_step2missingDirectiveOnInterfaceField", false);
// Let's parse (load) the GraphQL schemas, but not call the addRelayConnections() method, so that we can break
// the Node interface compliance for the relay connection specification
((GenerateGraphQLSchemaConfigurationTestHelper) configuration).addRelayConnections = false;
documentParser.parseDocuments();
DirectiveImpl dir = new DirectiveImpl();
dir.setName("RelayConnection");
dir.getDirectiveLocations().add(DirectiveLocation.FIELD_DEFINITION);
//
FieldImpl f = (FieldImpl) getField("AllFieldCasesInterfaceType", "id");
// We need to pass the "is a list" test
FieldTypeAST list = FieldTypeAST.builder().listDepth(1).listItemFieldTypeAST(f.getFieldTypeAST()).build();
f.setFieldTypeAST(list);
//
AppliedDirectiveImpl d = new AppliedDirectiveImpl();
d.setDirective(dir);
f.setAppliedDirectives(new ArrayList<>());
f.getAppliedDirectives().add(d);
Logger mockLogger = mock(Logger.class);
AddRelayConnections.logger = mockLogger;
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
// Go, go, go
RuntimeException e = assertThrows(RuntimeException.class, () -> addRelayConnections.addEdgeConnectionAndApplyNodeInterface());
// Verification
assertTrue(e.getMessage().contains("2 error(s) was(were) found"), "Expected '2 error(s) was(were) found' in " + e.getMessage());
// First error
verify(mockLogger, times(2)).error(argument.capture());
String errorMessage = argument.getAllValues().get(0);
assertTrue(errorMessage.contains(" AllFieldCasesInterfaceType "));
assertTrue(errorMessage.contains(" id "));
assertTrue(errorMessage.contains("interface AllFieldCasesInterface, in which this field doesn't have the directive @RelayConnection applied"));
// Second error
errorMessage = argument.getAllValues().get(1);
assertTrue(errorMessage.contains(" AllFieldCasesInterfaceType "));
assertTrue(errorMessage.contains(" id "));
assertTrue(errorMessage.contains("interface WithID, in which this field doesn't have the directive @RelayConnection applied"));
}
Aggregations