Search in sources :

Example 1 with Directive

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

the class DocumentParser method parseOneDocument.

/**
 * Generates the target classes for the given GraphQL schema definition
 *
 * @param document
 */
public void parseOneDocument(Document document) {
    // List of all the names of the query types. There should be only one. But we're ready for more (for instance if
    // several schema files have been merged)
    List<String> queryObjectNames = new ArrayList<>();
    // List of all the names of the mutation types. There should be only one. But we're ready for more (for instance
    // if several schema files have been merged)
    List<String> mutationObjectNames = new ArrayList<>();
    // List of all the names of the subscription types. There should be only one. But we're ready for more (for
    // instance if several schema files have been merged)
    List<String> subscriptionObjectNames = new ArrayList<>();
    // The Directives must be read first, as they may be found on almost any kind of definition in the GraphQL
    // schema
    document.getDefinitions().stream().filter(n -> (n instanceof DirectiveDefinition)).forEach(node -> directives.add(readDirectiveDefinition((DirectiveDefinition) node)));
    // Looks for a schema definitions, to list the defined queries, mutations and subscriptions (should be only one
    // of each), but we're ready for more. (for instance if several schema files have been merged)
    logger.debug("Looking for schema definition");
    for (Definition<?> node : document.getDefinitions()) {
        if (node instanceof SchemaDefinition) {
            readSchemaDefinition((SchemaDefinition) node, queryObjectNames, mutationObjectNames, subscriptionObjectNames);
        }
    // if
    }
    // for
    logger.debug("Reading node definitions");
    for (Definition<?> node : document.getDefinitions()) {
        // directive
        if (node instanceof DirectiveDefinition) {
        // Directives are read latter
        } else // enum
        if (node instanceof EnumTypeDefinition) {
            enumTypes.add(readEnumType((EnumTypeDefinition) node));
        } else // input object
        if (node instanceof InputObjectTypeDefinition) {
            objectTypes.add(readInputObjectType((InputObjectTypeDefinition) node));
        } else // interface
        if (node instanceof InterfaceTypeDefinition) {
            interfaceTypes.add(readInterfaceType((InterfaceTypeDefinition) node));
        } else // extend object
        if (node instanceof ObjectTypeExtensionDefinition) {
            // ObjectTypeExtensionDefinition is a subclass of ObjectTypeDefinition, so we need to check it first.
            // 
            // No action here: we'll manage all the object extensions once all object definitions have been read
            objectTypeExtensionDefinitions.add((ObjectTypeExtensionDefinition) node);
        } else // object
        if (node instanceof ObjectTypeDefinition) {
            // Let's check what kind of ObjectDefinition we have
            String name = ((ObjectTypeDefinition) node).getName();
            if (queryObjectNames.contains(name) || DEFAULT_QUERY_NAME.equals(name)) {
                // We first read the object type, that'll go to the main package
                ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
                o.setRequestType("query");
                objectTypes.add(o);
                // Then we read the query, that'll go in the util subpackage: its imports are different
                if (queryType != null) {
                    throw new RuntimeException("Error while reading the query '" + ((ObjectTypeDefinition) node).getName() + "'. A Query root operation has already been read, with name'" + queryType.getName() + "'");
                }
                queryType = o;
            } else if (mutationObjectNames.contains(name) || DEFAULT_MUTATION_NAME.equals(name)) {
                // We first read the object type, that'll go to the main package
                ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
                o.setRequestType("mutation");
                objectTypes.add(o);
                // Then we read the mutation, that'll go in the util subpackage: its imports are different
                if (mutationType != null) {
                    throw new RuntimeException("Error while reading the mutation '" + ((ObjectTypeDefinition) node).getName() + "'. A Mutation root operation has already been read, with name'" + mutationType.getName() + "'");
                }
                mutationType = o;
            } else if (subscriptionObjectNames.contains(name) || DEFAULT_SUBSCRIPTION_NAME.equals(name)) {
                // We first read the object type, that'll go to the main package
                ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
                o.setRequestType("subscription");
                objectTypes.add(o);
                // Then we read the subscription, that'll go in the util subpackage: its imports are different
                if (subscriptionType != null) {
                    throw new RuntimeException("Error while reading the subscription '" + ((ObjectTypeDefinition) node).getName() + "'. A Subscription root operation has already been read, with name'" + subscriptionType.getName() + "'");
                }
                subscriptionType = o;
            } else {
                objectTypes.add(readObjectTypeDefinition((ObjectTypeDefinition) node));
            }
        } else // scalar extension
        if (node instanceof ScalarTypeExtensionDefinition) {
            readScalarExtensionType((ScalarTypeExtensionDefinition) node);
        } else // scalar
        if (node instanceof ScalarTypeDefinition) {
            // Custom scalars implementation must be provided by the configuration. We just check that it's OK.
            readCustomScalarType((ScalarTypeDefinition) node);
        } else // schema
        if (node instanceof SchemaDefinition) {
        // No action, we already parsed it
        } else // union
        if (node instanceof UnionTypeDefinition) {
        // Unions are read latter, once all GraphQL types have been parsed
        } else {
            logger.warn("Non managed node type: " + node.getClass().getName());
        }
    }
    // for
    // Once all Types have been properly read, we can read the union types
    logger.debug("Reading union definitions");
    document.getDefinitions().stream().filter(n -> (n instanceof UnionTypeDefinition)).forEach(n -> unionTypes.add(readUnionType((UnionTypeDefinition) n)));
}
Also used : UnionType(com.graphql_java_generator.plugin.language.impl.UnionType) EnumValueImpl(com.graphql_java_generator.plugin.language.impl.EnumValueImpl) Directive(com.graphql_java_generator.plugin.language.Directive) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CommonConfiguration(com.graphql_java_generator.plugin.conf.CommonConfiguration) InputValueDefinition(graphql.language.InputValueDefinition) DirectiveDefinition(graphql.language.DirectiveDefinition) SchemaDefinition(graphql.language.SchemaDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) AbstractNode(graphql.language.AbstractNode) FieldTypeAST(com.graphql_java_generator.plugin.language.FieldTypeAST) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) Map(java.util.Map) TypeName(graphql.language.TypeName) EnumValue(com.graphql_java_generator.plugin.language.EnumValue) OperationTypeDefinition(graphql.language.OperationTypeDefinition) DirectiveImpl(com.graphql_java_generator.plugin.language.impl.DirectiveImpl) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) FieldDefinition(graphql.language.FieldDefinition) Field(com.graphql_java_generator.plugin.language.Field) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) GraphQLConfiguration(com.graphql_java_generator.plugin.conf.GraphQLConfiguration) DirectiveLocation(com.graphql_java_generator.plugin.language.DirectiveLocation) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) PostConstruct(javax.annotation.PostConstruct) NonNullType(graphql.language.NonNullType) ListType(graphql.language.ListType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl) ParserOptions(graphql.parser.ParserOptions) Setter(lombok.Setter) AppliedDirective(com.graphql_java_generator.plugin.language.AppliedDirective) ScalarExtensionType(com.graphql_java_generator.plugin.language.impl.ScalarExtensionType) Getter(lombok.Getter) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) HashMap(java.util.HashMap) Type(com.graphql_java_generator.plugin.language.Type) ScalarType(com.graphql_java_generator.plugin.language.impl.ScalarType) ArrayList(java.util.ArrayList) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) UnionTypeDefinition(graphql.language.UnionTypeDefinition) Parser(graphql.parser.Parser) Definition(graphql.language.Definition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumType(com.graphql_java_generator.plugin.language.impl.EnumType) Logger(org.slf4j.Logger) GraphqlUtils(com.graphql_java_generator.util.GraphqlUtils) AppliedDirectiveImpl(com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl) GenerateCodeCommonConfiguration(com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration) IOException(java.io.IOException) Argument(graphql.language.Argument) Document(graphql.language.Document) StringValue(graphql.language.StringValue) CustomScalarType(com.graphql_java_generator.plugin.language.impl.CustomScalarType) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaDefinition(graphql.language.SchemaDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) ArrayList(java.util.ArrayList) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) DirectiveDefinition(graphql.language.DirectiveDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition)

