Search in sources :

Example 1 with FieldImpl

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

the class AddRelayConnections method addNodeInterface.

/**
 * Adds the <I>Node</I> interfaces The <I>Node</I> is defined in the
 * <A HREF="https://relay.dev/graphql/connections.htm">Relay Connection specification</A>. If a <I>Node</I>
 * interface already exists in the schema, it is checked to be compliant to this specification. If not, an exception
 * is thrown.
 *
 * @throws RuntimeException
 *             Thrown if a <I>Node</I> interface already exists, but is not compliant with the Relay Connection
 *             specification
 */
void addNodeInterface() {
    final String NODE = "Node";
    boolean found = false;
    for (InterfaceType i : documentParser.getInterfaceTypes()) {
        if (NODE.equals(i.getName())) {
            // We've found it.
            found = true;
            // Let's check its properties
            if (i.getMemberOfUnions().size() != 0) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (member of unions)");
            }
            if (i.getFields().size() != 1) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain exactly one field)");
            }
            if (!"id".equals(i.getFields().get(0).getName())) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain only the 'id' field)");
            }
            if (!"ID".equals(i.getFields().get(0).getGraphQLTypeSimpleName())) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain only the 'id' field, of the 'ID' type)");
            }
            if (!i.getFields().get(0).isId()) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain only the 'id' field, that is an identifier)");
            }
            if (i.getFields().get(0).getFieldTypeAST().getListDepth() > 0) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain only the 'id' field, that is not a list)");
            }
            if (!i.getFields().get(0).getFieldTypeAST().isMandatory()) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should contain only the 'id' field, that is mandatory)");
            }
            if (i.getRequestType() != null) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should not be a query/mutation/subscription)");
            }
            if (i.isInputType()) {
                throw new RuntimeException("The " + NODE + " interface already exists, but is not compliant with the Relay specification (it should not be an input type)");
            }
            // Ok, this interface is compliant. We're done.
            node = i;
            break;
        }
    }
    if (!found) {
        // The interface Node has not been found. But this name can (and may not) be used for another type.
        if (documentParser.getType(NODE, false) != null) {
            throw new RuntimeException("A " + NODE + " type already exists. This prevents to create the " + NODE + " interface, as described in this article: https://dev.to/mikemarcacci/intermediate-interfaces-generic-utility-types-in-graphql-50e8");
        }
        // We're in the standard case: the interface doesn't exist in the given schema(s). Let's define it.
        node = new InterfaceType(NODE, configuration, documentParser);
        // Adding the id field toe the Node interface
        FieldImpl f = FieldImpl.builder().name("id").owningType(node).documentParser(documentParser).fieldTypeAST(// 
        FieldTypeAST.builder().graphQLTypeSimpleName("ID").mandatory(true).build()).build();
        node.getFields().add(f);
        documentParser.getInterfaceTypes().add(node);
        documentParser.getTypes().put(NODE, node);
    }
}
Also used : InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Example 2 with FieldImpl

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

the class AddRelayConnections method addEdgeInterface.

/**
 * Adds the <I>Edge</I> interface. The <I>Edge</I> is defined in the
 * <A HREF="https://dev.to/mikemarcacci/intermediate-interfaces-generic-utility-types-in-graphql-50e8">generic
 * utility types blog entry</A>, that leads to allow that an interface implements an interface in the GraphQL
 * specification.If an <I>Edge</I> interface already exists in the schema, it is checked to be compliant to this
 * specification. If not, an exception is thrown.
 *
 * @throws RuntimeException
 *             Thrown if a <I>Edge</I> interface already exists, but is not compliant with the specification
 */
