MichalLytek / type-graphql

Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
https://typegraphql.com
MIT License
7.98k stars 674 forks source link

The types returned by 'subscribe(...)' are incompatible between these types. #1601

Closed AaronNGray closed 5 months ago

AaronNGray commented 6 months ago

Describe the Bug I am getting the following issue with both 2.0.0-beta.1 and 2.0.0-beta.4

I managed to solve the issue and had a working solution, but on retrying the solution the package-lock.json did not seem to manage to save the solution !!!

TypeScript compile time error :-

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:113:9 - error TS2322: Type 'import("C:/Users/aaron/Development/Feed/apollo-v4-check/server/node_modules/graphql-subscriptions/dist/pubsub").PubSub' is not assignable to type 'import("C:/Users/aaron/Development/Feed/apollo-v4-check/server/node_modules/type-graphql/build/typings/typings/subscriptions").PubSub'.
  The types returned by 'subscribe(...)' are incompatible between these types.
    Property '[Symbol.asyncIterator]' is missing in type 'Promise<number>' but required in type 'AsyncIterable<unknown>'.

113         pubSub: pubsub
            ~~~~~~

  node_modules/typescript/lib/lib.es2018.asynciterable.d.ts:38:5
    38     [Symbol.asyncIterator](): AsyncIterator<T>;
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    '[Symbol.asyncIterator]' is declared here.
  node_modules/type-graphql/build/typings/schema/build-context.d.ts:26:5
    26     pubSub?: PubSub;
           ~~~~~~
    The expected type comes from property 'pubSub' which is declared here on type 'BuildSchemaOptions'

    at createTSError (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:1077:36)
    at Object.compile (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\aaron\Development\Feed\apollo-v4-check\server\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
  diagnosticCodes: [ 2322 ]

non yoga based.

    ~~~
    import { PubSub } from 'graphql-subscriptions';
    import { buildSchema } from "type-graphql";
    ~~~
    const pubsub = new PubSub();

    const schema = await buildSchema({
        resolvers: Resolvers,
        validate: false,
        pubSub: pubsub
    })
    ~~~

To Reproduce A quick guide how to reproduce the bug. You can paste here code snippets or even better, provide a link to the repository with minimal reproducible code example.

I will provide a working and non working examples tomorrow. I will probably have to whittle the main code down and zip everything up including the node_modules directory for the working version as the package-lock.json file does not seem to be working !

Expected Behavior A clear and concise description of what you expected to happen.

@apollo/server@4.9.5 v @apollo/server@4.10.0 + type-graphql@2.0.0-beta.4 v graphql-subscriptions@2.0.0 type system to be consistent

Logs If applicable, add some console logs to help explain your problem. You can paste the errors with stack trace that were printed when the error occurred.

Environment (please complete the following information):

Additional Context npm ls -all working apollo-v4-server.ls.npm.txt not working apollo-v4-check-server.ls.npm.txt

Add any other context about the problem here.

AaronNGray commented 6 months ago

Looking at https://github.com/dotansimha/graphql-yoga/blob/main/packages/subscription/src/create-pub-sub.ts#L36 and https://github.com/apollographql/graphql-subscriptions/blob/master/src/pubsub.ts#L8 subscription methods it might need an adapter for supporting both ?

type-graphql@1.1.1 used to support graphql-subscriptions@2.0.0 right out of the box. But type-graphql@2.0.0-beta.1 does not seem to anymore. The problem seems to be there is no type-graphql@2.0.0-beta.1 tag on this repo. So I cannot see what is going wrong here and why it used to work ?

I will produce a general example with type-graphql@1.1.1 and graphql-subscriptions@2.0.0 and another with type-graphql@2.0.0-beta.1 (the one that used to work, and try this with my working node_modules zip) and another with type-graphql@2.0.0-beta.4.

AaronNGray commented 6 months ago

@MichalLytek - It looks to me like you v2.0.0's should really be either a type-graphql-yoga or a type-graphql-apollo or the straight type-graphql should still work with graphql-subscriptions, or there should be adapters, or an adaptive typing and calling ?

AaronNGray commented 6 months ago

Okay, it looks like you overwrote/republished the NPM type-graphql@2.0.0-beta.1 with a different distribution. This broke my code the old one is based on graphql-subscription the newer one on @yoga/subscription. To verify this I copied the type-graphql from my working old test repo to another one freshly npm install'ed and that one started working.

AaronNGray commented 6 months ago

Right okay type-graphql@2.0.0-beta.3 uses graphql-subscriptions@2.0.0 again and works with a cast resolvers to NonEmptyArray !!

Okay I can hopefully move forward with this now, but please bear in mind my previous comments above.

MichalLytek commented 6 months ago

graphql-subscriptions are outdated and should not be used anymore. Neither internally by TypeGraphQL, nor by your code.

Migration to yoga pubsub, even with redis event target are really easy and should be favored instead of writing adapter for sticking with old graphql-subscriptions package.

I am getting the following issue with both 2.0.0-beta.1 and 2.0.0-beta.4

Only the beta 4 uses the new yoga-based subscriptions.

AaronNGray commented 5 months ago

@MichalLytek - Usage of graphql-subscriptions against @graphql-yoga/subscription :- https://npmcharts.com/compare/graphql-subscriptions,@graphql-yoga/subscription?interval=30 https://npmcharts.com/compare/graphql-subscriptions,@graphql-yoga/subscription?interval=120

andreidiaconescu commented 5 months ago
MichalLytek commented 5 months ago

i guess the issue comes from the fact that, an npm package with a version was overwritten/republished, but the npm version was not increased, (in my case type-graphql@2.0.0-beta.3) , which broke code that was already working.

This is not possible in npm. You can only unlist selected version in 24h after publishing and there's no way to republish it then, not mentioning overriding with other package data.

MichalLytek commented 5 months ago

Closing this one as in beta 6 there's a change in subscription lib and should be no more issues with types, etc.

AaronNGray commented 5 months ago

@MichalLytek - Do I still have support for graphql-subscriptions ? Or do I have to folk ?

I have two examples I would like to have them both work :-

https://github.com/AaronNGray/vite-js-apollo-v4-graph-ws-subscription-ssl-example https://github.com/AaronNGray/vite-js-apollo-v4-graph-ws-yoga-subscription-example

MichalLytek commented 5 months ago

graphql-subscriptions can work if you write an adapter for the new PubSub interface.

However, it makes no sense to still use graphql-subscriptions as migrating to yoga subscription or other solution you find is quite easy. I did it in examples and tests while changing the core.

https://typegraphql.com/docs/next/migration-guide.html#subscriptions