Nestjs graphql gateway

Nestjs graphql gateway DEFAULT


Apollo Federation offers a means of splitting your monolithic GraphQL server into independent microservices. It consists of two components: a gateway and one or more federated microservices. Each microservice holds part of the schema and the gateway merges the schemas into a single schema that can be consumed by the client.

To quote the Apollo docs, Federation is designed with these core principles:

  • Building a graph should be declarative. With federation, you compose a graph declaratively from within your schema instead of writing imperative schema stitching code.
  • Code should be separated by concern, not by types. Often no single team controls every aspect of an important type like a User or Product, so the definition of these types should be distributed across teams and codebases, rather than centralized.
  • The graph should be simple for clients to consume. Together, federated services can form a complete, product-focused graph that accurately reflects how it’s being consumed on the client.
  • It’s just GraphQL, using only spec-compliant features of the language. Any language, not just JavaScript, can implement federation.
Warning Apollo Federation currently does not support subscriptions.

In the next example, we'll set up a demo application with a gateway and two federated endpoints: a Users service and a Posts service.

Federated example: Users#

First, install the optional dependency for federation:

Schema first#

The User service has a simple schema. Note the directive: it tells the Apollo query planner that a particular instance of User can be fetched if you have its . Also, note that we extend the type.

Our resolver has one extra method: . It's called by the Apollo Gateway whenever a related resource requires a User instance. We'll see an example of this in the Posts service later on. Please note the decorator.

Finally, we hook everything up in a module together with a . This module accepts the same options as the regular .

Code first#

Code first federation is very similar to regular code first GraphQL. We simply add some extra decorators to the entity.

Our resolver has one extra method: . It's called by the Apollo Gateway whenever a related resource requires a instance. We'll see an example of this in the Posts service later on. Please note the decorator.

Finally, we hook everything up in a module together with a . This module accepts the same options as the regular .

Federated example: Posts#

Our Post service serves aggregated posts via a query, but also extends our type with

Schema first#

The Posts service references the User type in its schema by marking it with the keyword. It also adds one property to the User type. Note the directive used for matching instances of User, and the directive indicating that the field is managed elsewhere.

Our resolver has one method of interest here: . It returns a reference containing and any additional properties your application needs to resolve the reference, in this case only an . The is used by the GraphQL Gateway to pinpoint the microservice responsible for the User type and request the instance. The Users service discussed above will be called on the method.

The Posts service has virtually the same module, but is included below for the sake of completeness:

Code first#

We will need to create a class representing our entity. Even though it lives in another service, we will be using and extending it. Note the and directives.

We create the resolver for our extension on the entity as follows:

We also need to create our entity:

And its resolver:

And finally, tie it together in a module. Note the schema build options, where we specify that is an outside type.

Federated example: Gateway#

First, install the optional dependency for the gateway:

Our gateway only needs a list of endpoints and will auto-discover the schemas from there. Therefore it is the same for code and schema first, and the code for our gateway is very short:

Hint Apollo recommends that you don't rely on the service discovery in a production environment but use their Graph Manager instead.

Sharing context#

You can customize the requests between the gateway and federated services using a build service. This allows you to share context about the request. You can easily extend the default and implement one of the hooks. Please refer to Apollo Docs on for more information about the possibilities.

Async configuration#

Both the Federation and Gateway modules support asynchronous initialization using the same that's documented in Quick start.


Federation: Export `ApolloGateway` #1603

@jmcdo29 That to me sounds like the best option to go with.

I was about to just implement the preliminary idea of it and instantly saw that everything is so tied to Apollo. I'm sort of over Apollo at the moment. Their team is so slow to implement things that 3rd party OSS guys are coming up with as well as they're really pushing people towards their managed services and our organization doesn't like to be tied down.

Projects like, (stitching is alive and well again due to Apollo Federation not being so great), the list can go on.

I tried using + and it felt so hacky and I wasn't even sure if a future release will break it. I end up resorting in my gateway not even using :-. ATM you have to use to do cool things to a schema. I really want to use code first GraphQL in nest.js to create a schema, then build my server manually if I have to.

Nearly all GraphQL servers at this moment take in a schema and it's good to go.

Some require and a resolver map. (which you can just use to get these things if you really need them):

So a utility module without any server marriage would come in handy to use 3rd party server integrations. It'd make the entire Nest.js GraphQL ecosystem that much better :)

  1. 2001 dodge intrepid transmission
  2. Within the wires
  3. Lien miller funeral home
  4. Bucky barnes birthday


NestJS GraphQL Apollo Federation extension. You keep using @nestjs/graphql for all other steps

NPM VersionLicenseCode SizeTop LanguageTop Language


$ yarn install nestjs-graphql-gateway

Setup federated service

workspaceName:'GRAPHQL CQRS',
// Code first TypegraphQl
// optional orphaned types

