How to resolve "Resource not found" error in graphql-google-pubsub?

Viewed 143

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.

Error Image

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.

1 Answers

There is my way to set apollo-server-express and graphql-google-pubsub in my project:

//route to your serviceAccount or credentials
const serviceAccount = require("../credentials.json");

const options = {
  projectId: serviceAccount["project_id"],
  credentials: {
    client_email: serviceAccount["client_email"],
    private_key: serviceAccount["private_key"],
  },
};

const commonMessageHandler = (payload)=>{
  const dataBuffer = payload.data || '{}';
  return JSON.parse(dataBuffer);
}

const topic2SubName = (topicName: string) =>
  `projects/${serviceAccount["project_id"]}/subscriptions/${topicName}`;

const pubsub = new GooglePubSub(options, topic2SubName, commonMessageHandler);

Things to check if in your case "Resource not found":

  1. Create topic and also subscription under topic in Google Pubsub https://console.cloud.google.com/cloudpubsub
  2. To make sure topic2SubName handler correct when set up Pubsub instance
  3. To check when pubsub.publish(${topicName}, data) with correct topic name

Hope this could work for you.

Related