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

net.morimekta.providence.graphql.gql.GQLOperation Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package net.morimekta.providence.graphql.gql;

import net.morimekta.providence.descriptor.PField;
import net.morimekta.providence.descriptor.PService;
import net.morimekta.providence.descriptor.PServiceMethod;
import net.morimekta.util.collect.UnmodifiableList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.util.Objects;

/**
 * Representing a single GQL operation, which is either a query
 * or a mutation.
 *
 * {@inheritDoc}
 */
@Immutable
public class GQLOperation implements GQLSelection {
    private final PService           service;
    private final List selectionSet;
    private final boolean            mutation;
    private final String             name;

    public GQLOperation(@Nonnull PService service,
                        boolean mutation,
                        @Nullable String name,
                        @Nonnull List selectionSet) {
        this.service = service;
        this.mutation = mutation;
        this.name = name;
        this.selectionSet = selectionSet;
    }

    public boolean isMutation() {
        return mutation;
    }

    public boolean hasMethodCall(PServiceMethod method) {
        for (GQLSelection selection : selectionSet) {
            if (selection instanceof GQLMethodCall) {
                GQLMethodCall call = (GQLMethodCall) selection;
                if (call.getMethod().equals(method)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean hasSelection(@Nonnull PField... fields) {
        return false;
    }

    @Nonnull
    @Override
    public List getSelection(@Nonnull PField field) {
        return UnmodifiableList.listOf();
    }

    @Nullable
    public String getName() {
        return name;
    }

    @Nonnull
    public PService getService() {
        return service;
    }

    @Nonnull
    @Override
    public List getSelectionSet() {
        return selectionSet;
    }

    @Override
    public int hashCode() {
        return Objects.hash(GQLOperation.class, name, service, selectionSet);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (name != null)  {
            if (mutation) {
                builder.append("mutation ");
            } else {
                builder.append("query ");
            }
            builder.append(name);
            builder.append(" ");
        }
        builder.append("{");
        boolean first = true;
        for (GQLSelection selection : selectionSet) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(selection.toString());
        }
        builder.append("}");
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy