node.js - GraphQLError:类型查询必须定义一个或多个字段。使用类型鹅
问题描述
我尝试在我的项目中实现 typegoose。我正在创建一个nestjs graphql 服务器并使用猫鼬将文档推送到我的数据库中。
我尝试使用 typegoose 来简化实现新功能的方式,但我遇到了一个问题,即用 typegoose 转换我的猫鼬“模式”。
让我们看看我的代码
广告.ts
import { buildSchema, getModelForClass, prop } from '@typegoose/typegoose';
export class Ad{
@prop({ required: true })
title: String;
@prop({ required: true })
description: String;
}
export const AdModel = getModelForClass(Ad);
export const AdSchema = buildSchema(Ad);
广告控制器.ts
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AdsService } from './ads.service';
import { Ad } from './models/ad';
@Controller('ads')
export class AdsController {
constructor(private readonly adService: AdsService) {}
@Get()
async getAds(): Promise<Ad[] | null> {
return await this.adService.findAll();
}
@Post()
async create(@Body() adDto: Partial<Ad>): Promise<Ad> {
return await this.adService.create(adDto);
}
}
广告服务.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Ad } from './models/ad';
import { ReturnModelType } from '@typegoose/typegoose';
@Injectable()
export class AdsService {
constructor(
@InjectModel(Ad.name)
private readonly adModel: ReturnModelType<typeof Ad>) {}
async create(adDto: Partial<Ad>): Promise <Ad> {
const createdAd = new this.adModel(adDto);
return await createdAd.save();
}
async findAll(): Promise <Ad[]> {
return await this.adModel.find().exec();
}
}
广告模块.ts
import { Module } from '@nestjs/common';
import { AdsService } from './ads.service';
import { MongooseModule } from '@nestjs/mongoose';
import { Ad, AdSchema } from './models/ad';
import { AdsController } from './ads.controller';
@Module({
imports:[MongooseModule.forFeature([{ name: Ad.name, schema: AdSchema }])],
controllers: [AdsController],
providers: [AdsService],
})
export class AdsModule {}
但我收到此错误消息:
错误堆栈跟踪
GraphQLError: Type Query must define one or more fields.
at SchemaValidationContext.reportError (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/type/validate.js:90:19)
at validateFields (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/type/validate.js:251:13)
at validateTypes (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/type/validate.js:226:7)
at validateSchema (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/type/validate.js:54:3)
at graphqlImpl (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/graphql.js:79:62)
at /home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/graphql.js:28:59
at new Promise (<anonymous>)
at Object.graphql (/home/olivier/www/other/green/back-end/graphql-server/node_modules/graphql/graphql.js:26:10)
at Function.<anonymous> (/home/olivier/www/other/green/back-end/graphql-server/node_modules/type-graphql/dist/schema/schema-generator.js:18:52)
at Generator.next (<anonymous>)
at /home/olivier/www/other/green/back-end/graphql-server/node_modules/tslib/tslib.js:110:75
at new Promise (<anonymous>)
at Object.__awaiter (/home/olivier/www/other/green/back-end/graphql-server/node_modules/tslib/tslib.js:106:16)
at Function.generateFromMetadata (/home/olivier/www/other/green/back-end/graphql-server/node_modules/type-graphql/dist/schema/schema-generator.js:15:24)
at /home/olivier/www/other/green/back-end/graphql-server/node_modules/type-graphql/dist/utils/buildSchema.js:11:65
at Generator.next (<anonymous>) {
message: 'Type Query must define one or more fields.'
}
]
(node:1828) UnhandledPromiseRejectionWarning: Error: Generating schema error
at Function.<anonymous> (/home/olivier/www/other/green/back-end/graphql-server/node_modules/type-graphql/dist/schema/schema-generator.js:20:27)
at Generator.next (<anonymous>)
at fulfilled (/home/olivier/www/other/green/back-end/graphql-server/node_modules/tslib/tslib.js:107:62)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:1828) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1828) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
我想这与 Typegoose 无关。您是否定义了任何 Graphql 类型或查询?如果您添加GraphQLModule
到应用程序的模块中,则需要有一个适当的.graphql
文件,该文件至少应具有 Query 类型。
推荐阅读
- python - catboost 如何为缺失的数值选择一个值?
- php - 在一个主题中使用两个不同的标题
- javascript - 如何在 Vue 组件的外部脚本文件上运行 es-lint 规则
- python - 比较 2 个不均匀列表包括在 python 中具有唯一键的字典和在 a 中但不是在 b 中的搜索键值
- python - 通过 jupyter 使用 Python 将 .csv 文件转换为指定样式
- c++ - C++ 游戏逻辑 UI 与线程同步
- java - 无法将字节数组(通过 android 客户端通过套接字发送的 jpg 文件)转换回 jpg 文件(文件损坏)
- kotlin - 如何在子命令 picocli 中使用父命令
- typescript - 如何在下一个 js 的服务器端渲染中使用钩子?
- clojure - 在 Clojure 中迭代 Map 时如何保持顺序?