question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in apollo-server-express: Subscription field must return Async Iterable. Received: undefined

See original GitHub issue

I’m trying to implement subscriptions with apollo-server-express. From graphql-playground I receive the following error…

{
  "error": {
    "message": "Subscription field must return Async Iterable. Received: undefined"
  }
}

Until now I had not had problems with queries and mutations. I also had no errors with subscriptions using apollo-server. But I do have them with apollo-server-express.

This is the code:

const {gql, ApolloError, PubSub} = require('apollo-server-express');

// Mongoose models
const NoteModel = require('../../mongoose/NoteModel');

// Instance PubSub (publications and subscriptions)
const pubsub = new PubSub();

// Subscriptions tags
const NOTE_ADDED = 'NOTE_ADDED';


// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
    type Note {
        _id: Int
        title: String
        description: String
    }

    input NoteInput {
        title: String
        description: String
    }

    # The "Query" type is the root of all GraphQL queries.
    # Using extend is particularly useful in avoiding a large 
    # list of fields on root Queries and Mutations
    # More info: https://www.apollographql.com/docs/graphql-tools/generate-schema#extend-types
    extend type Query {
        listNotes: [Note]
    }

    extend type Mutation {
        addNote(input: NoteInput) : Note!,
        updateNote(
            id: Int!,
            input: NoteInput
        ): Note!,
        deleteNote(id: Int!): Boolean!
    }

    extend type Subscription {
        noteAdded: [Note]
    }
`;


const resolvers = {
    Query: {
        listNotes: async (parent, args, context, info) => {
            
        }
    },
    Mutation: {
        addNote: (parent, args, context, info) => {
            pubsub.publish(NOTE_ADDED, { noteAdded: args.input });
            return NoteModel.create(args.input);
        },
        updateNote: (parent, args, context, info) => {

        },
        deleteNote: (parent, args, context, info) => {

        }
    },
    Subscription: {
        noteAdded: {
            subscribe: () => pubsub.asyncIterator([NOTE_ADDED]),
        },
    },
};


module.exports = {
    typeDefs,
    resolvers
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
abernixcommented, May 7, 2019

👋 I’ll close this since this doesn’t appear to be a bug with Apollo Server, but rather a question about how to use it or one of its components.

Rather than asking it here in GitHub Issues — where efforts are focused on fixing bugs and adding new features — I’d ask that you take this question to the Apollo Server channel within the Apollo community on Spectrum.chat where there are community members who might be able to relate to a similar problem, or might be able to help you out more interactively. Thanks for your understanding!

14reactions
pavel-koryagincommented, Aug 3, 2019

@abernix It is definitely a bug! And I did some investigation.

  1. @LaurenceM10 wrote that it works with new ApolloServer({ typeDefs, resolvers }), but it does not with new ApolloServer({ modules })

  2. In my case it does not work with new ApolloServer({ schema })

So, I found where the problem appears: http://center.koryagin.com/i/2019-08-04_00:29:39_6df691.png

And with this trivial monkey-patching everything is working, subscription messages arrive in browser.

const schema = buildFederatedSchema(federationSources);
schema._subscriptionType._fields.messageCreated.subscribe = 
  federationSources[11].resolvers.Subscription.messageCreated.subscribe;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscription field must return Async Iterable. Received ...
We are trying to get the resolver for a subscription working in Apollo Server. The subscription works when it is a top-level field...
Read more >
apollo "Subscription field must return Async Iterable. Received
I fixed this error by hardening imports (there was some issue w/ the index file not properly exporting). "message": "Subscription field must ......
Read more >
Subscription field must return Async Iterable. Received
It means that somewhere, some piece of code expected an object to be an async iterable, but it was undefined instead :slight_smile:.
Read more >
Subscriptions in Apollo Server - Apollo GraphQL Docs
The subscribe function must return an object of type AsyncIterator , a standard interface for iterating over asynchronous results. In the above postCreated....
Read more >
Subscription field must return Async Iterable. Received
received : undefined. Add Answer | View In TPC Matrix. Technical Problem Cluster First Answered On August 31, 2021 Popularity 6/10 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found