import { TruffleDB } from "truffle-db";
import { ArtifactsLoader } from "./artifacts";
import { schema as rootSchema } from "truffle-db/schema";
import { Workspace, schema } from "truffle-db/workspace";
const tmp = require("tmp");
import {
  makeExecutableSchema
} from "@gnd/graphql-tools";
import { gql } from "apollo-server";

//dummy query here because of known issue with Apollo mutation-only schemas 
const typeDefs = gql`
  type Mutation {
    loadArtifacts: Boolean
  }
  type Query {
    dummy: String
  }
`;

const resolvers = {
  Mutation: {
    loadArtifacts: {
      resolve: async (_, args, { artifactsDirectory, contractsDirectory, db }, info) => {
        const compilationConfig = {
          contracts_directory: contractsDirectory,
          contracts_build_directory: tmp.dirSync({ unsafeCleanup: true }),
          all: true
        }
      
        const loader = new ArtifactsLoader(db, compilationConfig);
        loader.load();
      } 
    }
  }
}

export const loaderSchema = makeExecutableSchema({ typeDefs, resolvers });

import { TruffleDB } from "truffle-db";
import { ArtifactsLoader } from "./artifacts";
import { schema as rootSchema } from "truffle-db/schema";
import { Workspace, schema } from "truffle-db/workspace";
const tmp = require("tmp");
import {
  makeExecutableSchema
} from "@gnd/graphql-tools";
import { gql } from "apollo-server";

//dummy query here because of known issue with Apollo mutation-only schemas 
const typeDefs = gql`
  type Mutation {
    loadArtifacts: Boolean
  }
  type Query {
    dummy: String
  }
`;

const resolvers = {
  Mutation: {
    loadArtifacts: {
      resolve: async (_, args, { artifactsDirectory, contractsDirectory, db }, info) => {
        const tempDir = tmp.dirSync({ unsafeCleanup: true })
        const compilationConfig = {
          contracts_directory: contractsDirectory,
          contracts_build_directory: tempDir.name,
          all: true
        }
      
        const loader = new ArtifactsLoader(db, compilationConfig);
        await loader.load()
        tempDir.removeCallback();
        return true;
      } 
    }
  }
}

export const loaderSchema = makeExecutableSchema({ typeDefs, resolvers });

Use tmp directory for build in manual truffle compile
Use tmp directory for build in manual truffle compile
