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

graphql.schema.GraphQLObjectType Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.schema;

import graphql.AssertException;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.ObjectTypeDefinition;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static graphql.util.FpKit.getByName;
import static graphql.util.FpKit.valuesToList;
import static java.lang.String.format;
import static java.util.Collections.emptyList;

/**
 * This is the work horse type and represents an object with one or more field values that can be retrieved
 * by the graphql system.
 *
 * Those fields can themselves by object types and so on until you reach the leaf nodes of the type tree represented
 * by {@link graphql.schema.GraphQLScalarType}s.
 *
 * See http://graphql.org/learn/schema/#object-types-and-fields for more details on the concept.
 */
@PublicApi
public class GraphQLObjectType implements GraphQLType, GraphQLOutputType, GraphQLFieldsContainer, GraphQLCompositeType, GraphQLUnmodifiedType, GraphQLNullableType, GraphQLDirectiveContainer {


    private final String name;
    private final String description;
    private final Map fieldDefinitionsByName = new LinkedHashMap<>();
    private List interfaces = new ArrayList<>();
    private final List directives;
    private final ObjectTypeDefinition definition;

    @Internal
    public GraphQLObjectType(String name, String description, List fieldDefinitions,
                             List interfaces) {
        this(name, description, fieldDefinitions, interfaces, emptyList(), null);
    }

    @Internal
    public GraphQLObjectType(String name, String description, List fieldDefinitions,
                             List interfaces, List directives, ObjectTypeDefinition definition) {
        assertValidName(name);
        assertNotNull(fieldDefinitions, "fieldDefinitions can't be null");
        assertNotNull(interfaces, "interfaces can't be null");
        this.name = name;
        this.description = description;
        this.interfaces = interfaces;
        this.definition = definition;
        this.directives = assertNotNull(directives);
        buildDefinitionMap(fieldDefinitions);
    }

    void replaceTypeReferences(Map typeMap) {
        this.interfaces = this.interfaces.stream()
                .map(type -> (GraphQLOutputType) new SchemaUtil().resolveTypeReference(type, typeMap))
                .collect(Collectors.toList());
    }

    private void buildDefinitionMap(List fieldDefinitions) {
        for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions) {
            String name = fieldDefinition.getName();
            if (fieldDefinitionsByName.containsKey(name))
                throw new AssertException(format("Duplicated definition for field '%s' in type '%s'", name, this.name));
            fieldDefinitionsByName.put(name, fieldDefinition);
        }
    }

    @Override
    public List getDirectives() {
        return new ArrayList<>(directives);
    }

    public GraphQLFieldDefinition getFieldDefinition(String name) {
        return fieldDefinitionsByName.get(name);
    }

    public List getFieldDefinitions() {
        return new ArrayList<>(fieldDefinitionsByName.values());
    }


    /**
     * @return This returns GraphQLInterface or GraphQLTypeReference instances, if the type
     * references are not resolved yet. After they are resolved it contains only GraphQLInterface.
     * Reference resolving happens when a full schema is built.
     */
    public List getInterfaces() {
        return new ArrayList<>(interfaces);
    }

    public String getDescription() {
        return description;
    }


    public String getName() {
        return name;
    }

    public ObjectTypeDefinition getDefinition() {
        return definition;
    }

    @Override
    public String toString() {
        return "GraphQLObjectType{" +
                "name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", fieldDefinitionsByName=" + fieldDefinitionsByName.keySet() +
                ", interfaces=" + interfaces +
                '}';
    }

    /**
     * This helps you transform the current GraphQLObjectType into another one by starting a builder with all
     * the current values and allows you to transform it how you want.
     *
     * @param builderConsumer the consumer code that will be given a builder to transform
     *
     * @return a new object based on calling build on that builder
     */
    public GraphQLObjectType transform(Consumer builderConsumer) {
        Builder builder = newObject(this);
        builderConsumer.accept(builder);
        return builder.build();
    }

    public static Builder newObject() {
        return new Builder();
    }

    public static Builder newObject(GraphQLObjectType existing) {
        return new Builder(existing);
    }

    @PublicApi
    public static class Builder {
        private String name;
        private String description;
        private ObjectTypeDefinition definition;
        private final Map fields = new LinkedHashMap<>();
        private final Map interfaces = new LinkedHashMap<>();
        private final Map directives = new LinkedHashMap<>();

        public Builder() {
        }

        public Builder(GraphQLObjectType existing) {
            name = existing.getName();
            description = existing.getDescription();
            definition = existing.getDefinition();
            fields.putAll(getByName(existing.getFieldDefinitions(), GraphQLFieldDefinition::getName));
            interfaces.putAll(getByName(existing.getInterfaces(), GraphQLType::getName));
            directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName));
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder description(String description) {
            this.description = description;
            return this;
        }

        public Builder definition(ObjectTypeDefinition definition) {
            this.definition = definition;
            return this;
        }

        public Builder field(GraphQLFieldDefinition fieldDefinition) {
            assertNotNull(fieldDefinition, "fieldDefinition can't be null");
            this.fields.put(fieldDefinition.getName(), fieldDefinition);
            return this;
        }

        /**
         * Take a field builder in a function definition and apply. Can be used in a jdk8 lambda
         * e.g.:
         * 
         *     {@code
         *      field(f -> f.name("fieldName"))
         *     }
         * 
* * @param builderFunction a supplier for the builder impl * * @return this */ public Builder field(UnaryOperator builderFunction) { assertNotNull(builderFunction, "builderFunction can't be null"); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition(); builder = builderFunction.apply(builder); return field(builder.build()); } /** * Same effect as the field(GraphQLFieldDefinition). Builder.build() is called * from within * * @param builder an un-built/incomplete GraphQLFieldDefinition * * @return this */ public Builder field(GraphQLFieldDefinition.Builder builder) { return field(builder.build()); } public Builder fields(List fieldDefinitions) { assertNotNull(fieldDefinitions, "fieldDefinitions can't be null"); fieldDefinitions.forEach(this::field); return this; } /** * This is used to clear all the fields in the builder so far. * * @return the builder */ public Builder clearFields() { fields.clear(); return this; } public boolean hasField(String fieldName) { return fields.containsKey(fieldName); } public Builder withInterface(GraphQLInterfaceType interfaceType) { assertNotNull(interfaceType, "interfaceType can't be null"); this.interfaces.put(interfaceType.getName(), interfaceType); return this; } public Builder withInterface(GraphQLTypeReference reference) { assertNotNull(reference, "reference can't be null"); this.interfaces.put(reference.getName(), reference); return this; } public Builder withInterfaces(GraphQLInterfaceType... interfaceType) { for (GraphQLInterfaceType type : interfaceType) { withInterface(type); } return this; } public Builder withInterfaces(GraphQLTypeReference... references) { for (GraphQLTypeReference reference : references) { withInterface(reference); } return this; } /** * This is used to clear all the interfaces in the builder so far. * * @return the builder */ public Builder clearInterfaces() { interfaces.clear(); return this; } public Builder withDirectives(GraphQLDirective... directives) { for (GraphQLDirective directive : directives) { withDirective(directive); } return this; } public Builder withDirective(GraphQLDirective directive) { assertNotNull(directive, "directive can't be null"); directives.put(directive.getName(), directive); return this; } public Builder withDirective(GraphQLDirective.Builder builder) { return withDirective(builder.build()); } /** * This is used to clear all the directives in the builder so far. * * @return the builder */ public Builder clearDirectives() { directives.clear(); return this; } public GraphQLObjectType build() { return new GraphQLObjectType(name, description, valuesToList(fields), valuesToList(interfaces), valuesToList(directives), definition); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy