logaretm / villus

🏎 A tiny and fast GraphQL client for Vue.js
https://villus.dev
MIT License
790 stars 31 forks source link

SubscriptionForwarder Typescript issue with graphql-ws #186

Closed JarvisH closed 1 year ago

JarvisH commented 1 year ago

When using graphql-ws like so:

import { createClient } from 'graphql-ws'
const wsClient = createClient(...)
...
use: [
      handleSubscriptions(operation => {
        return {
          subscribe: obs => {
            wsClient.subscribe(
              {
                query: ...,
                variables: ...
              },
              obs
            )

            return {
              unsubscribe: () => {}
            }
          }
        }
      }),

The following Typescript error occurs for the obs argument:

Argument of type 'ObserverLike<StandardOperationResult<any>>' 
is not assignable to parameter of type 'Sink<ExecutionResult<any, ObjMap<unknown>>>'

It essentially boils down to this Villus interface:

interface ObserverLike<T> {
    next?: (value: T) => void;
    error?: (err: any) => void;
    complete?: () => void;
}

vs. this grapqhl-ws interface:

interface Sink<T = unknown> {
    next(value: T): void;
    error(error: unknown): void;
    complete(): void;
}

I can understand the difficulty of ensuring compatibility with all sorts of external libraries, on the other hand, if the properties in interface ObserverLike do not have to be optional it would be an easy fix.

logaretm commented 1 year ago

That makes sense. Even if someone are using some other implementation they could just provide a no op functions.