Apollo Graphql Subscriptions: Different subscription resolvers for create, update & delete or a single resolver?

Viewed 144

I am using Typegrpahql for creating my graphql server with express. I want to clarify whether I should create separate subscription resolvers for create, update and delete operations of an entity? Or should I create one resolver since the return type of the data will always be the same?

Suppose I have an entity like this

type Board {
  createdAt: DateTime!
  id: ID!
  members: [User!]!
  name: String!
  owner: User!
  stacks: [Stack!]!
  updatedAt: DateTime
}

And I have a mutation resolver class like this

@Resolver(Board)
export class BoardMuations {
  @Authorized()
  @Mutation(() => Board)
  async createBoard(
    @PubSub() pubSub: PubSubEngine,
    @Arg("name") name: string,
    @Ctx() { uid }: MyContext
  ): Promise<Board> {
    // perform create opration
  }

  @Authorized()
  @Mutation(() => Board)
  async updateBoard(
    @PubSub() pubSub: PubSubEngine,
    @Arg("boardId") boardId: string,
    @Arg("name") name: string
  ): Promise<Board | undefined> {
    // perform update operation
  }

  @Authorized()
  @Mutation(() => DeleteResponse)
  async deleteBoard(@PubSub() pubSub: PubSubEngine, @Arg("boardId") boardId: string): Promise<DeleteResponse> {
    //perform delete operation
  }
}

So should I create a subscription resolver like this

@Subscription({ topics: "CUD_BOARD" })
  newBoard(@Root() data: Board): Board {
    return data;
  }

Or should I create different resolvers for every operation? what is this topics array?

I could also provide different topic strings as an array as I understand. What difference would it make to create different topics strings and publish the event using them?

I am currently publishing the event like this await pubSub.publish("CUD_BOARD", board);

0 Answers
Related