<commit_before>const getNestedField = require('./getNestedField')
const fieldsMap = require('./fieldsMap')

// This could be further optimised by just fetching requested sub fields
const getSourceFields = requestedFields => {
  return Object.keys(requestedFields).reduce((agg, x) => [
    ...agg,
    ...fieldsMap[x].fields,
  ], [])
}

const mapDocToGraphQL = (doc, requestedFields) => {
  const docValues = Object.keys(requestedFields).reduce((agg, x) => ({
    ...agg,
    [x]: fieldsMap[x].fields.map(esField => getNestedField(doc._source, esField))
  }), {})
  return Object.keys(requestedFields).reduce((agg, x) => ({
    ...agg,
    [x]: fieldsMap[x].resolveValues(docValues[x])
  }), {})
}

module.exports = {
  getSourceFields,
  mapDocToGraphQL
}<commit_msg>fix(list): allow requesting __typename on list query<commit_after>const getNestedField = require('./getNestedField')
const fieldsMap = require('./fieldsMap')

const reqFieldsList = reqFieldsObj => {
  return Object.keys(reqFieldsObj).filter(x => fieldsMap[x])
}

// This could be further optimised by just fetching requested sub fields
const getSourceFields = requestedFields => {
  return reqFieldsList(requestedFields).reduce((agg, x) => [
      ...agg,
      ...fieldsMap[x].fields,
    ], [])
}

const getGqlFieldValue = (gqlField, esDoc) => fieldsMap[gqlField].resolveValues(
  fieldsMap[gqlField].fields.map(esField => getNestedField(esDoc._source, esField))
)

const mapDocToGraphQL = (doc, requestedFields) => {
  return reqFieldsList(requestedFields).reduce((agg, x) => ({
    ...agg,
    [x]: getGqlFieldValue(x, doc)
  }), {})
}

module.exports = {
  getSourceFields,
  mapDocToGraphQL
}
{'fraction_non_alphanumeric': 0.15906362545018007, 'fraction_numerical': 0.0, 'mean_word_length': 4.465573770491804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '7057328', 'n_tokens_mistral': 532, 'n_tokens_neox': 532, 'n_words': 106}