<commit_before>/**
 * Copyright 2016 Eventchain team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 */
package org.eventchain.graphql;

import graphql.annotations.GraphQLAnnotations;
import graphql.annotations.GraphQLField;
import graphql.schema.*;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.eventchain.Command;
import org.eventchain.layout.Layout;
import org.eventchain.layout.LayoutIgnore;
import org.eventchain.layout.Property;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;

@Slf4j
public abstract class GraphQLCommand<R> extends Command<R> {

    private final Class<R> resultClass;
    private final Layout<? extends GraphQLCommand> layout;

    @SneakyThrows
    public GraphQLCommand(Class<R> resultClass) {
        this.resultClass = resultClass;
        layout = new Layout<>(getClass());
    }

    private GraphQLFieldDefinition mutation;

    @Setter @Getter (onMethod = @__(@LayoutIgnore)) @Accessors(fluent = true)
    private DataFetchingEnvironment environment;

    @SneakyThrows @SuppressWarnings("unchecked")
    public R mutate(DataFetchingEnvironment environment) {
        GraphQLCommand instance = getClass().newInstance();
        instance.environment(environment);

        Map<String, Object> input = (Map<String, Object>) environment.getArguments().values().toArray()[0];

        instance.setClientMutationId((String) input.get("clientMutationId"));

        for (Property property : layout.getProperties()) {
            Object value = input.get(property.getName());
            property.set(instance, property.getType().isInstanceOf(Optional.class) ? Optional.of(value) : value);
        }

        GraphQLContext context = (GraphQLContext) environment.getContext();
        context.setCommand(instance);

        instance.beforePublishing();
        CompletableFuture<R> future = context.getRepository().publish(instance);
        return future.get();
    }

    protected void beforePublishing() {};

    public static class Mutation extends GraphQLInputObjectType {
        public Mutation(GraphQLObjectType objectType) {
            super("input", objectType.getDescription(), fields(objectType));
        }

        private static List<GraphQLInputObjectField> fields(GraphQLObjectType objectType) {
            List<GraphQLInputObjectField> fields = new ArrayList<>();
            for (GraphQLFieldDefinition field : objectType.getFieldDefinitions()) {
                GraphQLInputObjectField inputField = newInputObjectField().
                        name(field.getName()).
                        description(field.getDescription()).
                        type(field.getType() instanceof GraphQLObjectType ? GraphQLAnnotations.inputObject((GraphQLObjectType) field.getType()) : (GraphQLInputType) field.getType()).
                        build();
                fields.add(inputField);
            }
            return fields;
        }
    }

    @SneakyThrows @LayoutIgnore @SuppressWarnings("unused")
    public GraphQLFieldDefinition getMutation() {
        if (mutation == null) {
            GraphQLObjectType objectType =  GraphQLAnnotations.object(this.getClass());
            GraphQLObjectType resultType = GraphQLAnnotations.objectBuilder(resultClass).
                    name(getClass().getSimpleName()).
                    field(newFieldDefinition().name("clientMutationId").type(GraphQLString).dataFetcher(e -> ((GraphQLCommand)((GraphQLContext)e.getContext()).getCommand()).getClientMutationId()).build()).build();

            GraphQLFieldDefinition.Builder builder = newFieldDefinition().
                    name(objectType.getName()).
                    type(resultType).
                    argument(newArgument().name("input").type(new Mutation(objectType)).build()).
                    dataFetcher(this::mutate);

            mutation = builder.build();
        }
        return mutation;
    }

    @Getter(onMethod = @__({@GraphQLField, @LayoutIgnore})) @Setter
    private String clientMutationId;
}<commit_msg>Make sure input objects get different names<commit_after>/**
 * Copyright 2016 Eventchain team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 */
package org.eventchain.graphql;

import graphql.annotations.GraphQLAnnotations;
import graphql.annotations.GraphQLField;
import graphql.schema.*;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.eventchain.Command;
import org.eventchain.layout.Layout;
import org.eventchain.layout.LayoutIgnore;
import org.eventchain.layout.Property;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;

@Slf4j
public abstract class GraphQLCommand<R> extends Command<R> {

    private final Class<R> resultClass;
    private final Layout<? extends GraphQLCommand> layout;

    @SneakyThrows
    public GraphQLCommand(Class<R> resultClass) {
        this.resultClass = resultClass;
        layout = new Layout<>(getClass());
    }

    private GraphQLFieldDefinition mutation;

    @Setter @Getter (onMethod = @__(@LayoutIgnore)) @Accessors(fluent = true)
    private DataFetchingEnvironment environment;

    @SneakyThrows @SuppressWarnings("unchecked")
    public R mutate(DataFetchingEnvironment environment) {
        GraphQLCommand instance = getClass().newInstance();
        instance.environment(environment);

        Map<String, Object> input = (Map<String, Object>) environment.getArguments().values().toArray()[0];

        instance.setClientMutationId((String) input.get("clientMutationId"));

        for (Property property : layout.getProperties()) {
            Object value = input.get(property.getName());
            property.set(instance, property.getType().isInstanceOf(Optional.class) ? Optional.of(value) : value);
        }

        GraphQLContext context = (GraphQLContext) environment.getContext();
        context.setCommand(instance);

        instance.beforePublishing();
        CompletableFuture<R> future = context.getRepository().publish(instance);
        return future.get();
    }

    protected void beforePublishing() {};

    public static class Mutation extends GraphQLInputObjectType {
        public Mutation(GraphQLObjectType objectType) {
            super(objectType.getName() + "Input", objectType.getDescription(), fields(objectType));
        }

        private static List<GraphQLInputObjectField> fields(GraphQLObjectType objectType) {
            List<GraphQLInputObjectField> fields = new ArrayList<>();
            for (GraphQLFieldDefinition field : objectType.getFieldDefinitions()) {
                GraphQLInputObjectField inputField = newInputObjectField().
                        name(field.getName()).
                        description(field.getDescription()).
                        type(field.getType() instanceof GraphQLObjectType ? GraphQLAnnotations.inputObject((GraphQLObjectType) field.getType()) : (GraphQLInputType) field.getType()).
                        build();
                fields.add(inputField);
            }
            return fields;
        }
    }

    @SneakyThrows @LayoutIgnore @SuppressWarnings("unused")
    public GraphQLFieldDefinition getMutation() {
        if (mutation == null) {
            GraphQLObjectType objectType = GraphQLAnnotations.object(this.getClass());
            GraphQLObjectType resultType = GraphQLAnnotations.objectBuilder(resultClass).
                    name(getClass().getSimpleName()).
                    field(newFieldDefinition().name("clientMutationId").type(GraphQLString).dataFetcher(e -> ((GraphQLCommand)((GraphQLContext)e.getContext()).getCommand()).getClientMutationId()).build()).build();

            GraphQLFieldDefinition.Builder builder = newFieldDefinition().
                    name(objectType.getName()).
                    type(resultType).
                    argument(newArgument().name("input").type(new Mutation(objectType)).build()).
                    dataFetcher(this::mutate);

            mutation = builder.build();
        }
        return mutation;
    }

    @Getter(onMethod = @__({@GraphQLField, @LayoutIgnore})) @Setter
    private String clientMutationId;
}
{'fraction_non_alphanumeric': 0.1019995960412038, 'fraction_numerical': 0.002423752777216724, 'mean_word_length': 3.4032903512672297, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5933108', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2503, 'n_words': 579}