Backend2025년 7월 25일2분 읽기

GraphQL Subscriptions — 실시간 데이터를 위한 구현 가이드

YS
YoungSam
조회 1059

Subscription 정의

// 스키마
type Subscription {
  messageAdded(chatId: ID!): Message!
}

// Resolver
const resolvers = {
  Subscription: {
    messageAdded: {
      subscribe: (_, { chatId }) =>
        pubsub.asyncIterator([`CHAT_${chatId}`]),
    },
  },
};

클라이언트

import { useSubscription, gql } from "@apollo/client";

const NEW_MESSAGE = gql`
  subscription OnMessage($chatId: ID!) {
    messageAdded(chatId: $chatId) {
      id content author { name }
    }
  }
`;

const { data } = useSubscription(NEW_MESSAGE, {
  variables: { chatId: "1" },
});
GraphQLSubscriptionsRealtime

댓글 0

아직 댓글이 없습니다.