Allow subscription to return initial data of an entity
Asked Answered
M

0

6

I need a subscription to return initial state of a requested type.

I have tasks system in my app, a task may have multiple states: CREATED, QUEUED, RUNNING, COMPLETED, CANCELLED, CANCELLING etc. When a task is created, it has CREATED state and then goes through multiple states depending on its type, until it is COMPLETED.

There is a subscription taskUpdated(id: ID!): Task which does what it says. This is a subscription resolver:

    taskUpdated: {
      subscribe: async (...args) => {
        try {
          // get initial task state
          const initialTaskData = await resolveTask(args[1].input);

          const cb = withFilter(
            (_, { input }) => {
              const iterator = tasksPubsub.asyncIterator(TASK_UPDATED);

              // Set initial task state in the iterator? 

              return iterator;
            },
            (payload, variables) => {
              return payload.id === variables.input.id;
            },
          );

          return cb.apply(null, args);
        } catch (e) {
          // handle error
          debugger;
        }
      },
    },

How do I set initial task state in the iterator? Someone suggested to create a wrapper around iterator, that would do the work, but I don't have any ideas where to start and it seems to be an overkill. Are there any simple solutions for this issue?

PS. Do a query to read and then a subscription for updates is not an option in my case due to front-end limitation.

Meitner answered 27/9, 2019 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.