I am working on a Graphql Project and in there I am using apollo-server-express(v3) for Graphql Server with Subscriptions as they have described in docs. My subscriptions work just fine when I use PubSub from graphql-subscriptions but as they've described in Docs that graphql-subscriptions's PubSub is not recommended for Production versions of Project so for Production Purpose I am trying to use graphql-google-pubsub as they have given in recommended list of Production libraries for PubSub. I have provided my code below.
index.js
const express = require('express');
const { createServer } = require('http');
const { WebSocketServer } = require('ws');
const { useServer } = require('graphql-ws/lib/use/ws');
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core');
const { GooglePubSub } = require('@axelspringer/graphql-google-pubsub');
const schema = require('./schema');
const app = express();
const httpServer = createServer(app);
const WSServer = new WebSocketServer({
server: httpServer,
path: 'graphql'
});
const pubsub = new GooglePubSub();
const serverCleanup = useServer({ schema, context: { pubsub } }, WSServer);
const apolloServer = new ApolloServer({
schema,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async serverWillStart() {
return {
async drainServer() {
serverCleanup.dispose();
}
};
}
}
]
});
async function startServer() {
await apolloServer.start();
apolloServer.applyMiddleware({ app });
httpServer.listen(4000, () => {
console.log('Server is up on Port 4000');
});
}
startServer();
typeDefs & resolvers
const typeDefs = gql`
type Subscription {
count: Int!
}
`;
const resolvers = {
Subscription: {
count: {
subscribe(_, __, { pubsub }) {
return pubsub.asyncIterator('counter');
}
}
}
};
Now, for Google Credentials, I have credentials.json in root of the project. also I have set ENV variable named GOOGLE_APPLICATION_CREDENTIALS.
Now, the problem is that whenever I run the Subscription from Apollo-Studio; the following error I get in my terminal.
I have all my credentials right in credentials.json file. Also, my Service account has "Owner" Role and the Account is "enabled". I have already enabled 'Cloud Pub/Sub API' on Project.