Example 2 with Directive

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

the class AddRelayConnectionsTest method checkRelayConnectionDirective.

private void checkRelayConnectionDirective() {
    final String RELAY_CONNECTION = "RelayConnection";
    boolean found = false;
    for (Directive d : documentParser.getDirectives()) {
        if (RELAY_CONNECTION.equals(d.getName())) {
            // The directive should exist only once, so we may not already have found it.
            if (found) {
                fail("There are two directives with '" + RELAY_CONNECTION + "' as a name");
            }
            // We've found it.
            found = true;
            // Let's check its properties
            assertEquals(0, d.getArguments().size(), "No arguments");
            assertEquals(1, d.getDirectiveLocations().size(), "Directive has one location");
            assertEquals(DirectiveLocation.FIELD_DEFINITION, d.getDirectiveLocations().get(0), "Directive locations may only be: FIELD_DEFINITION");
        }
    }
    if (!found) {
        fail("The directive " + RELAY_CONNECTION + " has not been found");
    }
}
Also used : Directive(com.graphql_java_generator.plugin.language.Directive)

Example 3 with Directive

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

the class AddRelayConnectionsTest method testAddRelayConnections_schemaWithWrongRelayConnectionDirective.

@Test
@Execution(ExecutionMode.CONCURRENT)
void testAddRelayConnections_schemaWithWrongRelayConnectionDirective() throws IOException {
    // Preparation
    loadSpringContext(AllGraphQLCases_Client_SpringConfiguration.class, "testAddRelayConnections_schemaWithWrongRelayConnectionDirective", true);
    // 
    Directive relayConnection = documentParser.getDirectiveDefinition("RelayConnection");
    // Let's remove the only item in the list
    relayConnection.getDirectiveLocations().remove(0);
    // Wrong location!
    relayConnection.getDirectiveLocations().add(DirectiveLocation.ENUM);
    documentParser.getDirectives().add(relayConnection);
    // Verification
    AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> checkRelayConnectionDirective());
    assertTrue(e.getMessage().contains("ENUM"));
}
Also used : AssertionFailedError(org.opentest4j.AssertionFailedError) Directive(com.graphql_java_generator.plugin.language.Directive) Execution(org.junit.jupiter.api.parallel.Execution) Test(org.junit.jupiter.api.Test)

Aggregations

Directive (com.graphql_java_generator.plugin.language.Directive)3 CommonConfiguration (com.graphql_java_generator.plugin.conf.CommonConfiguration)1 GenerateCodeCommonConfiguration (com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration)1 GraphQLConfiguration (com.graphql_java_generator.plugin.conf.GraphQLConfiguration)1 AppliedDirective (com.graphql_java_generator.plugin.language.AppliedDirective)1 DirectiveLocation (com.graphql_java_generator.plugin.language.DirectiveLocation)1 EnumValue (com.graphql_java_generator.plugin.language.EnumValue)1 Field (com.graphql_java_generator.plugin.language.Field)1 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)1 Type (com.graphql_java_generator.plugin.language.Type)1 AppliedDirectiveImpl (com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl)1 CustomScalarType (com.graphql_java_generator.plugin.language.impl.CustomScalarType)1 DirectiveImpl (com.graphql_java_generator.plugin.language.impl.DirectiveImpl)1 EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)1 EnumValueImpl (com.graphql_java_generator.plugin.language.impl.EnumValueImpl)1 FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)1 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)1 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)1 ScalarExtensionType (com.graphql_java_generator.plugin.language.impl.ScalarExtensionType)1 ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)1