mercurius-js / mercurius-typescript

TypeScript usage examples and "mercurius-codegen" for Mercurius

Home Page:https://mercurius.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mercuriusCodegen is not a function

turkerdev opened this issue · comments

without that function it runs, but it doesnt generate generated.ts and i cant get intellisense in resolver.
also i ran your example, it works fine.
i use module as type in package.json, i dont know if that matters because you use cjs in your example

import fastify from "fastify";
import logger from "../utils/logger.js";
import mercurius from "mercurius";
import resolvers from "../graphql/resolvers/main.js";
import env from "../config/env.js";
import mercuriusCodegen, { loadSchemaFiles } from "mercurius-codegen";
import { buildSchema } from "graphql";
const server = fastify();
const { schema } = loadSchemaFiles("src/graphql/*.gql", {
    watchOptions: {
        enabled: env.NODE_ENV === "development",
        onChange(schema) {
            server.graphql.replaceSchema(buildSchema(schema.join("\n")));
            server.graphql.defineResolvers(resolvers);
            mercuriusCodegen(server, {
                targetPath: "./src/generated/graphql.ts",
            });
        },
    },
});
server.register(mercurius, {
    graphiql: env.NODE_ENV === "development",
    schema,
    resolvers,
});
// HERE!
mercuriusCodegen(server, {
    targetPath: "./src/generated/graphql.ts",
    watchOptions: {
        enabled: env.NODE_ENV === "development",
    },
});
export default server;

solved the problem by using

codegen.codegenMercurius()

but it was odd why it didnt work other way.

it's because of esModuleInterop, the main function is exported as .default and .codegenMercurius

image

anyways, I updated the examples to use the named export codegenMercurius