<commit_before>// Copyright 2017 Google LLC
//
// 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
// limitations under the License.

package com.google.api.graphql.rejoiner;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Message;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentBuilder;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;

import javax.annotation.Nullable;
import javax.inject.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Module for registering parts of a {@link graphql.schema.GraphQLSchema}.
 *
 * <p>Any public fields of type {@link GraphQLFieldDefinition} annotated with {@link Query} or
 * {@link Mutation} will be added to the top level query or mutation. Fields of type {@link
 * TypeModification} annotated with {@link SchemaModification} will be applied to the generated
 * schema in order to add, remove, or replace fields on a GraphQL type. Fields of type {@link
 * FileDescriptor} annotated with {@link ExtraType} will be available to GraphQL when creating the
 * final schema.
 */
public abstract class SchemaModule extends AbstractModule {

  private final ImmutableSet.Builder<Descriptor> referencedDescriptors = ImmutableSet.builder();
  private final List<GraphQLFieldDefinition> allQueriesInModule = new ArrayList<>();
  private final List<GraphQLFieldDefinition> allMutationsInModule = new ArrayList<>();

  /**
   * Returns a reference to the GraphQL type corresponding to the supplied proto.
   *
   * <p>All types in the proto are made available to be included in the GraphQL schema.
   */
  protected final GraphQLOutputType getTypeReference(Descriptor descriptor) {
    referencedDescriptors.add(descriptor);
    return ProtoToGql.getReference(descriptor);
  }

  protected ImmutableList<GraphQLFieldDefinition> extraMutations() {
    return ImmutableList.of();
  }

  protected void addQuery(GraphQLFieldDefinition query) {
    allQueriesInModule.add(query);
  }

  protected void addMutation(GraphQLFieldDefinition mutation) {
    allMutationsInModule.add(mutation);
  }

  protected void addQueryList(List<GraphQLFieldDefinition> queries) {
    allQueriesInModule.addAll(queries);
  }

  protected void addMutationList(List<GraphQLFieldDefinition> mutations) {
    allMutationsInModule.addAll(mutations);
  }

  protected void configureSchema() {}

