graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Lack of typescript support in subscriptions configuration section (unable to set up graphql-ws)

Open Kasheftin opened this issue 3 years ago • 3 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

I have an issue when trying to set up graphql subscriptions using graphql-ws. I need to specify custom port (for being able serving wss/https using nginx) and append context with custom loaders (they use DataLoader under the hood). However documentation does not say anything about the port.

What's more important, the typing in subscriptions configuration section is missing. That's how my app.module.ts looks like:

@Module({
  imports: [
    ...
    GraphQLModule.forRootAsync({
      driver: ApolloDriver,
      imports: [TasksModule],
      inject: [TasksService],
      useFactory: (tasksService: TasksService) => ({
        autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
        context: () => createTaskLoaders(tasksService),
        subscriptions: { // no typescript support for the entire subscriptions section
          'graphql-ws': {
            anyOptionName: 'you can put anything here - it's not checked',
            onConnect: (context) => {
              context.extra.test = 'context is not typed as well'
            }
          },
          'any-other-library-does-not-matter-if-installed-or-not': true
        }
      })
    })
  ]
})

Describe the solution you'd like

It would be nice to have type safe in such a critical place as root level set up. Also, since subscriptions-transport-ws is going to be deprecated, it would be nice to have the documentation being focused on graphql-ws usage overall.

Teachability, documentation, adoption, migration strategy

No response

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

I'm stuck. I can not set up the basic subscription hello world using graphql-ws and serve it through wss with nginx.

Kasheftin avatar Aug 31 '22 06:08 Kasheftin

+1 on the lack of typing around graphql-ws. I actually just had a conversation over in the discussion forum for graphql-ws about this exact issue before I realized it was a lack of typing on the part of this repo: https://github.com/enisdenjo/graphql-ws/discussions/257#discussioncomment-3665653.

Nickersoft avatar Sep 16 '22 21:09 Nickersoft

Would you like to create a PR for this issue?

kamilmysliwiec avatar Sep 28 '22 08:09 kamilmysliwiec

I got it going with the following:

import { Context } from 'apollo-server-core'
import { Context as WSContext } from 'graphql-ws'
{
    ...,
    subscriptions: {
      'graphql-ws': {
        onConnect: (context: WSContext) => {
          return setupContext(context)
        },
        onDisconnect: (context: WSContext) => {
          //
        }
      }
    },
    context: async (context: Context<any>) => {
      return setupContext(context)
    }
}

Then a setupContext function which handles both ws requests and http requests... be aware the Context object is slightly different but the types should help from the imports as shown above

bneigher avatar Dec 02 '22 04:12 bneigher