apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.59k stars 133 forks source link

Must return async iterator? #119

Closed alexknipfer closed 6 years ago

alexknipfer commented 6 years ago

I can't seem to solve this particular issue creating a subscription. In the payload, I'm receiving the following error: Subscription must return Async Iterable. Received: undefined

Subscription looks as follows:

const socket = require('../../lib/socket')
const { withFilter } = require('graphql-subscriptions')

const discAdded = () => {
  subscribe: withFilter(
    () => socket.asyncIterator('DISC_ADDED'),
    (payload, variables) => {
      return payload.createdBy === variables.createdBy
    }
  )
}

module.exports = discAdded

Full repo here: https://github.com/alexknipfer/DiscShareServer/tree/subscriptions

Any help greatly appreciated!

ravenscar commented 6 years ago

Hey I just randomly came across your question, but I remember getting a similar error because subscriptions (unlike query/mutation) don't take a function. They just have an object which has a subscription, which in turn takes a function. I altered your code (just really the const discAdded line and an extra ) at the end), I hope it works.

const socket = require('../../lib/socket')
const { withFilter } = require('graphql-subscriptions')

const discAdded = ({
  subscribe: withFilter(
    () => socket.asyncIterator('DISC_ADDED'),
    (payload, variables) => {
      return payload.createdBy === variables.createdBy
    }
  )
})
alexknipfer commented 6 years ago

Thanks for the reply, good to know it doesn't take a function. Unfortunately, this is still causing the same error of Subscription must return Async Iterable. Received: undefined in the payload.

ravenscar commented 6 years ago

@alexknipfer

I just had a look at your repo and I think you forgot to add the Subscriptions in your resolvers: https://github.com/alexknipfer/DiscShareServer/blob/subscriptions/api/resolvers/index.js

alexknipfer commented 6 years ago

@ravenscar Thank you so much man! Didn't even catch that, after making that change and returning an object, that did the trick!