<commit_before>const {
  addFields,
  addArguments,
  createTypeFetcher,
} = require('olymp-graphql/server');
const { get, set, lowerFirst } = require('lodash');

export default {
  // aggregation
  name: 'relation',
  description: 'Marks a type as a relative.',
  resolveStatic: {
    enter(node, directive, { resolvers, parent, ancestors, ast }) {
      const isList = node.type.kind === 'ListType';
      const type = isList ? node.type.type : node.type;

      const argProperty =
        directive.arguments.length &&
        directive.arguments.find(x => x.name.value === 'property');
      const argRelationType =
        directive.arguments.length &&
        directive.arguments.find(x => x.name.value === 'type');
      const relationType = get(
        argRelationType,
        'value.value',
        isList ? '1-n' : '1-1'
      );

      const leftType = ancestors[ancestors.length - 1].name.value;
      const leftTable = ancestors[
        ancestors.length - 1
      ].name.value.toLowerCase();
      const leftField = node.name.value;
      const leftNode = parent;
      const rightType = type.name.value;
      const rightTable = type.name.value.toLowerCase();
      const rightField =
        get(argProperty, 'value.value') || lowerFirst(leftTable);
      const rightNode = createTypeFetcher(
        node => get(node, 'type.name.value') === rightType
      )(ast);

      // one-to-one / one-to-many
      // Only add xxxId field to store/access the id
      if (relationType === '1-1') {
        addFields(ast, leftNode, `${leftField}Id: String`);
        set(resolvers, `${leftType}.${leftField}`, (source, args, { monk }) =>
          monk.collection(rightTable).findOne({
            id: source[`${leftField}Id`],
          })
        );
      } else if (relationType === '1-n') {
        // Add list accessor and xxxIds field since dealing with many
        // addInputTypes(rightTable, ast);
        /* addArguments(
          ast,
          node,
          `query: ${rightType}Query, sort: ${rightType}Sort, limit: Int, skip: Int`<commit_msg>fix relation<commit_after>const {
  addFields,
  addArguments,
  createTypeFetcher,
} = require('olymp-graphql/server');
const { get, set, lowerFirst } = require('lodash');

export default {
  // aggregation
  name: 'relation',
  description: 'Marks a type as a relative.',
  resolveStatic: {
    enter(node, directive, { resolvers, parent, ancestors, ast }) {
      const isList = node.type.kind === 'ListType';
      const type = isList ? node.type.type : node.type;

      const argProperty =
        directive.arguments.length &&
        directive.arguments.find(x => x.name.value === 'property');
      const argRelationType =
        directive.arguments.length &&
        directive.arguments.find(x => x.name.value === 'type');
      const relationType = get(
        argRelationType,
        'value.value',
        isList ? '1-n' : '1-1'
      );

      const leftType = ancestors[ancestors.length - 1].name.value;
      const leftTable = ancestors[
        ancestors.length - 1
      ].name.value.toLowerCase();
      const leftField = node.name.value;
      const leftNode = parent;
      const rightType = type.name.value;
      const rightTable = type.name.value.toLowerCase();
      const rightField =
        get(argProperty, 'value.value') || lowerFirst(leftTable);
      const rightNode = createTypeFetcher(
        node => get(node, 'type.name.value') === rightType
      )(ast);

      // one-to-one / one-to-many
      // Only add xxxId field to store/access the id
      if (relationType === '1-1') {
        console.log(relationType, `${leftType}.${leftField}`);
        addFields(ast, leftNode, `${leftField}Id: String`);
        set(resolvers, `${leftType}.${leftField}`, (source, args, { monk }) =>
          monk.collection('item').findOne({
            id: source[`${leftField}Id`],
            _type: rightTable,
          })
        );
      } else if (relationType === '1-n') {
        // Add list accessor and xxxIds field since dealing with many
        // addInputTypes(rightTable, ast);
        /* addArguments(
          ast,
          node,
          `query: ${rightType}Query, sort: ${rightType}Sort, limit: Int, skip: Int`
{'fraction_non_alphanumeric': 0.1433640880056778, 'fraction_numerical': 0.003785190442394133, 'mean_word_length': 2.454248366013072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6066', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1264, 'n_words': 294}