We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to configure GraphQL subscriptions using existing express server.
But seems that there is some kind of conflict. Error thrown in graphiql console:
graphiql
WebSocket connection to 'ws://localhost:3000/subscriptions' failed: Connection closed before receiving a handshake response
When using new server. There is no error.
Here the graphQL configuration I've used: this.setSameServer() - uses nest http server instance. this.setDifferentServer() - uses new http instance.
this.setSameServer()
this.setDifferentServer()
import { MiddlewareConsumer, Module, HttpServer, Inject, NestModule, OnModuleDestroy, } from '@nestjs/common'; import { AppController } from 'app.controller'; import { AppService } from 'app.service'; import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'; import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql'; import { AuthorResolver } from 'author.resolver'; import { SubscriptionServer } from 'subscriptions-transport-ws'; import { execute, subscribe } from 'graphql'; import { createServer } from 'http'; import { HTTP_SERVER_REF } from '@nestjs/core'; @Module({ imports: [GraphQLModule, AuthorResolver], controllers: [AppController], providers: [ { provide: 'SUBSCRIPTION_SERVER', useFactory: () => { const server = createServer(); return new Promise(resolve => server.listen(88, () => resolve(server))); }, }, AppService, ], }) export class AppModule implements NestModule, OnModuleDestroy { private subscriptionServer: SubscriptionServer; private subscriptionPort: number; private wsServer: HttpServer; constructor( private readonly graphQLFactory: GraphQLFactory, @Inject(HTTP_SERVER_REF) private readonly httpServerRef: HttpServer, @Inject('SUBSCRIPTION_SERVER') private readonly ws: HttpServer, ) { this.setSameServer(); //this.setDifferentServer(); } private setSameServer() { this.wsServer = this.httpServerRef.getHttpServer(); this.subscriptionPort = 3000; } private setDifferentServer() { this.wsServer = this.ws; this.subscriptionPort = 88; } public configure(consumer: MiddlewareConsumer) { const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql'); const schema = this.graphQLFactory.createSchema({ typeDefs }); const route = '/graphql'; const routeIDE = '/graphiql'; const routeSubs = '/subscriptions'; const middlewareIDE = graphiqlExpress({ endpointURL: route, subscriptionsEndpoint: 'ws://localhost:' + this.subscriptionPort + routeSubs, }); const middleware = graphqlExpress(req => ({ schema, rootValue: req, debug: false, })); consumer.apply(middleware).forRoutes(route); consumer.apply(middlewareIDE).forRoutes(routeIDE); this.subscriptionServer = new SubscriptionServer( { execute, subscribe, schema, }, { server: this.wsServer, path: routeSubs, }, ); } public onModuleDestroy() { this.subscriptionServer.close(); } }
Used these issues for help: #500 nestjs/graphql#6
And full repo if you want to reproduce: https://github.com/ph55/nest-graphql-subscriptions
The text was updated successfully, but these errors were encountered:
Please, use @nestjs/graphql repository to report this issue.
@nestjs/graphql
Sorry, something went wrong.
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.
No branches or pull requests
Trying to configure GraphQL subscriptions using existing express server.
But seems that there is some kind of conflict.
Error thrown in
graphiql
console:When using new server. There is no error.
Here the graphQL configuration I've used:
this.setSameServer()
- uses nest http server instance.this.setDifferentServer()
- uses new http instance.Used these issues for help:
#500
nestjs/graphql#6
And full repo if you want to reproduce:
https://github.com/ph55/nest-graphql-subscriptions
The text was updated successfully, but these errors were encountered: