Skip to content

Mercurius-codegen throw error: error TS2304: Cannot find name '_Service'. #82

Open
@bkjp

Description

@bkjp

I am using @mercuriusjs/federation and mercurius-codegen to build a subgraph. I follow implementation directed by the doc. But always get the following errors

src/__generated-types__.ts:209:33 - error TS2304: Cannot find name '_Service'.

209   _Service: ResolverTypeWrapper<_Service>;
                                    ~~~~~~~~

src/__generated-types__.ts:223:13 - error TS2304: Cannot find name '_Service'.

223   _Service: _Service;
                ~~~~~~~~

src/__generated-types__.ts:277:19 - error TS2304: Cannot find name 'Query_entitiesArgs'.

277     RequireFields<Query_entitiesArgs, "representations">
                      ~~~~~~~~~~~~~~~~~~

src/__generated-types__.ts:356:52 - error TS2304: Cannot find name '_Service'.

356     sdl?: LoaderResolver<Maybe<Scalars["String"]>, _Service, {}, TContext>;

In my code typescript is not angry anywhere.
Here is my server.ts file

import Fastify, { FastifyRequest, FastifyReply } from "fastify";
import mercurius from "mercurius";
import mercuriusCodegen from "mercurius-codegen";
import { buildFederationSchema } from "@mercuriusjs/federation";
//
import { userTypeDefs } from "./user/user.schema";
import { userResolvers } from "./user/user.resolvers";

//###########################################################################

const app = Fastify();

const PORT = 4001;

const buildContext = async (req: FastifyRequest, _reply: FastifyReply) => {
  return {
    authorization: req.headers.authorization,
  };
};

type PromiseType<T> = T extends PromiseLike<infer U> ? U : T;

declare module "mercurius" {
  interface MercuriusContext
    extends PromiseType<ReturnType<typeof buildContext>> {}
}

app.register(mercurius, {
  schema: buildFederationSchema(userTypeDefs, {
    isGateway: false,
  }),
  resolvers: userResolvers,
  context: buildContext,
});

mercuriusCodegen(app, {
  // Commonly relative to your root package.json
  targetPath: "./src/__generated-types__.ts",
  // operationsGlob: "./src/graphql/operations/*.gql",
  // codegenConfig: {
  //   loadersCustomParentTypes: {
  //     Human: "never",
  //   },
  // },
}).catch(console.error);

// Start server
app.get("/", async function (req, reply) {
  const query = "{ _service { sdl } }";
  return app.graphql(query);
});

app.listen({ port: PORT });

I don't know what is going wrong there. I cannot modify generated-types.ts file since it is generated automatically when I start the server.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions