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

graphql.schema.GraphQLInputObjectType Maven / Gradle / Ivy

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

import graphql.AssertException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.Assert.assertNotNull;

public class GraphQLInputObjectType implements GraphQLType, GraphQLInputType, GraphQLUnmodifiedType, GraphQLNullableType {

    private final String name;
    private final String description;


    private final Map fieldMap = new LinkedHashMap();

    public GraphQLInputObjectType(String name, String description, List fields) {
        assertNotNull(name, "name can't be null");
        assertNotNull(fields, "fields can't be null");
        this.name = name;
        this.description = description;
        buildMap(fields);
    }

    private void buildMap(List fields) {
        for (GraphQLInputObjectField field : fields) {
            String name = field.getName();
            if (fieldMap.containsKey(name))
                throw new AssertException("field " + name + " redefined");
            fieldMap.put(name, field);
        }
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public List getFields() {
        return new ArrayList(fieldMap.values());
    }

    public GraphQLInputObjectField getField(String name) {
        return fieldMap.get(name);
    }

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

    public static class Builder {
        private String name;
        private String description;
        private List fields = new ArrayList();

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

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

        public Builder field(GraphQLInputObjectField field) {
            assertNotNull(field, "field can't be null");
            fields.add(field);
            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(BuilderFunction builderFunction) { assertNotNull(builderFunction, "builderFunction should not be null"); GraphQLInputObjectField.Builder builder = GraphQLInputObjectField.newInputObjectField(); builder = builderFunction.apply(builder); return field(builder); } /** * Same effect as the field(GraphQLFieldDefinition). Builder.build() is called * from within * * @param builder an un-built/incomplete GraphQLFieldDefinition * @return this */ public Builder field(GraphQLInputObjectField.Builder builder) { this.fields.add(builder.build()); return this; } public Builder fields(List fields) { for (GraphQLInputObjectField field : fields) { field(field); } return this; } public GraphQLInputObjectType build() { return new GraphQLInputObjectType(name, description, fields); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy