Search in sources :

Example 1 with AppliedDirectiveImpl

use of com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.

the class AddRelayConnectionsTest method testAddRelayConnections_relayConnectionOnInputTypeField.

/**
 * The <I>&#064;RelayConnection</I> directive may not be set on a field of an input type. It's possible, as the
 * <I>&#064;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"));
}
Also used : DirectiveImpl(com.graphql_java_generator.plugin.language.impl.DirectiveImpl) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Example 2 with AppliedDirectiveImpl

use of com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl 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"));
}
Also used : DirectiveImpl(com.graphql_java_generator.plugin.language.impl.DirectiveImpl) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Example 3 with AppliedDirectiveImpl

use of com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl in project graphql-maven-plugin-project by graphql-java-generator.

the class DocumentParser method readAppliedDirectives.

/**
 * Reads a GraphQL directive that has been applied to an item of the GraphQL schema. The relevant directive
 * definition should already have been read before (see {@link #readDirectiveDefinition(DirectiveDefinition)}).
 *
 * @param directives
 * @return
 */
List<AppliedDirective> readAppliedDirectives(List<graphql.language.Directive> directives) {
    List<AppliedDirective> ret = new ArrayList<>();
    if (directives != null) {
        for (graphql.language.Directive nodeDirective : directives) {
            AppliedDirectiveImpl d = new AppliedDirectiveImpl();
            d.setDirective(getDirectiveDefinition(nodeDirective.getName()));
            // Let's read its arguments
            if (nodeDirective.getArguments() != null) {
                for (Argument a : nodeDirective.getArguments()) {
                    // We store the graphql.language.Value as we receive it. We may not have parsed the relevant
                    // Object to check its field, and obviously, we can"t instanciate any object or enum yet, as we
                    // dont't even generated any code.
                    d.getArgumentValues().put(a.getName(), a.getValue());
                }
            }
            ret.add(d);
        }
    // for
    }
    return ret;
}
Also used : AppliedDirective(com.graphql_java_generator.plugin.language.AppliedDirective) Argument(graphql.language.Argument) ArrayList(java.util.ArrayList) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl)

Example 4 with AppliedDirectiveImpl

use of com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl 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"));
}
Also used : FieldTypeAST(com.graphql_java_generator.plugin.language.FieldTypeAST) DirectiveImpl(com.graphql_java_generator.plugin.language.impl.DirectiveImpl) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl) Logger(org.slf4j.Logger) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Aggregations

AppliedDirectiveImpl (com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl)4 DirectiveImpl (com.graphql_java_generator.plugin.language.impl.DirectiveImpl)3 Test (org.junit.jupiter.api.Test)3 Execution (org.junit.jupiter.api.parallel.Execution)3 FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)2 AppliedDirective (com.graphql_java_generator.plugin.language.AppliedDirective)1 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)1 Argument (graphql.language.Argument)1 ArrayList (java.util.ArrayList)1 Logger (org.slf4j.Logger)1