import { InjectionToken, Scope, createModule } from "graphql-modules";
import { loadFilesSync } from "@graphql-tools/load-files";
import { resolvers } from "./resolvers";
import { join } from "node:path";
import DataLoader from "dataloader";
import { ExampleModule } from "./module-types";

export const EXAMPLE_DATA_LOADER = new InjectionToken("EXAMPLE_DATA_LOADER");

export const exampleModule = createModule({
  id: "example-module",
  dirname: __dirname,
  typeDefs: loadFilesSync(join(__dirname, "./typedefs/*.graphql")),
  providers: [
    {
      provide: EXAMPLE_DATA_LOADER,
      scope: Scope.Operation,
      useFactory: () =>
        new DataLoader<string, ExampleModule.World>(async (keys) => {
          return keys.map(() => {
            return { x: Math.random(), y: Math.random() };
          });
        }),
    },
  ],
  resolvers,
});
