Mercurius-codegen error Cannot find name '_Service

67 views Asked by At

I am implementing a graphql api subgraph using Mercuriusjs, @mercurius/federation and typescript by following their doc. But always getting error

Here are some parts of my code.

server.ts

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",
}).catch(console.error);

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

app.listen({ port: PORT });

user.schema.ts

import { gql } from "mercurius-codegen";

export const userTypeDefs = gql`
  type Query {
    _empty: String
  }

  extend type Query {
    user(id: ID!): User
    allUsers: [User]
  }

  type Mutation {
    createUser(createUserInput: CreateUserInput!): User
  }

  type User @key(fields: "id") {
    id: ID!
    username: String!
    password: String!
    email: String!
  }

  input CreateUserInput {
    username: String!
    password: String!
    email: String!
  }
`;

user.resolvers.ts

export const userResolvers: IResolvers = {
  Query: {
    allUsers: async () => await getAllUsers(),
    user: async (_, args) => await getUserById(args.id),
  },

  Mutation: {
    createUser: async (_, args) => {
      const userData = args.createUserInput;
      const createdUser = await registerUser(userData);
      return createdUser;
    },
  },

  // Resolver reference for entity User.
  User: {
    __resolveReference: (source) => {
      return getUserById(source.id);
    },
  },
};

When starting the server I get the following error

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>;

generated-types.ts is a generated file by mercurius-codegen.

It is very difficult for me to debug this, since the problem (I think) is related to the generated file by mercurius-codegen.

I like this library, but I remark that ther is a lack of tutorials for @mercurius/federation and typescript.

0

There are 0 answers