enisdenjo / graphql-yoga-nestjs

Nest GraphQL GraphQL Yoga driver
https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-nestjs
25 stars 11 forks source link

fix!: Make @nestjs/graphql a peer dependency #49

Closed yusufkandemir closed 1 year ago

yusufkandemir commented 1 year ago

Fixes #48, but the problem is more than that.

This package pins @nestjs/graphql to a specific version, as a dependency: https://github.com/charlypoly/graphql-yoga-nestjs/blob/7182757556c60321ca39f4ffffaa813c27697141/package.json#L67

This guide instructs installing @nestjs/graphql: https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-nestjs#installation

But, by declaring it as a dependency, and furthermore pinning it to a specific version, you enforce users to use the same version. This makes it impossible to upgrade @nestjs/graphql freely. When using monorepo setups, the users have to uninstall @nestjs/core from their projects and depend on @graphql-yoga/nestjs to provide it. Here is the error happening when there are multiple @nestjs/graphql packages:

[Nest] 82791  - 12/07/2022, 10:34:33 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the YogaDriver. Please make sure that the "httpAdapterHost" property is available in the current context.

Potential solutions:
- Is GraphQLModule a valid NestJS module?
- If HttpAdapterHost is a provider, is it part of the current GraphQLModule?
- If HttpAdapterHost is exported from a separate @Module, is that module imported within GraphQLModule?
  @Module({
    imports: [ /* the Module containing HttpAdapterHost */ ]
  })

In a Yarn Workspaces setup, if I patch yarn.lock to remove @nestjs/graphql from @graphql-yoga/nestjs, it works fine, but the types rightfully fail. If I remove @nestjs/graphql from my dependencies, it works fine. I tried upgrading the code-first example project and it works fine. If I do yarn add @nestjs/graphql@10.1.6, it still works because the example project is not a monorepo. If I do yarn add @nestjs/graphql, which resolves to 10.1.7, it stops working and throws this error:

[Nest] 89366  - 12/07/2022, 10:50:02 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the YogaDriver. Please make sure that the "graphQlFactory" property is available in the current context.

Potential solutions:
- Is GraphQLModule a valid NestJS module?
- If GraphQLFactory is a provider, is it part of the current GraphQLModule?
- If GraphQLFactory is exported from a separate @Module, is that module imported within GraphQLModule?
  @Module({
    imports: [ /* the Module containing GraphQLFactory */ ]
  })

which is basically the same as the first error I sent, just for a different "provider".

It should be specified in peerDependencies instead, just like how NestJS does in Apollo and Mercurius drivers: https://github.com/nestjs/graphql/blob/5997d561f9abc17bfc444a10b71edbbc72ef817c/packages/apollo/package.json#L52 https://github.com/nestjs/graphql/blob/5997d561f9abc17bfc444a10b71edbbc72ef817c/packages/mercurius/package.json#L39

I upgraded the code-first example to NestJS v9. To reproduce the problem I've mentioned, just do the following:

$ yarn start
# should work fine

$ yarn add @nestjs/graphql # will install a different version

$ yarn start
# error