Replies: 3 comments 3 replies
|
If you only use NestJS as an adapter, using GQLoom in NestJS is very simple. You just need to pass the Schema weaved by GQLoom to import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { AppController } from './app.controller';
import { schema } from './graphql';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
schema,
graphiql: true,
}),
],
controllers: [AppController],
providers: [],
})
export class AppModule {}I've created a simple example: https://stackblitz.com/edit/nestjs-typescript-starter-xugidczl?file=src%2Fgraphql%2Findex.ts |
|
I recommend checking out: https://github.com/incetarik/nestjs-graphql-zod, this library uses zod to build GraphQL types in nestjs |
|
Before developing GQLoom, I had carefully considered: Could I create a plugin for NestJS | TypeGraphQL, allowing us to use existing resolver builders and leverage the established NestJS | TypeGraphQL ecosystem? After weighing development experience and type safety, I abandoned the idea of building a plugin because the Decorator that NestJS | TypeGraphQL relies on cannot provide comprehensive type inference and type checking. For example, building a GraphQL API using NestJS Code-First mode has too much code redundancy and type unsafety: import { Field, ID, ObjectType, Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
@ObjectType('Giraffe')
export class Giraffe {
@Field(() => ID) // ID for GraphQL type
id: string; // string for TypeScript type
@Field(() => String, { description: 'The giraffe\'s name' }) // String for GraphQL type
name: string; // String for TypeScript type
@Field(() => Date) // Date for GraphQL type
birthday: Date; // Date for TypeScript type
}
@Resolver(() => Giraffe) // Giraffe for GraphQL type
export class GiraffeResolver {
@Query(() => GiraffeModel)
giraffe() {
return {
id: 'g1',
name: 'Melman',
birthday: new Date('2000-05-12'),
};
}
@ResolveField(() => Number)
age(
@Parent() giraffe: Giraffe, // Giraffe for TypeScript type
@Args('currentDate', {
type: () => Date, // Date for GraphQL type
nullable: true,
defaultValue: () => new Date(),
})
currentDate: Date, // Date for TypeScript type
): number {
return currentDate.getFullYear() - giraffe.birthday.getFullYear();
}
}Furthermore, if you write completely incorrect types, TypeScript cannot detect the issue, such as: @ObjectType('Giraffe')
export class Giraffe {
@Field(() => String) // String for GraphQL type
id: number; // number for TypeScript type, it's wrong but TypeScript can't tell
@Field(() => String, { description: 'The giraffe\'s name' }) // String for GraphQL type
name: number; // number for TypeScript type, it's wrong but TypeScript can't tell
@Field(() => Date) // Date for GraphQL type
birthday: number; // number for TypeScript type, it's wrong but TypeScript can't tell
}ORMs that rely on decorators like TypeORM have the same issue. This critical problem also prompted tRPC to give up class-based syntax. In the long run, ECMAScript decorators will replace Personally, I'm not optimistic about the application of Decorator features. If type-related problems remain unresolved, decorators will gradually decline in usage. |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I've been exploring for GQL and found out about this package, and I wonder if you ever considered to weave also into
NestJSso that one can use thisgqloomwith likezodand wire it up intoNestJS.All reactions