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

graphql.normalized.NormalizedQuery Maven / Gradle / Ivy

package graphql.normalized;

import com.google.common.collect.ImmutableListMultimap;
import graphql.Assert;
import graphql.Internal;
import graphql.execution.MergedField;
import graphql.execution.ResultPath;
import graphql.language.Field;
import graphql.schema.FieldCoordinates;
import graphql.schema.GraphQLFieldsContainer;

import java.util.List;
import java.util.Map;

@Internal
public class NormalizedQuery {

    private final List topLevelFields;
    private final ImmutableListMultimap fieldToNormalizedField;
    private final Map normalizedFieldToMergedField;
    private final ImmutableListMultimap coordinatesToNormalizedFields;

    public NormalizedQuery(List topLevelFields,
                           ImmutableListMultimap fieldToNormalizedField,
                           Map normalizedFieldToMergedField,
                           ImmutableListMultimap coordinatesToNormalizedFields) {
        this.topLevelFields = topLevelFields;
        this.fieldToNormalizedField = fieldToNormalizedField;
        this.normalizedFieldToMergedField = normalizedFieldToMergedField;
        this.coordinatesToNormalizedFields = coordinatesToNormalizedFields;
    }

    public ImmutableListMultimap getCoordinatesToNormalizedFields() {
        return coordinatesToNormalizedFields;
    }

    public List getTopLevelFields() {
        return topLevelFields;
    }

    /**
     * This is a multimap: the size of it reflects the all the normalized fields
     *
     * @return an immutable list multi map of field to normalised field
     */
    public ImmutableListMultimap getFieldToNormalizedField() {
        return fieldToNormalizedField;
    }

    public List getNormalizedFields(Field field) {
        return fieldToNormalizedField.get(field);
    }

    public Map getNormalizedFieldToMergedField() {
        return normalizedFieldToMergedField;
    }

    public MergedField getMergedField(NormalizedField normalizedField) {
        return normalizedFieldToMergedField.get(normalizedField);
    }

    public NormalizedField getNormalizedField(MergedField mergedField, GraphQLFieldsContainer fieldsContainer, ResultPath resultPath) {
        List normalizedFields = fieldToNormalizedField.get(mergedField.getSingleField());
        List keysOnlyPath = resultPath.getKeysOnly();
        for (NormalizedField normalizedField : normalizedFields) {
            if (normalizedField.getListOfResultKeys().equals(keysOnlyPath)) {
                if (normalizedField.getObjectTypeNames().contains(fieldsContainer.getName())) {
                    return normalizedField;
                }
            }
        }
        return Assert.assertShouldNeverHappen("normalized field not found");
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy