在使用 NestJS + GraphQL Federation + Code First 构建的服务中提供 Subgraph,遇到了模式错误问题

错误发生的情况

参考にしてください、NestJSでFederationを構成するためのサンプルが以下のURLに記載されています。Subgraphを提供するプロジェクトを構築して実行すると、エラーが発生しました。

 

错误内容 (Error content)

发生了以下错误。错误内容显示 GraphQL 的模式不正确。

/your-project-path/node_modules/@apollo/federation-internals/src/error.ts:66
    super(
    ^
GraphQLError: The schema is not a valid GraphQL schema.. Caused by:
Invalid definition for directive "@tag": "@tag" should have locations FIELD_DEFINITION, OBJECT, INTERFACE, UNION, ARGUMENT_DEFINITION, SCALAR, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION, but found (non-subset) FIELD_DEFINITION, OBJECT, INTERFACE, UNION, ARGUMENT_DEFINITION, SCALAR, ENUM, ENUM_VALUE, INPUT_OBJECT, INPUT_FIELD_DEFINITION, SCHEMA
    at new AggregateGraphQLError (/your-project-path/node_modules/@apollo/federation-internals/src/error.ts:66:5)
    at aggregateError (/your-project-path/node_modules/@apollo/federation-internals/src/error.ts:87:10)
    at ErrGraphQLValidationFailed (/your-project-path/node_modules/@apollo/federation-internals/src/definitions.ts:61:17)
    at buildSchemaFromAST (/your-project-path/node_modules/@apollo/federation-internals/src/buildSchema.ts:173:37)
    at buildSubgraph (/your-project-path/node_modules/@apollo/federation-internals/src/federation.ts:1250:27)
    at buildSubgraphSchema (/your-project-path/node_modules/@apollo/subgraph/src/buildSubgraphSchema.ts:60:33)
    at GraphQLFederationFactory.generateSchemaFromCodeFirst (/your-project-path/node_modules/@nestjs/graphql/dist/federation/graphql-federation.factory.js:69:32)
    at GraphQLFederationFactory.generateSchema (/your-project-path/node_modules/@nestjs/graphql/dist/federation/graphql-federation.factory.js:31:22)
    at GraphQLModule.onModuleInit (/your-project-path/node_modules/@nestjs/graphql/dist/graphql.module.js:103:27)
    at callModuleInitHook (/your-project-path/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9)

改正方案

app.module.ts文件中包含了自动生成(基于代码)以下GraphQL模式的设置,但默认情况下联邦版本为1,将其改为2后可以解决模式错误。

// (省略)
autoSchemaFile: {
  federation: 2, // <- デフォルトが Federation バージョン = 1 となっている
  path: './schema.graphql',
},
// (省略)
广告
将在 10 秒后关闭
bannerAds