<commit_before>/* eslint-disable import/prefer-default-export */
import { GraphQLInt, GraphQLString } from 'graphql';

import PageType from './types/PageType';

const extractCursors = opaqueCursor => {
  let cursorData;
  if (opaqueCursor) {
    cursorData = Buffer.from(opaqueCursor, 'base64')
      .toString('ascii')
      .split('$');
  } else {
    cursorData = [new Date(), 0];
  }
  return cursorData;
};

export const createConnection = (resultType, resultModel, resolverFn) => {
  return {
    type: PageType(resultType),
    args: { first: { type: GraphQLInt }, after: { type: GraphQLString } },
    resolve: async (
      parent,
      { first = 10, after = '' },
      { viewer, loaders },
    ) => {
      const [cursorDate, cursorId] = extractCursors(after);
      const data = await resolverFn(cursorDate, cursorId, first);

      const queries = data.map(p => resultModel.gen(viewer, p.id, loaders));
      const dataSet = data.reduce((acc, curr) => {
        acc[curr.id] = curr;
        return acc;
      }, {});
      const results = await Promise.all(queries);
      const edges = results.map(p => ({ node: p }));
      const endCursor =
        edges.length > 0
          ? Buffer.from(
              `${new Date(
                dataSet[edges[edges.length - 1].node.id].time,
              ).toJSON()}$${edges[edges.length - 1].node.id}`,
            ).toString('base64')
          : null;

      const hasNextPage = edges.length === first;
      return { edges, pageInfo: { startCursor: null, endCursor, hasNextPage } };
    },
  };
};
<commit_msg>fix for null values in connections<commit_after>/* eslint-disable import/prefer-default-export */
import { GraphQLInt, GraphQLString } from 'graphql';

import PageType from './types/PageType';

const extractCursors = opaqueCursor => {
  let cursorData;
  if (opaqueCursor) {
    cursorData = Buffer.from(opaqueCursor, 'base64')
      .toString('ascii')
      .split('$');
  } else {
    cursorData = [new Date(), 0];
  }
  return cursorData;
};

export const createConnection = (resultType, resultModel, resolverFn) => {
  return {
    type: PageType(resultType),
    args: { first: { type: GraphQLInt }, after: { type: GraphQLString } },
    resolve: async (
      parent,
      { first = 10, after = '' },
      { viewer, loaders },
    ) => {
      const [cursorDate, cursorId] = extractCursors(after);
      const data = await resolverFn(cursorDate, cursorId, first);

      const queries = data.map(p => resultModel.gen(viewer, p.id, loaders));
      const dataSet = data.reduce((acc, curr) => {
        acc[curr.id] = curr;
        return acc;
      }, {});
      const results = await Promise.all(queries);
      const edges = results.map(p => ({ node: p }));
      let endCursor;
      try {
        endCursor =
          edges.length > 0
            ? Buffer.from(
                `${new Date(
                  dataSet[edges[edges.length - 1].node.id].time,
                ).toJSON()}$${edges[edges.length - 1].node.id}`,
              ).toString('base64')
            : null;
      } catch {
        endCursor = null;
      }

      const hasNextPage = edges.length === first;
      return { edges, pageInfo: { startCursor: null, endCursor, hasNextPage } };
    },
  };
};

{'fraction_non_alphanumeric': 0.14895577395577395, 'fraction_numerical': 0.006142506142506142, 'mean_word_length': 2.4069037656903767, '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': '5241287', 'n_tokens_mistral': 1018, 'n_tokens_neox': 995, 'n_words': 234}