<commit_before>import { parse } from 'graphql/language/parser';

import {
  OperationDefinition,
  Document,
  FragmentDefinition,
} from 'graphql';

export function parseDocument(doc: string): Document {
  const parsed = parse(doc);

  if (!parsed || parsed.kind !== 'Document') {
    throw new Error('Not a valid GraphQL document.');
  }

  return parsed;
}

export function parseFragment(fragment: string): FragmentDefinition {
  const parsedFragment: Document = parseDocument(fragment);

  if (parsedFragment.definitions.length !== 1) {
    throw new Error('Must have exactly one definition in document.');
  }

  if (parsedFragment.definitions[0].kind !== 'FragmentDefinition') {
    throw new Error('Must be a fragment.');
  }

  const fragmentDef: FragmentDefinition = parsedFragment.definitions[0] as FragmentDefinition;

  return fragmentDef;
}

export function parseQuery(query: string): OperationDefinition {
  const parsedQuery: Document = parseDocument(query);

  if (parsedQuery.kind !== 'Document' && parsedQuery.definitions.length !== 1) {
    throw new Error('Must have exactly one definition in document.');
  }

  const queryDefinition: OperationDefinition = parsedQuery.definitions[0] as OperationDefinition;

  if (queryDefinition.operation !== 'query') {
    throw new Error('Definition must be a query.');
  }

  return queryDefinition;
}

export function parseMutation(mutation: string): OperationDefinition {
  const parsedMutation: Document = parseDocument(mutation);

  if (parsedMutation.kind !== 'Document' && parsedMutation.definitions.length !== 1) {
    throw new Error('Must have exactly one definition in document.');
  }

  const mutationDefinition: OperationDefinition =
    parsedMutation.definitions[0] as OperationDefinition;

  if (mutationDefinition.operation !== 'mutation') {
    throw new Error('Definition must be a mutation.');
  }

  return mutationDefinition;
}<commit_msg>Reduce code duplication in the parsing methods<commit_after>import { parse } from 'graphql/language/parser';

import {
  Document,
  Definition,
  OperationDefinition,
  FragmentDefinition,
} from 'graphql';

export function parseDocument(doc: string): Document {
  const parsed = parse(doc);

  if (!parsed || parsed.kind !== 'Document') {
    throw new Error('Not a valid GraphQL document.');
  }

  return parsed as Document;
}

function parseDefinition(definition: string): Definition {
  const parsedDocument = parseDocument(definition);

  if (parsedDocument.definitions.length !== 1) {
    throw new Error('Must have exactly one definition in document.');
  }

  return parsedDocument.definitions[0];
}

export function parseFragment(fragment: string): FragmentDefinition {
  const parsedFragment = parseDefinition(fragment);

  if (parsedFragment.kind !== 'FragmentDefinition') {
    throw new Error('Must be a fragment.');
  }

  return parsedFragment as FragmentDefinition;
}

export function parseQuery(query: string): OperationDefinition {
  const queryDefinition = parseDefinition(query) as OperationDefinition;

  if (queryDefinition.kind !== 'OperationDefinition' && queryDefinition.operation !== 'query') {
    throw new Error('Definition must be a query.');
  }

  return queryDefinition;
}

export function parseMutation(mutation: string): OperationDefinition {
  const mutationDefinition = parseDefinition(mutation) as OperationDefinition;

  if (mutationDefinition.kind !== 'OperationDefinition' && mutationDefinition.operation !== 'mutation') {
    throw new Error('Definition must be a mutation.');
  }

  return mutationDefinition;
}
{'fraction_non_alphanumeric': 0.10875034945485043, 'fraction_numerical': 0.002516074923119933, 'mean_word_length': 5.003355704697986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4758422', 'n_tokens_mistral': 937, 'n_tokens_neox': 912, 'n_words': 281}