<commit_before>import { logger } from "@truffle/db/logger";
const debug = logger("db:meta:interface");

import gql from "graphql-tag";
import { print } from "graphql/language/printer";
import { GraphQLSchema, DocumentNode, ExecutionResult, execute } from "graphql";
import { ApolloServer } from "apollo-server";
import type TruffleConfig from "@truffle/config";

import type { Collections } from "./collections";
import type { Workspace } from "./data";
import * as Pouch from "./pouch";
import Configstore from "configstore";
import * as path from "path";

export interface Db<_C extends Collections> {
  /**
   * Perform a query or mutation via GraphQL interface
   */
  execute(
    request: DocumentNode | string,
    variables: any
  ): Promise<ExecutionResult>;
}

export interface ConnectOptions<_C extends Collections> {
  directory?: string;
  adapter?: Pouch.Adapters.AdapterOptions;
}

export const forAttachAndSchema = <C extends Collections>(options: {
  attach: (options: Pouch.Adapters.AttachOptions) => Workspace<C>;
  schema: GraphQLSchema;
}) => {
  const { attach, schema } = options;

  const connect = (
    config: TruffleConfig | ConnectOptions<C> | undefined
  ): Db<C> => {
    // TODO: get rid of @truffle/config stuff and rename field to "directory"
    let options;
    if (config && "working_directory" in config) {
      // TruffleConfig case
      options = toConnectOptions(config as TruffleConfig);
    } else if (config && "directory" in config) {
      // ConnectOptions case
      options = config;
    } else {
      const configStore = new Configstore(
        "truffle",
        {},
        { globalConfigPath: true }
      );
      const truffleDataDirectory = path.dirname(configStore.path);
      options = {
        directory: truffleDataDirectory,
        adapter: (config || {}).adapter
      };
    }
    const workspace = attach(options);

    return {
      async execute(
        request: DocumentNode | string,
        variables: any = {}
      ): Promise<ExecutionResult> {
        const document =
          typeof request === "string"
            ? gql`
                ${request}
              `
            : request;
        const response = await execute(
          schema,
          document,
          null,
          { workspace },
          variables
        );

        if (response.errors) {
          debug("request %s", print(document));
          debug("errors %O", response.errors);
        }

        return response;
      }
    };
  };

  const serve = (config: TruffleConfig | ConnectOptions<C> | undefined) => {
    // TODO: get rid of @truffle/config stuff and rename field to "directory"
    let options;
    if (config && "working_directory" in config) {
      // TruffleConfig case
      options = toConnectOptions(config as TruffleConfig);
    } else if (config && "workingDirectory" in config) {
      // ConnectOptions case
      options = config;
    } else {
      const configStore = new Configstore(
        "truffle",
        {},
        { globalConfigPath: true }
      );
      const truffleDataDirectory = path.dirname(configStore.path);
      options = {
        workingDirectory: truffleDataDirectory,
        adapter: (config || {}).adapter
      };
    }
    const workspace = attach(options);

    return new ApolloServer({
      tracing: true,
      schema,
      context: { workspace }
    });
  };

  return { connect, serve };
};<commit_msg>Update some missed references to workingDirectory<commit_after>import { logger } from "@truffle/db/logger";
const debug = logger("db:meta:interface");

import gql from "graphql-tag";
import { print } from "graphql/language/printer";
import { GraphQLSchema, DocumentNode, ExecutionResult, execute } from "graphql";
import { ApolloServer } from "apollo-server";
import type TruffleConfig from "@truffle/config";

import type { Collections } from "./collections";
import type { Workspace } from "./data";
import * as Pouch from "./pouch";
import Configstore from "configstore";
import * as path from "path";

export interface Db<_C extends Collections> {
  /**
   * Perform a query or mutation via GraphQL interface
   */
  execute(
    request: DocumentNode | string,
    variables: any
  ): Promise<ExecutionResult>;
}

export interface ConnectOptions<_C extends Collections> {
  directory?: string;
  adapter?: Pouch.Adapters.AdapterOptions;
}

export const forAttachAndSchema = <C extends Collections>(options: {
  attach: (options: Pouch.Adapters.AttachOptions) => Workspace<C>;
  schema: GraphQLSchema;
}) => {
  const { attach, schema } = options;

  const connect = (
    config: TruffleConfig | ConnectOptions<C> | undefined
  ): Db<C> => {
    // TODO: get rid of @truffle/config stuff and rename field to "directory"
    let options;
    if (config && "working_directory" in config) {
      // TruffleConfig case
      options = toConnectOptions(config as TruffleConfig);
    } else if (config && "directory" in config) {
      // ConnectOptions case
      options = config;
    } else {
      const configStore = new Configstore(
        "truffle",
        {},
        { globalConfigPath: true }
      );
      const truffleDataDirectory = path.dirname(configStore.path);
      options = {
        directory: truffleDataDirectory,
        adapter: (config || {}).adapter
      };
    }
    const workspace = attach(options);

    return {
      async execute(
        request: DocumentNode | string,
        variables: any = {}
      ): Promise<ExecutionResult> {
        const document =
          typeof request === "string"
            ? gql`
                ${request}
              `
            : request;
        const response = await execute(
          schema,
          document,
          null,
          { workspace },
          variables
        );

        if (response.errors) {
          debug("request %s", print(document));
          debug("errors %O", response.errors);
        }

        return response;
      }
    };
  };

  const serve = (config: TruffleConfig | ConnectOptions<C> | undefined) => {
    // TODO: get rid of @truffle/config stuff and rename field to "directory"
    let options;
    if (config && "working_directory" in config) {
      // TruffleConfig case
      options = toConnectOptions(config as TruffleConfig);
    } else if (config && "directory" in config) {
      // ConnectOptions case
      options = config;
    } else {
      const configStore = new Configstore(
        "truffle",
        {},
        { globalConfigPath: true }
      );
      const truffleDataDirectory = path.dirname(configStore.path);
      options = {
        directory: truffleDataDirectory,
        adapter: (config || {}).adapter
      };
    }
    const workspace = attach(options);

    return new ApolloServer({
      tracing: true,
      schema,
      context: { workspace }
    });
  };

  return { connect, serve };
};
{'fraction_non_alphanumeric': 0.11397911832946636, 'fraction_numerical': 0.0, 'mean_word_length': 2.5081383519837233, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, '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': '1862935', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1866, 'n_words': 541}