Comments (4)
hmm, interesting. I didn't even consider just using the mongoose module. If anything this module could be simplified a lot to just be a simple wrapper around the mongoose module. Right now it duplicates a lot of functionality I think.
From what I can tell from your examples, the main difference is
export const UserModel = getModelForClass(User);
export const UserSchema = buildSchema(User)
and
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }])
from nestjs-typegoose.
@gperdomor I have a problem with your example.
I have this message : " GraphQLError: Type Query must define one or more fields."
ad.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);
ad.controller.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);
}
}
ad.service.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();
}
}
ad.module.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 {}
The error stack trace
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.
I don't see any differences or i'm blind ;) and I don't find the solution yet. An idea ?
from nestjs-typegoose.
@oliviermattei I don't see differences either, but I'm not using graphQL, try without graphQL
from nestjs-typegoose.
I am doing to close this issue since it is stale.
from nestjs-typegoose.
Related Issues (20)
- Controller passes the test whereas the services level test don't
- Models with the same name HOT 1
- some error
- Incompatibility with the typegoose model
- Typegoose library hook errors.....Generic type HOT 2
- InjectModel brake on mongoose 5.12.9 and higher HOT 6
- Dependency Injection Inside a Pre save hook HOT 1
- Support/ update to NestJS v8 and others HOT 17
- Discriminator properties not working, only base class.
- MongooseError: Operation `xxx.find()` buffering timed out after 10000ms
- Mongoose version 6.0.5, without usenewurlparser parameter
- Mongoose 6.x Support Issue - no exported member 'ConnectionOptions' HOT 1
- replace Promises with Observables HOT 1
- 'useNewUrlParser' does not exist in type 'TypegooseConnectionOptions' HOT 4
- Can not install module HOT 5
- Dependency Dashboard
- Package maintenance HOT 5
- Using the same models in InjectModel HOT 1
- InjectConnection not supported?
- Not support new @nestjs/common 10 version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-typegoose.