#Mercurius Federation: federationMetadata not present in MercuriusFederationDriverConfig

17 messages · Page 1 of 1 (latest)

analog forum
#

I am currently trying to build a GraphQL App where one Service references an entity from another Service. I tried to follow the steps on the docs of Federation for Mercurius (https://docs.nestjs.com/graphql/federation#code-first-3) .
The docs state to set federationMetadata: true, but I am getting this error:

src/app.module.ts:14:7 - error TS2345: Argument of type '{ driver: typeof MercuriusFederationDriver; autoSchemaFile: true; federationMetadata: boolean; }' is not assignable to parameter of type 'MercuriusDriverConfig'.
  Object literal may only specify known properties, and 'federationMetadata' does not exist in type 'MercuriusDriverConfig'.

14       federationMetadata: true,
         ~~~~~~~~~~~~~~~~~~

Setup:
NodeJs: 18.16.0
PNPM: 8.6.0

A minimal repo where I could reproduce the missing config entry can be found here
https://github.com/Loubis/mercurius-federation

I am not sure if I missed something important somewhere in the setup step.

GitHub

Contribute to Loubis/mercurius-federation development by creating an account on GitHub.

wise cloud
#

@analog forum - You do understand that federation is the merging of different schemas coming from different microservices, right? I don't see any microservices in your repro repo, so I get the feeling you might be misunderstanding something.

analog forum
#

Yes I understand. I try to resolve a reference of Entity A in B in my Resolver similar to the Example in the docs but without extending the Schema of Type A
A and B are handled by different indipendent Services

// Resolver for B
@Resolver(B)
export class BResolver {
  ...

  @ResolveField(() => A)
  a(@Parent() b: B) {
    return { __typename: 'A', id: b.aId };
  }
}

// Resolver for A
@Resolver(A)
export class AResolver {
  ...

  @ResolveReference()
  resolverReference(reference: { __typename: string; id: string }) {
    return this.aService.getAById(reference.id);
  }
}

So I try to resolve a reference of one Federation Service to another Federation Service. If I understand it correctly the Federation Gateway should resolve my returned Reference in BResolver { __typename: 'A', id: b.aId } and should call the ResolveReference annotated function in AResolver.
My Main problem is that that the resolverReference function is not called when I try to query the other Enity with something like this

query {
  getBByID(...) {
    id
    ...
    a {
      id
      someFieldInA
    }
  }
}
#

After checking the Docs I noticed that I am missing the federationMetadata: true, option in my GraphQLModule but when I set the option I am getting the error I stated in my first post. It seems like the federationMetadata option is missing in MercuriusFederationDriverConfig type

wise cloud
#

Ok. So, Federation is done via two Mercurius plugins, which you need to install.

#

Sorry. I'm wrong. Seems Nest does the schema merging itself.

#

Hmm...

analog forum
#

@mercuriusjs/gateway seems to be a dependency. If I remove @mercuriusjs/gateway from my dependencies I get an error:

Error: Cannot find module '@mercuriusjs/gateway'
Require stack:
- [...]/mercurius-federation/node_modules/.pnpm/@nestjs+mercurius@12.0.4_@apollo+subgraph@2.2.3_@nestjs+common@10.0.0_@nestjs+graphql@12.0.7__ppawscpxwlgiwq7rprtdii4cqm/node_modules/@nestjs/mercurius/dist/drivers/mercurius-gateway.driver.js
- [...]/mercurius-federation/node_modules/.pnpm/@nestjs+mercurius@12.0.4_@apollo+subgraph@2.2.3_@nestjs+common@10.0.0_@nestjs+graphql@12.0.7__ppawscpxwlgiwq7rprtdii4cqm/node_modules/@nestjs/mercurius/dist/drivers/index.js
- [...]/mercurius-federation/node_modules/.pnpm/@nestjs+mercurius@12.0.4_@apollo+subgraph@2.2.3_@nestjs+common@10.0.0_@nestjs+graphql@12.0.7__ppawscpxwlgiwq7rprtdii4cqm/node_modules/@nestjs/mercurius/dist/index.js
- [...]/mercurius-federation/dist/app.module.js
- [...]/mercurius-federation/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> ([...]/mercurius-federation/node_modules/.pnpm/@nestjs+mercurius@12.0.4_@apollo+subgraph@2.2.3_@nestjs+common@10.0.0_@nestjs+graphql@12.0.7__ppawscpxwlgiwq7rprtdii4cqm/node_modules/@nestjs/mercurius/dist/drivers/mercurius-gateway.driver.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
#

I am using the CodeFirst Approch btw.

wise cloud
#

Unfortunately, I don't have time to figure this out. I'm not a user of federation, so I'm poking in the dark with you.

acoustic garnet
#

I'm running into the same issue here. Aside from the option just not existing, this issue is talking about a problem which seems to be rooted in the same cause https://github.com/nestjs/graphql/issues/2157. This issue points to a closed PR on apollo essentially saying to upgrade to federation 2. It is explicitely stated in the nest docs that mercurius does not fully support federation 2, and when trying anyways i get the same error as in the issue (no valid service SDLs provided). The only other option i see is to switch to apollo completely, but in nest v9 apollo and fastify didnt get along. I dont know if that has changed yet

analog forum
#

For me it works now, but could not pin point what fixed the issue. I experimented with some dependency versions. I can send you later the dependencies with version. I am also using nest v9.

#

It worked after I prepared a minimal setup to reproduce the issue. I can push that later too.

acoustic garnet
#

That would be nice, thank you. In the minimal setup, could you remove all ^ and ~ so the versions are exact?

acoustic garnet
#

After prototyping a bit it seems (at least with Nest v10) apollo and fastify 4 together work again, which allowed me to use federation 2 and use the latest versions of all packages necessary.

analog forum
#

Sorry for the long wait. I just removed the ^ for @apollo/subgraph