@galaxy-stack/orbit-graphql
v0.1.14
Published
GraphQL module for Orbit framework
Readme
@galaxy-stack/orbit-graphql
Mô tả
Module GraphQL cho Orbit với code-first approach, DataLoader support và WebSocket subscriptions.
Tính năng chính
1. Code-First Decorators
import {
ObjectType, Field, Query, Mutation,
Resolver, Args, Int
} from '@galaxy-stack/orbit-graphql';
@ObjectType()
class User {
@Field(() => Int)
id!: number;
@Field()
name!: string;
@Field({ nullable: true })
email?: string;
}
@Resolver(() => User)
class UserResolver {
@Query(() => User)
async user(@Args('id', Int) id: number) {
return await this.userService.findById(id);
}
@Query(() => [User])
async users() {
return await this.userService.findAll();
}
@Mutation(() => User)
async createUser(@Args('input') input: CreateUserInput) {
return await this.userService.create(input);
}
}2. Input Types
@InputType()
class CreateUserInput {
@Field()
name!: string;
@Field({ nullable: true })
email?: string;
}3. DataLoader (N+1 Prevention)
import { createDataLoader, ResolveField, Parent } from '@galaxy-stack/orbit-graphql';
@Resolver(() => Post)
class PostResolver {
private authorLoader = createDataLoader(
async (ids: number[]) => this.userService.findByIds(ids)
);
@ResolveField(() => User)
async author(@Parent() post: Post) {
return this.authorLoader.load(post.authorId);
}
}4. Subscriptions
import { Subscription, PubSub } from '@galaxy-stack/orbit-graphql';
@Resolver()
class NotificationResolver {
@Subscription(() => Notification)
notificationAdded() {
return pubSub.asyncIterator('NOTIFICATION_ADDED');
}
}
// Publish event
pubSub.publish('NOTIFICATION_ADDED', { notificationAdded: notification });Cấu hình Module
import { GraphQLModule } from '@galaxy-stack/orbit-graphql';
@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: true,
playground: true,
subscriptions: {
'graphql-ws': true,
},
}),
],
providers: [UserResolver, PostResolver],
})
class AppModule {}Field Decorators
@ObjectType()
class Product {
@Field(() => Int)
id!: number;
@Field(() => Float)
price!: number;
@Field(() => [String])
tags!: string[];
@Field(() => Int, { defaultValue: 0 })
stock!: number;
@Field({ deprecationReason: 'Use newField instead' })
oldField!: string;
}Enums
import { registerEnumType } from '@galaxy-stack/orbit-graphql';
enum UserRole {
ADMIN = 'ADMIN',
USER = 'USER',
}
registerEnumType(UserRole, { name: 'UserRole' });
@ObjectType()
class User {
@Field(() => UserRole)
role!: UserRole;
}Guards & Interceptors
@UseGuards(GqlAuthGuard)
@Resolver()
class ProtectedResolver {
@Query(() => String)
@UseInterceptors(LoggingInterceptor)
protected() {
return 'Protected data';
}
}