Nest: Multiple graphql endpoints sharing schema types and resolver modules

Created on 5 Jan 2018  路  5Comments  路  Source: nestjs/nest

I'm submitting a...

[X] Feature request

What is the motivation / use case for changing the behavior?

I have been playing around with nest this morning and was trying to get an instance with multiple graphql endpoints. The ideas was to have a user endpoint with limited schema scope and the admin endpoint with an extended one

I based this on https://github.com/nestjs/nest/tree/master/examples/12-graphql-apollo and you can follow me through here https://github.com/makstr/nestjs-multiple-endpoints-mergeTypesByPaths

yarn install
yarn dev

user endpoint

http://localhost:3000/graphql
http://localhost:3000/graphiql

query allCats {
  getCats{
    id
    name
  }
}

admin endpoint

http://localhost:3000/graphqladmin
http://localhost:3000/graphiqladmin

query allCats {
  getCats{
    id
    name
  }
}

query allDogs {
  getDogs{
    id
    name
  }
}

so on top of the "cats" module I created a sibling "dogs" module with the following directory structure:

  • src

    • user

  • - - cats

    • admin

  • - - dogs

then the src/app.module.ts imports two modules UserModule and AdminModule

import {
  Module,
  NestModule,
} from '@nestjs/common';

import { UserModule } from './user.module';
import { AdminModule } from './admin.module';

@Module({
  imports: [
    //
    // UserModule, 
    AdminModule
  ]
})
export class ApplicationModule {}

https://github.com/makstr/nestjs-multiple-endpoints-mergeTypesByPaths/blob/master/src/user.module.ts

https://github.com/makstr/nestjs-multiple-endpoints-mergeTypesByPaths/blob/master/src/admin.module.ts

user.module imports cats and admin.module import cats and dog and I changed the glob patterns to include the appropriate schemas only in each module respectively

Current behavior

  • both user and admin modules and their endpoints work as expected when launched individually
  • when the user.module is not imported the in the app.module admin endpoint behaves as expected and can run both 'cats' and 'dogs' queries.
  • when the admin.module is not imported the in the app.module user endpoint behaves as expected and can run 'cats' queries.
  • I can not get both modules to run simultaneously. I am getting errors that Query.getDogs defined in resolvers, but not in schema. Which makes me think the graphqlfactory can only handle one set of resolvers.

Environment


Nest version: 4.5.0


For Tooling issues:
- Node version: v8.1.2  
- Platform:  Mac 

Others:

question 馃檶

All 5 comments

Hi @makstr,

 I changed the glob patterns to include the appropriate schemas only in each module respectively

The GraphQLFactory creates resolvers by going through each module, not only these available in the current scope. For example, in the UserModule the GraphQLFactory will create resolvers that include DogsModule as well.

Allright, will need to work with that. Thank you!

Hi @makstr

Do you found a solution because i have the same problem.

I can not get both modules to run simultaneously. 
I am getting errors that Query.getDogs defined in resolvers, but not in schema. 
Which makes me think the graphqlfactory can only handle one set of resolvers.

I need to implement 2 GraphQL endpoint

Thanks

I'm wondering same thing , how to implement several graphQL endpoints with different schema within same nest app instance.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings