This should do:-

import 'reflect-metadata';
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { devLogger } from './utils/constants';
import { createServer } from 'http';
import { getSchema } from './utils/schema';

const main = async () =>
{
    const app = express();
    const ws = createServer( app );
    const apolloServer = new ApolloServer( {
        schema: await getSchema(),

    } );

    apolloServer.applyMiddleware( { app } );
    apolloServer.installSubscriptionHandlers( ws );

    const PORT = process.env.PORT || 5000;
    ws.listen( PORT, async () =>
    {
        devLogger( `Server started on port ${ PORT }` );
    } );
};

main().catch( err => devLogger( err ) );
Answer from Aman Bhardwaj on Stack Overflow
🌐
Typegraphql
typegraphql.com › docs › subscriptions.html
Subscriptions · TypeGraphQL
However, oftentimes clients want to get updates pushed to them from the server when data they care about changes. To support that, GraphQL has a third operation: subscription. TypeGraphQL of course has great support for subscription, using the @graphql-yoga/subscription package created by The Guild.
🌐
GitHub
github.com › MichalLytek › type-graphql › blob › master › docs › subscriptions.md
type-graphql/docs/subscriptions.md at master · MichalLytek/type-graphql
However, oftentimes clients want to get updates pushed to them from the server when data they care about changes. To support that, GraphQL has a third operation: subscription. TypeGraphQL of course has great support for subscription, using the @graphql-yoga/subscription package created by The Guild.
Author   MichalLytek
Top answer
1 of 2
2

This should do:-

import 'reflect-metadata';
import express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { devLogger } from './utils/constants';
import { createServer } from 'http';
import { getSchema } from './utils/schema';

const main = async () =>
{
    const app = express();
    const ws = createServer( app );
    const apolloServer = new ApolloServer( {
        schema: await getSchema(),

    } );

    apolloServer.applyMiddleware( { app } );
    apolloServer.installSubscriptionHandlers( ws );

    const PORT = process.env.PORT || 5000;
    ws.listen( PORT, async () =>
    {
        devLogger( `Server started on port ${ PORT }` );
    } );
};

main().catch( err => devLogger( err ) );
2 of 2
1

You don't need to import SubscriptionServer from subscriptions-transport-ws. Here is a simple GraphQL server template for you.

server.ts

import "reflect-metadata";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import http from 'http';

const main = async () => {
  const app = express();
  const httpServer = http.createServer(app)

  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      // Insert your resolvers here
      resolvers: [],
      validate: false
    }),
    // Not required but if you want to change your path do it here. The default path is graphql if "subscriptions" field is not passed
    subscriptions: {
      path: "/subscriptions",
      onConnect: () => console.log("Client connected for subscriptions"),
      onDisconnect: () => console.log("Client disconnected from subscriptions")
    }
  });

  apolloServer.applyMiddleware({ app, cors: false });
  apolloServer.installSubscriptionHandlers(httpServer)

  httpServer.listen(4000, () => console.log("Server ready at http://localhost:4000"))
};

main().catch((err) => console.error(err));

resolver

import {
  Arg,
  Mutation,
  Resolver,
  Root,

  // Required for subscription to work
  Subscription, 
  PubSub,
  PubSubEngine
} from "type-graphql";

@Resolver()
export class MessageResolver {
  @Subscription({ topics: 'NOTIFICATIONS' })
  newNotification(@Root() payload: string) : string {
    return payload
  }

  @Mutation(() => String)
  async sendMessage(
    @Arg('name') name: string,
    @PubSub() pubsub: PubSubEngine
  ): Promise<string> {
    pubsub.publish('NOTIFICATIONS', name)
    return name
  }
}

Note To answer your question, it's highly likely that Typescript isn't reading the file where you wrote your subscription resolver, resulting in no GraphQL schema being created. Be sure to add the following lines to your tsconfig.json file. Sometimes, vscode auto imports paths and add them to this line.

  "include": [
    "./src/**/*.tsx",
    "./src/**/*.ts"
  ]
🌐
GitHub
github.com › MichalLytek › type-graphql › blob › master › website › versioned_docs › version-0.17.1 › subscriptions.md
type-graphql/website/versioned_docs/version-0.17.1/subscriptions.md at master · MichalLytek/type-graphql
However, oftentimes clients want to get updates pushed to them from the server when data they care about changes. To support that, GraphQL has a third operation: subscription. TypeGraphQL of course has great support for subscription, using the graphql-subscriptions package created by Apollo GraphQL.
Author   MichalLytek
🌐
GitHub
github.com › MichalLytek › type-graphql › blob › master › website › versioned_docs › version-0.17.0 › subscriptions.md
type-graphql/website/versioned_docs/version-0.17.0/subscriptions.md at master · MichalLytek/type-graphql
However, oftentimes clients want to get pushed updates from the server when data they care about changes. To support that, GraphQL has a third operation: subscription. TypeGraphQL of course has great support for subscription, using graphql-subscriptions package created by Apollo GraphQL.
Author   MichalLytek
🌐
Typegraphql
typegraphql.com › docs › 2.0.0-beta.4 › migration-guide.html
Migration Guide · TypeGraphQL
Previously, you could omit it and rely on the default one created by TypeGraphQL. The reason for this change is that @graphql-yoga/subscriptions package allows to create a type-safe PubSub instance via the generic createPubSub function, so you can add type info about the topics and params required while using .publish() method.
🌐
Apollo GraphQL
apollographql.com › docs › apollo-server › data › subscriptions
Subscriptions in Apollo Server - Apollo GraphQL Docs
Because subscription updates are usually pushed by the server (instead of polled by the client), they generally use the WebSocket protocol instead of HTTP.
🌐
Medium
medium.com › @doyun.kim3878 › graphql-subscriptions-in-2021-the-backend-b602b1fe6e15
GraphQL Subscriptions in 2021 (The Backend) | by Doyun Kim | Medium
September 23, 2021 - Official documentations never seem to keep their tutorials up-to-date, do they? Let’s say you want to set up graphql subscriptions on your express.js server using typescript (that’s why you’re here after all). You start by looking up the type-graphql documentation on subscriptions, which advises you to use apollo-server.
Find elsewhere
🌐
DEV Community
dev.to › gdsckiitdev › build-a-chat-app-with-graphql-subscriptions-typescript-part-1-2p70
Build a chat app with GraphQL Subscriptions & TypeScript: Part 1 - DEV Community
February 28, 2021 - That's a lot of new code so let's understand what we're doing here. Aside from GraphQL object types, TypeGraphQL also allows us to create GraphQL queries, mutations, and subscriptions in a REST controller type fashion.
🌐
NestJS
docs.nestjs.com › graphql › subscriptions
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
🌐
GitHub
github.com › MichalLytek › type-graphql › issues › 1638
Subscriptions: New Implementation Proposal · Issue #1638 · MichalLytek/type-graphql
February 24, 2024 - import 'reflect-metadata' import { createServer } from 'node:http' import { createYoga } from 'graphql-yoga' import { buildSchemaSync, Resolver, Query, Subscription, Arg, Root } from 'type-graphql' @Resolver() class SampleResolver { @Query(returns => Boolean) sample() { return true } @Subscription(returns => Number, { subscribe: async function *({ args }) { for (let i = args.from; i >= 0; i--) { await new Promise(resolve => setTimeout(resolve, 1000)) yield i } } }) countdown( @Root() payload: Number, @Arg('from', type => Number) from: number, ) { return payload } } function bootstrap() { const schema = buildSchemaSync({ resolvers: [ SampleResolver, ], pubSub: true as any, }) const yoga = createYoga({ schema }) const server = createServer(yoga) server.listen(4000, () => { console.info('Server is running on http://localhost:4000/graphql') }) } bootstrap()
Author   MichalLytek
🌐
Medium
medium.com › @imbharat420 › using-typegraphql-and-subscriptions-with-apollo-server-4-c98db335553d
Using TypeGraphQL and Subscriptions with Apollo Server 4 | by Bharat Singh | Medium
March 28, 2023 - type TSub = { schema: GraphQLSchema httpServer: http.Server } const subscriptionsServer = ({ schema, httpServer }: TSub) => { return new SubscriptionServer( { execute, subscribe, schema, }, { server: httpServer, path: '/graphql', } ) } That’s it! With TypeGraphQL and Apollo Server, adding subscriptions to your GraphQL API is easy and type-safe.
🌐
GraphQL
graphql.org › learn › subscriptions
Subscriptions | GraphQL
1 month ago - One important distinction to make with subscription operations is that a document may contain a number of different subscription operations with different root fields, but each operation must have exactly one root field.
🌐
Owengombas
owengombas.github.io › graphql-composer-decorators › intro › typegraphql-comparison.html
Differences with TypeGraphQL | graphql-composer-decorators
@Resolver() class Resolver { Subscription({ topics: "NOTIFICATION" }) mySubscription() { // ... } @Query() trigger(@PubSub() pubSub): Boolean { pubsub.publish("NOTIFICATION"); // ... } } Arguments specific to the GraphQL context (info, source, root, etc...) are injected via decorators with TypeGraphQL (@Root, @Source, @Infos, ...) which can make the declaration of your methods pretty huge.
🌐
GitHub
github.com › MichalLytek › type-graphql › discussions › 1018
@Subscription on Apollo v2 and v3 · MichalLytek/type-graphql · Discussion #1018
https://github.com/AaronNGray/fullstack-apollo-3.0-subscription-type-graphql-example https://github.com/AaronNGray/fullstack-apollo-3.0-subscription-type-graphql-example/blob/main/server/src/index.ts https://github.com/AaronNGray/fullstack-apollo-3.0-subscription-type-graphql-example/blob/main/server/src/types.ts · Beta Was this translation helpful? Give feedback. ... Something went wrong. ... All we need to do is create an instance of PubSub according to the package instructions and then provide it to the TypeGraphQL buildSchema options
Author   MichalLytek
🌐
GitHub
github.com › MichalLytek › typegraphql-prisma › discussions › 424
Generators for Subscriptions · MichalLytek/typegraphql-prisma · Discussion #424
import { Project, SyntaxKind } from "ts-morph"; import * as prettier from "prettier"; import { v4 as uuid } from "uuid"; import fs from "fs"; interface Props { /** Path of the resolver that you'd like to add a subscription method */ resolverFilePath: string; /** Where to save the generated custom subscription resolver */ outputFilePath: string; /** TypeGraphQL object type (which defines the resolver schema model) */ objectType: string; /** TypeGraphQL arguments type (which define the arguments for the resolver mutation method) */ argsType: string; /** TypeGraphQL type that will be pushed to th
Author   MichalLytek
🌐
GitHub
github.com › MichalLytek › type-graphql › blob › master › examples › redis-subscriptions › index.ts
type-graphql/examples/redis-subscriptions/index.ts at master · MichalLytek/type-graphql
// Build TypeGraphQL executable schema · const schema = await buildSchema({ // Array of resolvers · resolvers: [RecipeResolver], // Create 'schema.graphql' file with schema definition in current directory · emitSchemaFile: path.resolve(__dirname, "schema.graphql"), // Publish/Subscribe ·
Author   MichalLytek
🌐
Graphql-ruby
graphql-ruby.org › subscriptions › subscription_type.html
GraphQL - Subscription Type
Subscription is the entry point for all subscriptions in a GraphQL system. Each field corresponds to an event which may be subscribed to: · This type is the root for subscription operations, for example: