All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition Maven / Gradle / Ivy

The newest version!
package com.kobylynskyi.graphql.codegen.model.definitions;

import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.AbstractDescribedNode;
import graphql.language.Comment;
import graphql.language.Description;
import graphql.language.Directive;
import graphql.language.DirectivesContainer;
import graphql.language.NamedNode;
import graphql.language.Node;
import graphql.language.SourceLocation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * Base class for all GraphQL definition types that contains base definition and its extensions
 *
 * @param  base type
 * @param  extension type
 */
public abstract class ExtendedDefinition, E extends T> {

    /**
     * Nullable because some schemas can have just "extends"
     */
    protected T definition;
    protected List extensions = new ArrayList<>();

    public String getName() {
        if (definition != null) {
            return definition.getName();
        } else {
            return extensions.stream().map(NamedNode::getName).findFirst().orElse(null);
        }
    }

    public SourceLocation getSourceLocation() {
        if (definition != null) {
            return definition.getSourceLocation();
        } else {
            return extensions.stream().map(Node::getSourceLocation).findFirst().orElse(null);
        }
    }

    public List getJavaDoc() {
        List javaDocFromDescription = getJavaDocFromDescription();
        if (javaDocFromDescription.isEmpty()) {
            return getJavaDocFromComments();
        }
        return javaDocFromDescription;
    }

    public List getJavaDocFromDescription() {
        List descriptions = new ArrayList<>();
        if (this.definition instanceof AbstractDescribedNode) {
            Description description = ((AbstractDescribedNode) this.definition).getDescription();
            if (description != null && Utils.isNotBlank(description.getContent())) {
                descriptions.add(description.getContent().trim());
            }
            this.extensions.stream()
                    .filter(Objects::nonNull)
                    .map(AbstractDescribedNode.class::cast)
                    .map(AbstractDescribedNode::getDescription).filter(Objects::nonNull)
                    .map(Description::getContent).filter(Utils::isNotBlank)
                    .map(String::trim).forEach(descriptions::add);
        }
        return descriptions;
    }

    public List getJavaDocFromComments() {
        List comments = new ArrayList<>();
        if (definition != null && definition.getComments() != null) {
            definition.getComments().stream()
                    .map(Comment::getContent).filter(Utils::isNotBlank)
                    .map(String::trim).forEach(comments::add);
        }
        extensions.stream()
                .map(Node::getComments)
                .flatMap(Collection::stream).filter(Objects::nonNull)
                .map(Comment::getContent).filter(Utils::isNotBlank)
                .map(String::trim).forEach(comments::add);
        return comments;
    }

    public List getDirectiveNames() {
        List directives = new ArrayList<>();
        if (this.definition instanceof DirectivesContainer) {
            List definitionDirectives = ((DirectivesContainer) this.definition).getDirectives();
            if (!Utils.isEmpty(definitionDirectives)) {
                definitionDirectives.stream().map(Directive::getName).forEach(directives::add);
            }
            this.extensions.stream().filter(Objects::nonNull)
                    .map(DirectivesContainer.class::cast)
                    .map(DirectivesContainer::getDirectives).filter(Objects::nonNull)
                    .forEach(ds -> ds.forEach(d -> directives.add(((Directive) d).getName())));
        }
        return directives;
    }

    public T getDefinition() {
        return definition;
    }

    public void setDefinition(T definition) {
        this.definition = definition;
    }

    public List getExtensions() {
        return extensions;
    }

    public void setExtensions(List extensions) {
        this.extensions = extensions;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy