<commit_before>/**
 * Copyright (c) 2017-present, Graphene.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import 'reflect-metadata';
import {
  GraphQLType,
  GraphQLString,
  GraphQLFloat,
  GraphQLList,
  assertType,
  isType,
  GraphQLFieldConfig
} from 'graphql';

export const GRAPHENE_TYPE_METADATA_KEY = 'graphene:type';
export const GRAPHENE_FIELDS_METADATA_KEY = 'graphene:fields';
export const GRAPHENE_DESCRIPTION_KEY = 'graphene:description';
export const GRAPHENE_DEPRECATED_KEY = 'graphene:deprecated';
// export const GRAPHENE_ARGUMENTS_METADATA_KEY = 'graphene:arguments';

// A utility funciton to convert values like:
// [String]
// To:
// GraphQLList(GraphQLString)
export const convertArrayToGraphQLList = (
  target: Array<any>
): GraphQLList<GraphQLType> => {
  if (target.length !== 1) {
    throw new Error(
      `Graphene Array definitions should contain exactly one element. Received ${
        target.length
      } elements.`
    );
  }
  var innerType = getGraphQLType(target[0]);
  return new GraphQLList(innerType);
};

// A utility funciton to convert values like:
// String
// To:
// GraphQLString
// Note: for String -> GraphQLString conversion to work we have to call
// setupNativeTypes
export const getGraphQLType = (target: any): GraphQLType => {
  if (Array.isArray(target)) {
    return convertArrayToGraphQLList(target);
  }
  if (isType(target)) {
    return target;
  }
  var metadataType: GraphQLType = Reflect.getMetadata(
    GRAPHENE_TYPE_METADATA_KEY,
    target
  );
  if (metadataType) {
    return metadataType;
  }
  throw new Error(
    `The target ${target} have no GraphQL type associated to it.`
  );
};

// An utility function to associate a GraphQL type
// with the specified target.
// This method takes full advantage of the Reflection API.
export const setGraphQLType = (target: any, type: GraphQLType): void => {
  // Is the provided type a GraphQLType?
  // Will fail if not.
  assertType(type);

  // First we check this type have no other type associated with it.
  if (Reflect.hasMetadata(GRAPHENE_TYPE_METADATA_KEY, target)) {
    throw new Error(
      `Type ${String(target)} already have a Graphene type attached.`
    );
  }
  // We define the type metadata through reflection
  Reflect.defineMetadata(GRAPHENE_TYPE_METADATA_KEY, type, target);
};

export type UnmountedFieldMap = {
  [key: string]: () => GraphQLFieldConfig<any, any>;
};

export type MountedFieldMap = {
  [key: string]: GraphQLFieldConfig<any, any>;
};

// Get the fields given a constructor
export const getFields = (target: any): UnmountedFieldMap => {
  var fields: UnmountedFieldMap;
  if (!Reflect.hasMetadata(GRAPHENE_FIELDS_METADATA_KEY, target)) {
    fields = {};
    Reflect.defineMetadata(GRAPHENE_FIELDS_METADATA_KEY, fields, target);
  } else {
    fields = Reflect.getMetadata(GRAPHENE_FIELDS_METADATA_KEY, target);
  }
  return fields;
};

// mountFields
export const mountFields = (
  fields: UnmountedFieldMap
) => (): MountedFieldMap => {
  var key: string;
  var finalFields: MountedFieldMap = {};
  for (key in fields) {
    finalFields[key] = fields[key]();
  }
  return finalFields;
};

export const assertFields = (target: any, fields: UnmountedFieldMap) => {
  if (Object.keys(fields).length === 0) {
    throw new Error(`Type ${target} must have fields defined on it.`);
  }
};

// The setup function that permit us to use
// the native String and Number types.
export const setupNativeTypes = () => {
  setGraphQLType(String, GraphQLString);
  setGraphQLType(Number, GraphQLFloat);
};

// Description setter
// Usage:
//   setDescription(MyClass, 'the description')
// Or:
//   setDescription(MyClass, 'methodName', 'the description')
export const setDescription = (
  target: any,
  keyOrDescription: string,
  description?: string
) => {
  if (typeof description === 'undefined') {
    Reflect.defineMetadata(GRAPHENE_DESCRIPTION_KEY, keyOrDescription, target);
  } else {
    Reflect.defineMetadata(
      GRAPHENE_DESCRIPTION_KEY,
      description,
      target,<commit_msg>Added Boolean to the mapped fields<commit_after>/**
 * Copyright (c) 2017-present, Graphene.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import 'reflect-metadata';
import {
  GraphQLType,
  GraphQLString,
  GraphQLFloat,
  GraphQLList,
  GraphQLBoolean,
  assertType,
  isType,
  GraphQLFieldConfig
} from 'graphql';

export const GRAPHENE_TYPE_METADATA_KEY = 'graphene:type';
export const GRAPHENE_FIELDS_METADATA_KEY = 'graphene:fields';
export const GRAPHENE_DESCRIPTION_KEY = 'graphene:description';
export const GRAPHENE_DEPRECATED_KEY = 'graphene:deprecated';
// export const GRAPHENE_ARGUMENTS_METADATA_KEY = 'graphene:arguments';

// A utility funciton to convert values like:
// [String]
// To:
// GraphQLList(GraphQLString)
export const convertArrayToGraphQLList = (
  target: Array<any>
): GraphQLList<GraphQLType> => {
  if (target.length !== 1) {
    throw new Error(
      `Graphene Array definitions should contain exactly one element. Received ${
        target.length
      } elements.`
    );
  }
  var innerType = getGraphQLType(target[0]);
  return new GraphQLList(innerType);
};

// A utility funciton to convert values like:
// String
// To:
// GraphQLString
// Note: for String -> GraphQLString conversion to work we have to call
// setupNativeTypes
export const getGraphQLType = (target: any): GraphQLType => {
  if (Array.isArray(target)) {
    return convertArrayToGraphQLList(target);
  }
  if (isType(target)) {
    return target;
  }
  var metadataType: GraphQLType = Reflect.getMetadata(
    GRAPHENE_TYPE_METADATA_KEY,
    target
  );
  if (metadataType) {
    return metadataType;
  }
  throw new Error(
    `The target ${target} have no GraphQL type associated to it.`
  );
};

// An utility function to associate a GraphQL type
// with the specified target.
// This method takes full advantage of the Reflection API.
export const setGraphQLType = (target: any, type: GraphQLType): void => {
  // Is the provided type a GraphQLType?
  // Will fail if not.
  assertType(type);

  // First we check this type have no other type associated with it.
  if (Reflect.hasMetadata(GRAPHENE_TYPE_METADATA_KEY, target)) {
    throw new Error(
      `Type ${String(target)} already have a Graphene type attached.`
    );
  }
  // We define the type metadata through reflection
  Reflect.defineMetadata(GRAPHENE_TYPE_METADATA_KEY, type, target);
};

export type UnmountedFieldMap = {
  [key: string]: () => GraphQLFieldConfig<any, any>;
};

export type MountedFieldMap = {
  [key: string]: GraphQLFieldConfig<any, any>;
};

// Get the fields given a constructor
export const getFields = (target: any): UnmountedFieldMap => {
  var fields: UnmountedFieldMap;
  if (!Reflect.hasMetadata(GRAPHENE_FIELDS_METADATA_KEY, target)) {
    fields = {};
    Reflect.defineMetadata(GRAPHENE_FIELDS_METADATA_KEY, fields, target);
  } else {
    fields = Reflect.getMetadata(GRAPHENE_FIELDS_METADATA_KEY, target);
  }
  return fields;
};

// mountFields
export const mountFields = (
  fields: UnmountedFieldMap
) => (): MountedFieldMap => {
  var key: string;
  var finalFields: MountedFieldMap = {};
  for (key in fields) {
    finalFields[key] = fields[key]();
  }
  return finalFields;
};

export const assertFields = (target: any, fields: UnmountedFieldMap) => {
  if (Object.keys(fields).length === 0) {
    throw new Error(`Type ${target} must have fields defined on it.`);
  }
};

// The setup function that permit us to use
// the native String and Number types.
export const setupNativeTypes = () => {
  setGraphQLType(String, GraphQLString);
  setGraphQLType(Boolean, GraphQLBoolean);
  setGraphQLType(Number, GraphQLFloat);
};

// Description setter
// Usage:
//   setDescription(MyClass, 'the description')
// Or:
//   setDescription(MyClass, 'methodName', 'the description')
export const setDescription = (
  target: any,
  keyOrDescription: string,
  description?: string
) => {
  if (typeof description === 'undefined') {
    Reflect.defineMetadata(GRAPHENE_DESCRIPTION_KEY, keyOrDescription, target);
  } else {
    Reflect.defineMetadata(
      GRAPHENE_DESCRIPTION_KEY,
      description,
      target,
{'fraction_non_alphanumeric': 0.10998085208233604, 'fraction_numerical': 0.001675442795595979, 'mean_word_length': 4.067919951485749, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3272583', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2604, 'n_words': 745}