<commit_before>import flow from 'lodash/flow';
import ValidationError from 'oors/build/errors/ValidationError';

export const withSchema = schema => {
  let validate;

  return resolver => (_, args, ctx, info) => {
    if (!validate) {
      validate = ctx.ajv.compile(schema);
    }

    if (!validate(args)) {
      throw new ValidationError(validate.errors);
    }

    return resolver(_, args, ctx, info);
  };
};

export const withArgs = parser => resolver => async (_, args, ctx, info) =>
  resolver(_, await parser(args, ctx), ctx, info);

export const withContext = fn => resolver => async (_, args, ctx, info) =>
  resolver(_, args, await fn(ctx), info);

export const compose = (...decorators) => flow(decorators.reverse());

export const resolver = definition => (target, propr, descriptor) => {
  Object.assign(descriptor, {
    enumerable: true,
  });

  Object.assign(descriptor.value, {
    isResolver: true,
    definition,
  });

  return descriptor;
};
<commit_msg>[oors-graphQL] withArgs decorator promise resolving support<commit_after>import flow from 'lodash/flow';
import ValidationError from 'oors/build/errors/ValidationError';
import isPlainObject from 'lodash/isPlainObject';

export const withSchema = schema => {
  let validate;

  return resolver => (_, args, ctx, info) => {
    if (!validate) {
      validate = ctx.ajv.compile(schema);
    }

    if (!validate(args)) {
      throw new ValidationError(validate.errors);
    }

    return resolver(_, args, ctx, info);
  };
};

const replaceValues = (obj, values, replacers) => {
  Object.keys(obj).forEach(key => {
    const value = obj[key];

    if (isPlainObject(value)) {
      replaceValues(value, values, replacers);
    } else {
      const index = values.indexOf(value);
      if (index > -1) {
        obj[key] = replacers[index]; // eslint-disable-line no-param-reassign
      }
    }
  });
};

export const withArgs = parser => resolver => async (_, args, ctx, info) => {
  const keys = [];
  const promises = [];

  const finalArgs = await parser(args, ctx, {
    resolve: promise => {
      const key = Symbol(keys.index);
      keys.push(key);
      promises.push(promise);
      return key;
    },
  });

  const values = await Promise.all(promises);

  replaceValues(finalArgs, keys, values);

  return resolver(_, finalArgs, ctx, info);
};

export const withContext = fn => resolver => async (_, args, ctx, info) =>
  resolver(_, args, await fn(ctx), info);

export const compose = (...decorators) => flow(decorators.reverse());

export const resolver = definition => (target, propr, descriptor) => {
  Object.assign(descriptor, {
    enumerable: true,
  });

  Object.assign(descriptor.value, {
    isResolver: true,
    definition,
  });

  return descriptor;
};

{'fraction_non_alphanumeric': 0.14828209764918626, 'fraction_numerical': 0.0003616636528028933, 'mean_word_length': 3.4541062801932365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '4545261', 'n_tokens_mistral': 867, 'n_tokens_neox': 838, 'n_words': 224}