I use graphql nodejs with apollo-server. When i try i got this error:
I already i returns pubsub.asyncIterator in resolver. It returns error. Another project same setting works but there it is not working. I examine some stack issues here but i couldnt solve.
Server: App.js
const express = require('express');
const mongoose = require('mongoose');
const { ApolloServer } = require('apollo-server');
const { mainSchema } = require('./graphql/schemas/index');
const { rootResolver } = require('./graphql/resolvers/index');
const app = express();
const server = new ApolloServer({
typeDefs: mainSchema,
resolvers: rootResolver,
subscriptions: {
path: `/graphql`,
onConnect: (connectionParams, webSocket, context) => {
console.log('Client connected');
},
onDisconnect: (webSocket, context) => {
console.log('Client disconnected')
},
},
});
server.listen(3000, "192.168.1.106").then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Subscription Resolver
Subscription :{
userNotifications : withFilter(
()=>pubSub.asyncIterator(NEW_NOTIFICATION_CREATE),
(payload, variables)=>{
console.log("payload=>", payload);
console.log("variables=>", variables);
return true
}
)}}
PubSub.js
const { PubSub } = require('graphql-subscriptions')
const pubSub =new PubSub()
exports.pubSub = pubSub;
I had must used subccribe keyword for subs define. Thanks me. Subscription :{ userNotifications : withFilter( subccribe: ()=>pubSub.asyncIterator(NEW_NOTIFICATION_CREATE), (payload, variables)=>{ console.log("payload=>", payload); console.log("variables=>", variables); return true } )}}