LoginSignup
1
0

NestJS + GraphQL Federation + Code First の構成で Subgraph を提供するサービスでスキーマエラー

Posted at

エラー発生状況

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

エラー内容

以下のようなエラーが発生した。エラー内容としては 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 のスキーマを自動生成する(Code First)設定が記載されているが、デフォルトでは federation のバージョンが 1 となるため、これを 2 に変更するとスキーマエラーが解消した。

// (省略)
autoSchemaFile: {
  federation: 2, // <- デフォルトが Federation バージョン = 1 となっている
  path: './schema.graphql',
},
// (省略)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0