1

I'm having an issue with Apollo GraphQL's subscription. When attempting to start the subscription I'm getting this in return:

"Subscription field must return Async Iterable. Received: { pubsub: { ee: [EventEmitter], subscriptions: {}, subIdCounter: 0 }, pullQueue: [], pushQueue: [], running: true, allSubscribed: null, eventsArray: [\"H-f_mUvS\"], return: [function return] }"

I have other subscriptions setup and are completely functional - so I can confirm the webserver is setup correctly.

I'm just curious if anyone else has ever ran onto this issue before.

Source code in PR diff (it's an open source project): https://github.com/astronomer/houston-api/pull/165/files

error in playground

2
  • Can you update your questions with the relevant resolver code? Nov 13, 2019 at 20:28
  • I added a link to the PR I have opening showing the relevant code that's causing the issue. It's quite involved and may not make total sense without a full context. Nov 13, 2019 at 20:32

1 Answer 1

0

I don't think this is an issue specific to the PR you posted. I'd be surprised if any of the subscriptions were working as is.

Your subscribe function should return an AsyncIterable, as the error states. Since it returns a call to createPoller, createPoller should return an AsyncIterable. But here's what that function looks like:

export default function createPoller(
  func,
  pubsub,
  interval = 5000, // Poll every 5 seconds
  timeout = 3600000 // Kill after 1 hour
) {
  // Gernate a random internal topic.
  const topic = shortid.generate();

  // Create an async iterator. This is what a subscription resolver expects to be returned.
  const iterator = pubsub.asyncIterator(topic);

  // Wrap the publish function on the pubsub object, pre-populating the topic.
  const publish = bind(curry(pubsub.publish, 2)(topic), pubsub);

  // Call the function once to get initial dataset.
  func(publish);

  // Then set up a timer to call the passed function. This is the poller.
  const poll = setInterval(partial(func, publish), interval);

  // If we are passed a timeout, kill subscription after that interval has passed.
  const kill = setTimeout(iterator.return, timeout);

  // Create a typical async iterator, but overwrite the return function
  // and cancel the timer. The return function gets called by the apollo server
  // when a subscription is cancelled.
  return {
    ...iterator,
    return: () => {
      log.info(`Disconnecting subscription ${topic}`);
      clearInterval(poll);
      clearTimeout(kill);
      return iterator.return();
    }
  };
}

So createPoller creates an AsyncIterable, but then creates a shallow copy of it and returns that. graphql-subscriptions uses iterall's isAsyncIterable for the check that's producing the error you're seeing. Because of the way isAsyncIterable works, a shallow copy won't fly. You can see this for yourself:

const { PubSub } = require('graphql-subscriptions')
const { isAsyncIterable } = require('iterall')

const pubSub = new PubSub()
const iterable = pubSub.asyncIterator('test')
const copy = { ...iterable }
console.log(isAsyncIterable(iterable)) // true
console.log(isAsyncIterable(copy)) // false

So, instead of returning a shallow copy, createPoller should just mutate the return method directly:

export default function createPoller(...) {
  ...
  iterator.return = () => { ... }

  return iterator
}
1
  • Fantastic response, thank you for the time you put into figuring this out. Hugely appreciated!! Nov 14, 2019 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.