Subscriptions
Subscriptions
Subscriptionμ Query λ° Mutationκ³Ό κ°μ λ λ€λ₯Έ GraphQL μμ
μ νμ
λλ€. μλ°©ν₯ μ μ‘ κ³μΈ΅, μ£Όλ‘ μΉ μμΌμ ν΅ν΄ μ€μκ° κ΅¬λ
μ μμ±ν μ μμ΅λλ€. Subscriptionμ λν μμΈν λ΄μ©μ μ¬κΈ°λ₯Ό μ°Έμ‘°νμμμ€. μλλ 곡μ Apollo λ¬Έμμμ μ§μ λ³΅μ¬ ν commentAdded
ꡬλ
μμ μ
λλ€.
Subscription: {
commentAdded: {
subscribe: () => pubSub.asyncIterator('commentAdded');
}
}
warning μλ¦Ό
pubsub
λPubSub
ν΄λμ€μ μΈμ€ν΄μ€μ λλ€. μμΈν λ΄μ©μ μ¬κΈ°λ₯Ό μ°Έμ‘°νμμμ€.
Schema first
Nestμμ λλ±ν ꡬλ
μ λ§λ€λ €λ©΄ @Subscription()
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©ν©λλ€.
const pubSub = new PubSub();
@Resolver('Author')
export class AuthorResolver {
constructor(
private readonly authorsService: AuthorsService,
private readonly postsService: PostsService,
) {}
@Query('author')
async getAuthor(@Args('id') id: number) {
return await this.authorsService.findOneById(id);
}
@ResolveProperty('posts')
async getPosts(@Parent() author) {
const { id } = author;
return await this.postsService.findAll({ authorId: id });
}
@Subscription()
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
}
컨ν
μ€νΈμ μΈμλ₯Ό κΈ°λ°μΌλ‘ νΉμ μ΄λ²€νΈλ₯Ό νν°λ§νκΈ° μν΄ filter
μμ±μ μ€μ ν μ μμ΅λλ€.
@Subscription('commentAdded', {
filter: (payload, variables) =>
payload.commentAdded.repositoryName === variables.repoFullName,
})
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
κ²μλ νμ΄λ‘λλ₯Ό λ³κ²½νλ €λ©΄ resolve
ν¨μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
@Subscription('commentAdded', {
resolve: value => value,
})
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
Type definitions
λ§μ§λ§ λ¨κ³λ μ ν μ μ νμΌμ μ λ°μ΄νΈνλ κ²μ λλ€.
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
votes: Int
}
type Query {
author(id: Int!): Author
}
type Comment {
id: String
content: String
}
type Subscription {
commentAdded(repoFullName: String!): Comment
}
μ νμ΅λλ€. μ°λ¦¬λ νλμ commentAdded(repoFullName: String!): Comment
κ°μ
μ λ§λ€μμ΅λλ€. μ 체 μν ꡬνμ μ¬κΈ°μμ μ°Ύμ μ μμ΅λλ€.
Code first
ν΄λμ€ μ°μ μ κ·Ό λ°©μμ μ¬μ©νμ¬ κ΅¬λ
μ λ§λ€λ €λ©΄ @Subscription()
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©ν©λλ€.
const pubSub = new PubSub();
@Resolver('Author')
export class AuthorResolver {
constructor(
private readonly authorsService: AuthorsService,
private readonly postsService: PostsService,
) {}
@Query(returns => Author, { name: 'author' })
async getAuthor(@Args({ name: 'id', type: () => Int }) id: number) {
return await this.authorsService.findOneById(id);
}
@ResolveProperty('posts')
async getPosts(@Parent() author) {
const { id } = author;
return await this.postsService.findAll({ authorId: id });
}
@Subscription(returns => Comment)
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
}
컨ν
μ€νΈμ μΈμλ₯Ό κΈ°λ°μΌλ‘ νΉμ μ΄λ²€νΈλ₯Ό νν°λ§νκΈ° μν΄ filter
μμ±μ μ€μ ν μ μμ΅λλ€.
@Subscription(returns => Comment, {
filter: (payload, variables) =>
payload.commentAdded.repositoryName === variables.repoFullName,
})
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
κ²μλ νμ΄λ‘λλ₯Ό λ³κ²½νλ €λ©΄resolve
ν¨μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
@Subscription(returns => Comment, {
resolve: value => value,
})
commentAdded() {
return pubSub.asyncIterator('commentAdded');
}
PubSub
μ¬κΈ°μλ λ‘컬 PubSub
μΈμ€ν΄μ€λ₯Ό μ¬μ©νμ΅λλ€. λμ , PubSub
λ₯Ό 곡κΈμλ‘ μ μνκ³ μμ±μ (@inject()
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νμ¬)λ₯Ό ν΅ν΄ μ£Όμ
ν λ€μ μ 체 μ ν리μΌμ΄μ
μμ μ¬ μ¬μ©ν΄μΌ ν©λλ€. Nest μ¬μ©μ μ§μ 곡κΈμ μ¬κΈ°μ λν μμΈν λ΄μ©μ μ½μ μ μμ΅λλ€.
{
provide: 'PUB_SUB',
useValue: new PubSub(),
}
Module
Subscriptionμ κ°λ₯νκ²νλ €λ©΄ installSubscriptionHandlers
μμ±μ true
λ‘ μ€μ ν΄μΌν©λλ€.
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
installSubscriptionHandlers: true,
}),
Subscription μλ² (μ : ν¬νΈ λ³κ²½)λ₯Ό μ¬μ©μ μ μνλ €λ©΄ subscriptions
μμ±μ μ¬μ©νμμμ€ (μ¬κΈ°).
Last updated
Was this helpful?