<commit_before>'use strict';

// Retrieve GraphQL main definition
// Takes into account GraphQL's `operationName`
const getMainDef = function ({
  queryDocument: { definitions },
  operationName,
}) {
  return definitions
    .filter(({ kind }) => kind === 'OperationDefinition')
    .find(({ name }) =>
      !operationName || (name && name.value) === operationName
    );<commit_msg>GraphQL validation<commit_after>'use strict';

const { throwError } = require('../../../../error');

// Retrieve GraphQL main definition
// Takes into account GraphQL's `operationName`
const getMainDef = function ({
  queryDocument: { definitions },
  operationName,
}) {
  const defs = definitions.filter(({ kind }) => kind === 'OperationDefinition');

  const operationNames = defs.map(({ name }) => name && name.value);
  validateOperatioNames({ operationNames });

  return defs.find(({ name }) =>
    !operationName || (name && name.value) === operationName
  );
};

const validateOperatioNames = function ({ operationNames }) {
  operationNames.forEach(validateOperatioName);
};

const validateOperatioName = function (operationName, index, operationNames) {
  const hasDuplicate = operationNames.slice(index + 1).includes(operationName);

  // GraphQL spec 5.1.1.1 'Operation Name Uniqueness'
  if (hasDuplicate) {
    const message = `Two operations are named '${operationName}'`;
    throwError(message, { reason: 'SYNTAX_VALIDATION' });
  }

  // GraphQL spec 5.1.2.1 'Lone Anonymous Operation'
  if (operationName === null && operationNames.length > 1) {
    const message = `All operations must have names, if there are several of them`;
    throwError(message, { reason: 'SYNTAX_VALIDATION' });
  }
{'fraction_non_alphanumeric': 0.14681603773584906, 'fraction_numerical': 0.00589622641509434, 'mean_word_length': 4.253869969040248, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4818263', 'n_tokens_mistral': 489, 'n_tokens_neox': 475, 'n_words': 131}