Skip to content

The open alternative to Salesforce, designed for AI.

3.1 KB
Raw Blame
import { ApolloLink, gql, type Operation } from '@apollo/client';
import { map } from 'rxjs';
import { logDebug } from '~/utils/logDebug';
import { logError } from '~/utils/logError';

import { isDefined } from 'twenty-shared/utils';
import { doNotTranslate } from 'twenty-shared/i18n';
import formatTitle from './formatTitle';

const getGroup = (collapsed: boolean) =>
  collapsed
    ? console.groupCollapsed.bind(console)
    : console.group.bind(console);

const parseQuery = (queryString: string) => {
  if (!queryString.trim()) {
    return [doNotTranslate('Generic'), ''];
  }

  const queryObj = gql`
    ${queryString}
  `;

  const { name } = queryObj.definitions[0] as any;
  return [name ? name.value : doNotTranslate('Generic'), queryString.trim()];
};

export const loggerLink = (getSchemaName: (operation: Operation) => string) =>
  new ApolloLink((operation, forward) => {
    const schemaName = getSchemaName(operation);
    operation.setContext({ start: Date.now() });

    const { variables } = operation;

    const operationType = (operation.query.definitions[0] as any).operation;
    const headers = operation.getContext().headers;

    const [queryName, query] = parseQuery(
      operation.query.loc?.source.body ?? '',
    );

    if (operationType === 'subscription') {
      const date = new Date().toLocaleTimeString();

      const titleArgs = formatTitle(operationType, schemaName, queryName, date);

      console.groupCollapsed(...titleArgs);

      if (Object.keys(variables).length !== 0) {
        logDebug('VARIABLES', variables);
      }

      logDebug('QUERY', query);

      console.groupEnd();

      return forward(operation);
    }

    return forward(operation).pipe(
      map((result) => {
        const time = Date.now() - operation.getContext().start;
        const errors = result.errors ?? result.data?.[queryName]?.errors;
        const hasError = Boolean(errors);

        try {
          const titleArgs = formatTitle(
            operationType,
            schemaName,
            queryName,
            time,
          );

          getGroup(!hasError)(...titleArgs);

          if (isDefined(errors)) {
            errors.forEach((err: any) => {
              logDebug(
                `%c${err.message}`,
                // oxlint-disable-next-line twenty/no-hardcoded-colors
                'color: #F51818; font-weight: lighter',
              );
            });
          }

          logDebug('HEADERS: ', headers);

          if (Object.keys(variables).length !== 0) {
            logDebug('VARIABLES', variables);
          }

          logDebug('QUERY', query);

          if (isDefined(result.data)) {
            logDebug('RESULT', result.data);
          }
          if (isDefined(errors)) {
            logDebug('ERRORS', errors);
          }

          console.groupEnd();
        } catch {
          // this may happen if console group is not supported
          logDebug(
            `${operationType} ${schemaName}::${queryName} (in ${time} ms)`,
          );
          if (isDefined(errors)) {
            logError(errors);
          }
        }

        return result;
      }),
    );
  });