ApolloServerPluginLandingPageLocalDefault从@apollo/server与NestJS GraphQLModule配置不兼容。

huangapple go评论86阅读模式
英文:

ApolloServerPluginLandingPageLocalDefault from @apollo/server is not compatible with NestJS GraphQLModule config

问题

I am migrating my NestJS application from Apollo v3 to Apollo v4, but I encountered a problem. Following the migration guide here I modified the import statement for the local landing page:

import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default'; //was 'apollo-server-core'
import { DenunciaDetallesModule } from './registry/denuncia-detalles/denuncia-detalles.module';

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      sortSchema: true,
      driver: ApolloDriver,
      playground: false,
      plugins: [ApolloServerPluginLandingPageLocalDefault()],
    }),
    DenunciaDetallesModule,
  ],
})
export class AppModule {}

However, when running the API, I get the following error:

src/app.module.ts:15:17 - error TS2322: Type 'ApolloServerPlugin<BaseContext>' is not assignable to type 'PluginDefinition'.
Types of property 'serverWillStart' are incompatible.
      Type '(service: GraphQLServerContext) => Promise<void | GraphQLServerListener>' is not assignable to type '(service: GraphQLServiceContext) => Promise<void | GraphQLServerListener>'.
        Types of parameters 'service' and 'service' are incompatible.
          Type 'GraphQLServiceContext' is missing the following properties from type 'GraphQLServerContext': cache, startedInBackground

15       plugins: [ApolloServerPluginLandingPageLocalDefault()],

I could just switch back to apollo-server-core, but I don't understand why the types are not compatible if I updated all packages and made a clean install. Here are my dependencies:

"dependencies": {
    "@apollo/server": "^4.7.4",
    "@nestjs/apollo": "^10.1.7",
    "@nestjs/class-validator": "^0.13.4",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/graphql": "^10.1.7",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.1.3",
    "@types/mssql": "^8.1.1",
    "apollo-server-core": "^3.12.0",
    "apollo-server-express": "^3.12.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "graphql": "^16.6.0",
    "joi": "^17.7.0",
    "mssql": "^9.1.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.11"
  }
英文:

I am migrating my NestJS application from Apollo v3 to Apollo v4, but I encountered a problem.
Following the migration guide here
I modified the import statement for the local landing page:

import { ApolloDriver, ApolloDriverConfig } from &#39;@nestjs/apollo&#39;;
import { Module } from &#39;@nestjs/common&#39;;
import { GraphQLModule } from &#39;@nestjs/graphql&#39;;
import { join } from &#39;path&#39;;
import { ApolloServerPluginLandingPageLocalDefault } from &#39;@apollo/server/plugin/landingPage/default&#39;; //was &#39;apollo-server-core&#39;
import { DenunciaDetallesModule } from &#39;./registry/denuncia-detalles/denuncia-detalles.module&#39;;

@Module({
  imports: [
    GraphQLModule.forRoot&lt;ApolloDriverConfig&gt;({
      autoSchemaFile: join(process.cwd(), &#39;src/schema.gql&#39;),
      sortSchema: true,
      driver: ApolloDriver,
      playground: false,
      plugins: [ApolloServerPluginLandingPageLocalDefault()],
    }),
    DenunciaDetallesModule,
  ],
})
export class AppModule {}

However, when running the API, I get the following error:

src/app.module.ts:15:17 - error TS2322: Type &#39;ApolloServerPlugin&lt;BaseContext&gt;&#39; is not assignable to type &#39;PluginDefinition&#39;.
Types of property &#39;serverWillStart&#39; are incompatible.
      Type &#39;(service: GraphQLServerContext) =&gt; Promise&lt;void | GraphQLServerListener&gt;&#39; is not assignable to type &#39;(service: GraphQLServiceContext) =&gt; Promise&lt;void | GraphQLServerListener&gt;&#39;.
        Types of parameters &#39;service&#39; and &#39;service&#39; are incompatible.
          Type &#39;GraphQLServiceContext&#39; is missing the following properties from type &#39;GraphQLServerContext&#39;: cache, startedInBackground

15       plugins: [ApolloServerPluginLandingPageLocalDefault()],

I could just switch back to apollo-server-core, but I don't understand why the types are not compatible if I updated all packages and made a clean install. Here are my dependencies:

&quot;dependencies&quot;: {
    &quot;@apollo/server&quot;: &quot;^4.7.4&quot;,
    &quot;@nestjs/apollo&quot;: &quot;^10.1.7&quot;,
    &quot;@nestjs/class-validator&quot;: &quot;^0.13.4&quot;,
    &quot;@nestjs/common&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/core&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/graphql&quot;: &quot;^10.1.7&quot;,
    &quot;@nestjs/platform-express&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/swagger&quot;: &quot;^6.1.3&quot;,
    &quot;@types/mssql&quot;: &quot;^8.1.1&quot;,
    &quot;apollo-server-core&quot;: &quot;^3.12.0&quot;,
    &quot;apollo-server-express&quot;: &quot;^3.12.0&quot;,
    &quot;class-transformer&quot;: &quot;^0.5.1&quot;,
    &quot;class-validator&quot;: &quot;^0.13.2&quot;,
    &quot;graphql&quot;: &quot;^16.6.0&quot;,
    &quot;joi&quot;: &quot;^17.7.0&quot;,
    &quot;mssql&quot;: &quot;^9.1.1&quot;,
    &quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
    &quot;rimraf&quot;: &quot;^3.0.2&quot;,
    &quot;rxjs&quot;: &quot;^7.2.0&quot;,
    &quot;typeorm&quot;: &quot;^0.3.11&quot;
  },

答案1

得分: 1

@nestjs/apollo@^10 兼容于 apollo 3。要使用 apollo 4,您需要使用 @nestjs/apollo@^11

英文:

@nestjs/apollo@^10 is comptible with apollo 3. To use apollo 4 you need to use @nestjs/apollo@^11

huangapple
  • 本文由 发表于 2023年6月29日 23:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定