  @Override
  protected final void configure() {
    configureSchema();
    Multibinder<GraphQLFieldDefinition> queryMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Queries.class);
    Multibinder<GraphQLFieldDefinition> mutationMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Mutations.class);
    Multibinder<TypeModification> typeModificationMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<TypeModification>() {}, Annotations.GraphModifications.class);
    Multibinder<FileDescriptor> extraTypesMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<FileDescriptor>() {}, Annotations.ExtraTypes.class);
    Multibinder<NodeDataFetcher> relayIdMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<NodeDataFetcher>() {}, Annotations.Queries.class);

    allMutationsInModule.addAll(extraMutations());

    try {
      for (Field field : findQueryFields(getClass())) {
        field.setAccessible(true);
        allQueriesInModule.add((GraphQLFieldDefinition) field.get(this));
      }

      for (Field field : findMutationFields(getClass())) {
        field.setAccessible(true);
        allMutationsInModule.add((GraphQLFieldDefinition) field.get(this));
      }

      for (Field field : findTypeModificationFields(getClass())) {
        field.setAccessible(true);
        typeModificationMultibinder.addBinding().toInstance((TypeModification) field.get(this));
      }

      for (Field field : findExtraTypeFields(getClass())) {
        field.setAccessible(true);
        extraTypesMultibinder.addBinding().toInstance((FileDescriptor) field.get(this));
      }

      for (Method method : findMethods(getClass(), Query.class)) {
        String name = method.getAnnotationsByType(Query.class)[0].value();
        allQueriesInModule.add(methodToFieldDefinition(method, name, null));
      }
      for (Method method : findMethods(getClass(), Mutation.class)) {
        String name = method.getAnnotationsByType(Mutation.class)[0].value();
        allMutationsInModule.add(methodToFieldDefinition(method, name, null));
      }

      Namespace namespaceAnnotation = findClassAnnotation(getClass(), Namespace.class);

      if (namespaceAnnotation == null) {
        allMutationsInModule.forEach(
            mutation -> mutationMultibinder.addBinding().toInstance(mutation));
        allQueriesInModule.forEach(query -> queryMultibinder.addBinding().toInstance(query));
      } else {
        String namespace = namespaceAnnotation.value();
        if (!allQueriesInModule.isEmpty()) {
          queryMultibinder
              .addBinding()
              .toInstance(
                  GraphQLFieldDefinition.newFieldDefinition()
                      .staticValue("")
                      .name(namespace)
                      .description(namespace)
                      .type(
                          GraphQLObjectType.newObject()
                              .name("__QUERY_FIELD_GROUP__" + namespace)
                              .fields(allQueriesInModule)
                              .build())
                      .build());
        }
        if (!allMutationsInModule.isEmpty()) {
          mutationMultibinder
              .addBinding()
              .toInstance(
                  GraphQLFieldDefinition.newFieldDefinition()
                      .staticValue("")
                      .name(namespace)
                      .description(namespace)
                      .type(
                          GraphQLObjectType.newObject()
                              .name("__MUTATION_FIELD_GROUP__" + namespace)
                              .fields(allMutationsInModule)
                              .build())
                      .build());<commit_msg>Double underscore prefixes are reserved for introspection<commit_after>// Copyright 2017 Google LLC
//
// 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
// limitations under the License.

package com.google.api.graphql.rejoiner;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Message;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentBuilder;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;

import javax.annotation.Nullable;
import javax.inject.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * Module for registering parts of a {@link graphql.schema.GraphQLSchema}.
 *
 * <p>Any public fields of type {@link GraphQLFieldDefinition} annotated with {@link Query} or
 * {@link Mutation} will be added to the top level query or mutation. Fields of type {@link
 * TypeModification} annotated with {@link SchemaModification} will be applied to the generated
 * schema in order to add, remove, or replace fields on a GraphQL type. Fields of type {@link
 * FileDescriptor} annotated with {@link ExtraType} will be available to GraphQL when creating the
 * final schema.
 */
public abstract class SchemaModule extends AbstractModule {

  private final ImmutableSet.Builder<Descriptor> referencedDescriptors = ImmutableSet.builder();
  private final List<GraphQLFieldDefinition> allQueriesInModule = new ArrayList<>();
  private final List<GraphQLFieldDefinition> allMutationsInModule = new ArrayList<>();

  /**
   * Returns a reference to the GraphQL type corresponding to the supplied proto.
   *
   * <p>All types in the proto are made available to be included in the GraphQL schema.
   */
  protected final GraphQLOutputType getTypeReference(Descriptor descriptor) {
    referencedDescriptors.add(descriptor);
    return ProtoToGql.getReference(descriptor);
  }

  protected ImmutableList<GraphQLFieldDefinition> extraMutations() {
    return ImmutableList.of();
  }

  protected void addQuery(GraphQLFieldDefinition query) {
    allQueriesInModule.add(query);
  }

  protected void addMutation(GraphQLFieldDefinition mutation) {
    allMutationsInModule.add(mutation);
  }

  protected void addQueryList(List<GraphQLFieldDefinition> queries) {
    allQueriesInModule.addAll(queries);
  }

  protected void addMutationList(List<GraphQLFieldDefinition> mutations) {
    allMutationsInModule.addAll(mutations);
  }

  protected void configureSchema() {}

  @Override
  protected final void configure() {
    configureSchema();
    Multibinder<GraphQLFieldDefinition> queryMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Queries.class);
    Multibinder<GraphQLFieldDefinition> mutationMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<GraphQLFieldDefinition>() {}, Annotations.Mutations.class);
    Multibinder<TypeModification> typeModificationMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<TypeModification>() {}, Annotations.GraphModifications.class);
    Multibinder<FileDescriptor> extraTypesMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<FileDescriptor>() {}, Annotations.ExtraTypes.class);
    Multibinder<NodeDataFetcher> relayIdMultibinder =
        Multibinder.newSetBinder(
            binder(), new TypeLiteral<NodeDataFetcher>() {}, Annotations.Queries.class);

    allMutationsInModule.addAll(extraMutations());

    try {
      for (Field field : findQueryFields(getClass())) {
        field.setAccessible(true);
        allQueriesInModule.add((GraphQLFieldDefinition) field.get(this));
      }

      for (Field field : findMutationFields(getClass())) {
        field.setAccessible(true);
        allMutationsInModule.add((GraphQLFieldDefinition) field.get(this));
      }

      for (Field field : findTypeModificationFields(getClass())) {
        field.setAccessible(true);
        typeModificationMultibinder.addBinding().toInstance((TypeModification) field.get(this));
      }

      for (Field field : findExtraTypeFields(getClass())) {
        field.setAccessible(true);
        extraTypesMultibinder.addBinding().toInstance((FileDescriptor) field.get(this));
      }

      for (Method method : findMethods(getClass(), Query.class)) {
        String name = method.getAnnotationsByType(Query.class)[0].value();
        allQueriesInModule.add(methodToFieldDefinition(method, name, null));
      }
      for (Method method : findMethods(getClass(), Mutation.class)) {
        String name = method.getAnnotationsByType(Mutation.class)[0].value();
        allMutationsInModule.add(methodToFieldDefinition(method, name, null));
      }

      Namespace namespaceAnnotation = findClassAnnotation(getClass(), Namespace.class);

      if (namespaceAnnotation == null) {
        allMutationsInModule.forEach(
            mutation -> mutationMultibinder.addBinding().toInstance(mutation));
        allQueriesInModule.forEach(query -> queryMultibinder.addBinding().toInstance(query));
      } else {
        String namespace = namespaceAnnotation.value();
        if (!allQueriesInModule.isEmpty()) {
          queryMultibinder
              .addBinding()
              .toInstance(
                  GraphQLFieldDefinition.newFieldDefinition()
                      .staticValue("")
                      .name(namespace)
                      .description(namespace)
                      .type(
                          GraphQLObjectType.newObject()
                              .name("_QUERY_FIELD_GROUP_" + namespace)
                              .fields(allQueriesInModule)
                              .build())
                      .build());
        }
        if (!allMutationsInModule.isEmpty()) {
          mutationMultibinder
              .addBinding()
              .toInstance(
                  GraphQLFieldDefinition.newFieldDefinition()
                      .staticValue("")
                      .name(namespace)
                      .description(namespace)
                      .type(
                          GraphQLObjectType.newObject()
                              .name("_MUTATION_FIELD_GROUP_" + namespace)
                              .fields(allMutationsInModule)
                              .build())
                      .build());
{'fraction_non_alphanumeric': 0.0945273631840796, 'fraction_numerical': 0.0012756729174639623, 'mean_word_length': 3.2571273418408904, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 47, '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': '5327112', 'n_tokens_mistral': 4083, 'n_tokens_neox': 4008, 'n_words': 923}