<commit_before>"use strict";

const {
  GraphQLObjectType,
  GraphQLSchema,
  GraphQLInt,
  GraphQLString,
  GraphQLList,
  GraphQLNonNull,
  GraphQLBoolean,
  GraphQLInputObjectType,
  GraphQLID
} = require('graphql');
const _ = require('lodash');
const pluralize = require('pluralize');
const camelcase = require('camelcase');

const {
  fromGlobalId,
  globalIdField,
  mutationWithClientMutationId
} = require("graphql-relay");

const {
  defaultArgs,
  defaultListArgs,
  attributeFields,
  resolver,
  relay: {
    sequelizeNodeInterface,
    sequelizeConnection
  }
} = require("graphql-sequelize");

const jsonType = require("graphql-sequelize/lib/types/jsonType.js");

function connectionNameForAssociation(Model, associationName) {
  return camelcase(`${Model.name}_${associationName}`);
}
function queryName(Model, type) {
  switch (type) {
    case 'findAll': {
      return camelcase(pluralize.plural(Model.name));
    }
    case 'findById': {
      return camelcase(Model.name);
    }
    default: {
      console.warn('Unknown query type: ',type);
      return camelcase(`${type}_${Model.name}`);
    }
  }
}
function mutationName(Model, type) {
  switch (type) {
    case 'create': {
      return camelcase(`${type}_${Model.name}`);
    }
    case 'update': {
      return camelcase(`${type}_${pluralize.plural(Model.name)}`);
    }
    case 'updateOne': {
      return camelcase(`update_${Model.name}`);
    }
    case 'delete': {
      return camelcase(`${type}_${pluralize.plural(Model.name)}`);
    }
    case 'deleteOne': {
      return camelcase(`delete_${Model.name}`);
    }
    default: {
      console.warn('Unknown mutation type: ',type);
      return camelcase(`${type}_${Model.name}`);
    }
  }
}

function convertFieldsToGlobalId(Model, fields) {
  // Fix Relay Global ID
  _.each(Object.keys(Model.rawAttributes), (k) => {
    if (k === "clientMutationId") {
      return;
    }
    // Check if reference attribute
    let attr = Model.rawAttributes[k];
    if (attr.references) {
      // console.log(`Replacing ${Model.name}'s field ${k} with globalIdField.`);
      let modelName = attr.references.model;
      // let modelType = types[modelName];
      fields[k] = globalIdField(modelName);
    } else if (attr.primaryKey) {
      fields[k] = globalIdField(Model.name);
      // Make primaryKey optional (allowNull=True)
      fields[k].type = GraphQLID;
    }
  });
}

function convertFieldsFromGlobalId(Model, data) {
  // Fix Relay Global ID
  _.each(Object.keys(data), (k) => {
    if (k === "clientMutationId") {
      return;
    }
    // Check if reference attribute
    let attr = Model.rawAttributes[k];
    if (attr.references || attr.primaryKey) {
      let {id} = fromGlobalId(data[k]);
      data[k] = parseInt(id);
    }
  });
}

function _createRecord({
  mutations,
  Model,<commit_msg>fix(convertFieldsFromGlobalId): Fix non-int id's<commit_after>"use strict";

const {
  GraphQLObjectType,
  GraphQLSchema,
  GraphQLInt,
  GraphQLString,
  GraphQLList,
  GraphQLNonNull,
  GraphQLBoolean,
  GraphQLInputObjectType,
  GraphQLID
} = require('graphql');
const _ = require('lodash');
const pluralize = require('pluralize');
const camelcase = require('camelcase');

const {
  fromGlobalId,
  globalIdField,
  mutationWithClientMutationId
} = require("graphql-relay");

const {
  defaultArgs,
  defaultListArgs,
  attributeFields,
  resolver,
  relay: {
    sequelizeNodeInterface,
    sequelizeConnection
  }
} = require("graphql-sequelize");

const jsonType = require("graphql-sequelize/lib/types/jsonType.js");

function connectionNameForAssociation(Model, associationName) {
  return camelcase(`${Model.name}_${associationName}`);
}
function queryName(Model, type) {
  switch (type) {
    case 'findAll': {
      return camelcase(pluralize.plural(Model.name));
    }
    case 'findById': {
      return camelcase(Model.name);
    }
    default: {
      console.warn('Unknown query type: ',type);
      return camelcase(`${type}_${Model.name}`);
    }
  }
}
function mutationName(Model, type) {
  switch (type) {
    case 'create': {
      return camelcase(`${type}_${Model.name}`);
    }
    case 'update': {
      return camelcase(`${type}_${pluralize.plural(Model.name)}`);
    }
    case 'updateOne': {
      return camelcase(`update_${Model.name}`);
    }
    case 'delete': {
      return camelcase(`${type}_${pluralize.plural(Model.name)}`);
    }
    case 'deleteOne': {
      return camelcase(`delete_${Model.name}`);
    }
    default: {
      console.warn('Unknown mutation type: ',type);
      return camelcase(`${type}_${Model.name}`);
    }
  }
}

function convertFieldsToGlobalId(Model, fields) {
  // Fix Relay Global ID
  _.each(Object.keys(Model.rawAttributes), (k) => {
    if (k === "clientMutationId") {
      return;
    }
    // Check if reference attribute
    let attr = Model.rawAttributes[k];
    if (attr.references) {
      // console.log(`Replacing ${Model.name}'s field ${k} with globalIdField.`);
      let modelName = attr.references.model;
      // let modelType = types[modelName];
      fields[k] = globalIdField(modelName);
    } else if (attr.primaryKey) {
      fields[k] = globalIdField(Model.name);
      // Make primaryKey optional (allowNull=True)
      fields[k].type = GraphQLID;
    }
  });
}

function convertFieldsFromGlobalId(Model, data) {
  // Fix Relay Global ID
  _.each(Object.keys(data), (k) => {
    if (k === "clientMutationId") {
      return;
    }
    // Check if reference attribute
    let attr = Model.rawAttributes[k];
    if (attr.references || attr.primaryKey) {
      let {id} = fromGlobalId(data[k]);

      // Check if id is numeric.
      if(!_.isNaN(_.toNumber(id))) {
          data[k] = parseInt(id);
      } else {
          data[k] = id;
      }
    }
  });
}

function _createRecord({
  mutations,
  Model,
{'fraction_non_alphanumeric': 0.15138007886164923, 'fraction_numerical': 0.0, 'mean_word_length': 3.31508875739645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7077084', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1924, 'n_words': 359}