void addEdgeInterface() {
    final String EDGE = "Edge";
    boolean found = false;
    for (InterfaceType i : documentParser.getInterfaceTypes()) {
        if (EDGE.equals(i.getName())) {
            // We've found it.
            found = true;
            // Let's check its properties
            if (i.getMemberOfUnions().size() != 0) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (member of unions)");
            }
            if (i.getRequestType() != null) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (it should not be a query/mutation/subscription)");
            }
            if (i.isInputType()) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (it should not be an input type)");
            }
            if (i.getFields().size() != 2) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (it should contain exactly two fields)");
            }
            // ///////////////////
            // The cursor field
            int j = 0;
            if (!"cursor".equals(i.getFields().get(j).getName())) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the first field should be the 'cursor' field)");
            }
            if (!"String".equals(i.getFields().get(j).getGraphQLTypeSimpleName())) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'cursor' field should be a String field)");
            }
            if (i.getFields().get(j).isId()) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'cursor' field should not be an identifier)");
            }
            if (i.getFields().get(j).getFieldTypeAST().getListDepth() > 0) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'cursor' field should not be a list)");
            }
            if (!i.getFields().get(j).getFieldTypeAST().isMandatory()) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'cursor' field should be mandatory)");
            }
            // ///////////////////
            // The node field
            j += 1;
            if (!"node".equals(i.getFields().get(j).getName())) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the second field should be the 'node' field)");
            }
            if (!"Node".equals(i.getFields().get(j).getGraphQLTypeSimpleName())) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'node' field should be of type [Edge])");
            }
            if (i.getFields().get(j).isId()) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'node' field should not be an identifier)");
            }
            if (i.getFields().get(j).getFieldTypeAST().getListDepth() > 0) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'node' field should not be a list)");
            }
            if (i.getFields().get(j).getFieldTypeAST().isMandatory()) {
                throw new RuntimeException("The " + EDGE + " interface already exists, but is not compliant with the Relay specification (the 'node' field should not be mandatory)");
            }
            // Ok, this interface is compliant. We're done.
            break;
        }
    }
    if (!found) {
        // The interface Edge has not been found. But this name can (and may not) be used for another type.
        if (documentParser.getType(EDGE, false) != null) {
            throw new RuntimeException("A " + EDGE + " type already exists. This prevents to create the " + EDGE + " interface, as described in this article: https://dev.to/mikemarcacci/intermediate-interfaces-generic-utility-types-in-graphql-50e8");
        }
        // We're in the standard case: the interface doesn't exist in the given schema(s). Let's define it.
        InterfaceType i = new InterfaceType(EDGE, configuration, documentParser);
        // Adding the id field toe the Node interface
        FieldImpl cursor = FieldImpl.builder().name("cursor").owningType(i).documentParser(documentParser).fieldTypeAST(// 
        FieldTypeAST.builder().graphQLTypeSimpleName("String").mandatory(true).build()).build();
        FieldImpl node = FieldImpl.builder().name("node").owningType(i).documentParser(documentParser).fieldTypeAST(// 
        FieldTypeAST.builder().graphQLTypeSimpleName("Node").build()).build();
        i.getFields().add(cursor);
        i.getFields().add(node);
        documentParser.getInterfaceTypes().add(i);
        documentParser.getTypes().put(EDGE, i);
    }
}
Also used : InterfaceType(com.graphql_java_generator.plugin.language.impl.InterfaceType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Example 3 with FieldImpl

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

the class DocumentParser method readField.

/**
 * Reads one GraphQL {@link FieldDefinition}, and maps it into a {@link Field}.
 *
 * @param fieldDef
 * @param owningType
 *            The type which contains this field
 * @return
 * @throws MojographqlUtils.executionException
 */
Field readField(FieldDefinition fieldDef, Type owningType) {
    FieldImpl field = readFieldTypeDefinition(fieldDef);
    field.setOwningType(owningType);
    // Let's read all its input parameters
    field.setInputParameters(fieldDef.getInputValueDefinitions().stream().map(this::readFieldTypeDefinition).collect(Collectors.toList()));
    // Let's store its comments
    field.setComments(fieldDef.getComments());
    return field;
}
Also used : FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Example 4 with FieldImpl

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

the class GenerateCodeDocumentParser method get__TypeField.

/**
 * @param o
 * @return
 */
private FieldImpl get__TypeField(Type o) {
    FieldImpl __type = FieldImpl.builder().documentParser(this).name("__type").fieldTypeAST(// 
    FieldTypeAST.builder().graphQLTypeSimpleName("__Type").mandatory(true).build()).owningType(o).build();
    __type.getInputParameters().add(FieldImpl.builder().documentParser(this).name("name").fieldTypeAST(// 
    FieldTypeAST.builder().graphQLTypeSimpleName("String").mandatory(true).build()).owningType(o).build());
    return __type;
}
Also used : FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Example 5 with FieldImpl

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

the class GenerateCodeDocumentParser method initRelations.

/**
 * Reads all the GraphQl objects, interfaces, union... that have been read from the GraphQL schema, and list all the
 * relations between Server objects (that is: all objects out of the Query/Mutation/Subscription types and the input
 * types). The found relations are stored, to be reused during the code generation.<BR/>
 * These relations are important for the server mode of the plugin, to generate the proper JPA annotations.
 */
void initRelations() {
    for (ObjectType type : getObjectTypes()) {
        // We initiate the relations only for regular objects (not query/mutation/subscription)
        if (type.getRequestType() == null) {
            if (!type.isInputType()) {
                for (Field field : type.getFields()) {
                    if (field.getType() instanceof ObjectType) {
                        RelationType relType = field.getFieldTypeAST().getListDepth() > 0 ? RelationType.OneToMany : RelationType.ManyToOne;
                        RelationImpl relation = new RelationImpl(type, field, relType);
                        // 
                        ((FieldImpl) field).setRelation(relation);
                        relations.add(relation);
                    }
                // if (instanceof ObjectType)
                }
            // if (!type.isInputType())
            }
        // for (field)
        }
    // if (type.getRequestType()== null)
    }
// for (type)
}
Also used : RelationImpl(com.graphql_java_generator.plugin.language.impl.RelationImpl) ObjectType(com.graphql_java_generator.plugin.language.impl.ObjectType) GraphQLObjectType(com.graphql_java_generator.annotation.GraphQLObjectType) Field(com.graphql_java_generator.plugin.language.Field) GraphQLField(com.graphql_java_generator.GraphQLField) RelationType(com.graphql_java_generator.plugin.language.RelationType) FieldImpl(com.graphql_java_generator.plugin.language.impl.FieldImpl)

Aggregations

FieldImpl (com.graphql_java_generator.plugin.language.impl.FieldImpl)15 InterfaceType (com.graphql_java_generator.plugin.language.impl.InterfaceType)7 ObjectType (com.graphql_java_generator.plugin.language.impl.ObjectType)7 FieldTypeAST (com.graphql_java_generator.plugin.language.FieldTypeAST)6 Field (com.graphql_java_generator.plugin.language.Field)5 Type (com.graphql_java_generator.plugin.language.Type)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 AppliedDirectiveImpl (com.graphql_java_generator.plugin.language.impl.AppliedDirectiveImpl)2 DirectiveImpl (com.graphql_java_generator.plugin.language.impl.DirectiveImpl)2 InputValueDefinition (graphql.language.InputValueDefinition)2 Execution (org.junit.jupiter.api.parallel.Execution)2 GraphQLField (com.graphql_java_generator.GraphQLField)1 GraphQLObjectType (com.graphql_java_generator.annotation.GraphQLObjectType)1 GenerateCodeCommonConfiguration (com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration)1 AppliedDirective (com.graphql_java_generator.plugin.language.AppliedDirective)1 RelationType (com.graphql_java_generator.plugin.language.RelationType)1 EnumType (com.graphql_java_generator.plugin.language.impl.EnumType)1 RelationImpl (com.graphql_java_generator.plugin.language.impl.RelationImpl)1 ScalarType (com.graphql_java_generator.plugin.language.impl.ScalarType)1