Setup Gateway



This project is MIT licensed.


NestJs GraphQL Gateway (supports type-graphql)

NestJS GraphQL Apollo Federation extension. You keep using @nestjs/graphql for all other steps

NPM VersionLicenseCode SizeTop LanguageTop Language


$ yarn install nestjs-graphql-gateway

Setup federated service

import{Module}from'@nestjs/common';import{GraphqlDistributedModule}from'nestjs-graphql-gateway'; @Module({imports: [GraphqlDistributedModule.forRoot({typePaths: [path.join(process.cwd()+'/apps/service-auth/src','/**/*.graphql')],introspection: true,playground: {workspaceName: 'GRAPHQL CQRS',settings: {'editor.theme': 'light',},},context: (ctx)=>ctx,})]})exportclassAppModule{}// Code first TypegraphQl @Module({imports: [GraphqlDistributedModule.forRoot({autoSchemaFile: 'graphs/demo.gql',// optional orphaned typesbuildSchemaOptions: {orphanedTypes: [Tenant,TenantMember,User],},context: (ctx)=>ctx,})]})exportclassAppModule{}

Setup Gateway

import{Module}from'@nestjs/common';import{GraphqlDistributedGatewayModule}from'nestjs-graphql-gateway'; @Module({imports: [GraphqlDistributedGatewayModule.forRoot({subscriptions: false,path: '/graphql',context: context=>context,serviceList: [{name: 'auth',url: 'http://localhost:1000/graphql'},{name: 'user',url: 'http://localhost:2000/graphql'},// more services],buildService({ url }){returnnewHeadersDatasource({ url });},}),]})exportclassAppModule{}


This project is MIT licensed.


Gateway nestjs graphql


provides basic federation support, with the intention of helping to

  • Plug into existing federated graphs, through references.
  • Create a federated relations/connections on types defined in other services.

These docs assume you have read

Base Type#

The simplest way to integrate with a federated graph is through references.

A reference is an object that looks like

The lets the gateway know which type is being referenced with additional fields that can be used to uniquely identify the type.

To reference a type in you must first create DTO that defines the base type.

Base Type#

The base type in its own service must be decorated with federated directives specifying its key.


Auto Generated Resolver#

When using the module add the option that will use to automatically expose add a decorator and method that the gateway can use.



The should be the field you want to look up the DTO by.

Manual Resolver#

If you want to manually define your resolver pass in the option to the .



The should be the field you want to look up the DTO by.

App Module#

This app module must also use the in order for the base type to be resolved by the gateway.


Reference Base Type#

In a separate service from the one defining the base type above, we can use Apollo Federation to extend that base type.

To do this with you must create a type that extends the base type contained in some other graphql service.


The type name must be the same name as the type it extends in the graph.

For example


@Reference Decorator#

To reference a type defined in another service you can use the decorator.

When using the decorator will NOT look up the relation through the , instead return a reference type like the one described above.


In the above example we provided the keys which look like the following

Which will map the to the field in the reference type.

Assuming you have the following

The reference type would be


Now that we have added the decorator the resolver will automatically add the reference to the graphql type when using or

  • NestjsQueryGraphQLModule
  • CRUDResolver



Federated Relations#

Another common use case is to add to a federated type from another service.

Lets continue with the example used above. We have add a reference to the but now lets add subTasks to the .


The first step is to create a . The is a special type of that allows looking up relations without defining them in your entity.


In this example we inject a service that will be used to look up relations. The method is used to filter relations when or is called.

Add the Connection#

Next we add the connection to the .


The name of the relation should match the name of the relation defined by your .


The same pattern applies when you have a single relation and use the decorator.


Federation Resolver#

Next we set up our resolver that exposes the relations in the schema. As with other resolvers you can use the or define your own .

  • NestjsQueryGraphQLModule
  • FederationResolver

When using the set the of the resolver to , and specify the .

Also, add the to the option to make it available for nest to inject the service into the auto-generated resolver.


When manually defining the resolver extend the .

The this will not set up any queries or mutations in the graph. Instead, it is used set up the reading of relations for a type that originates from another service.


Complete Example#

To see a complete example checkout

Edit this page

Add Authentication to a NestJS GraphQL API - Building the BEEHIVE Platform with BEEMAN.DEV - BLS004

Then she rolled down the window, about to say something, instead smiled and raised it again. The car drove away and I watched her for a few more minutes. For today the magic is over, but it's not gone forever.

Now discussing:

Of him on his knees. She continued to selflessly suck the member of the Caucasian, not forgetting to lick the eggs and massage the prostate, for convenience Zurab lifted his leg. Up on the bench, put one hand on the back of my beloved's head, adjusting the depth of immersion of the penis. At this time, the rest of the guys gathered in the hamam, with the exception of one who remained at the post, near.

250 251 252 253 254