mercurius-js / mercurius-js/validation

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

Open
#82 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
31
Forks
6
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with server.ts, especially the buildFederationSchema setup and the mercuriusCodegen call that writes src/generated-types.ts. Reproduce the TypeScript errors, then inspect the generated definitions around _Service and Query_entitiesArgs; done means the generated file type-checks for this federation setup without manual edits.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.