GraphQLError:必须提供查询根类型

时间:2020-09-28 16:18:52

标签: postgresql graphql nestjs typeorm

我的后端API使用的是NestJS,TypeORM和GraphQL。我收到以下错误:

GraphQLError [Object]: Query root type must be provided.
      at SchemaValidationContext.reportError (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:88:19)
      at validateRootTypes (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:107:13)
      at validateSchema (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/type/validate.js:52:3)
      at graphqlImpl (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:79:62)
      at /home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:28:59
      at new Promise (<anonymous>)
      at Object.graphql (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/graphql/graphql.js:26:10)
      at GraphQLSchemaFactory.<anonymous> (/home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:49:52)
      at Generator.next (<anonymous>)
      at /home/wise-introvert/Container/Projects/the-notebook/app/server/node_modules/tslib/tslib.js:114:75

这是我的文件结构和代码的样子: enter image description here

有人可以帮助我吗?我的仓库:https://github.com/wise-introvert/nestjs-graphql-api.git

2 个答案:

答案 0 :(得分:3)

运行GraphQL的所有服务器必须至少具有一个@Query()才能被视为有效的GraphQL服务器。没有它,apollo-server软件包将引发异常,并且服务器将无法启动。这可以很简单

@Resolver()
export class FooResolver {

  @Query(() => String)
  sayHello(): string {
    return 'Hello World!';
  }
}

答案 1 :(得分:0)

确保将解析程序也添加到模块提供程序中

@Module({
  imports: [
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: true,
    }),
  ],
  controllers: [],
  providers: [FooResolver], //< This
})
export class FooModule {}