0

I am implementing GraphQL subscription based on this document using the PubSub Class. However,

the document also says:- The PubSub class is not recommended for production environments, because it's an in-memory event system that only supports a single server instance. After you get subscriptions working in development, we strongly recommend switching it out for a different subclass of the abstract PubSubEngine class. Recommended subclasses are listed in Production PubSub libraries.

So, I chose the Google PubSub Library, the second option from the list.

In a file subscription.js, I wrote the following code:-

const { GooglePubSub }    = require('@axelspringer/graphql-google-pubsub');
const pubsub              = new GooglePubSub();
const UPDATE_PARKING      = "UPDATE_PARKING";

module.exports = {pubsub, UPDATE_PARKING};

In the file named parking.queries.js, the Graphql is like this:-

type Subscription {
  updateParking: Parking
}

In the file named parking.resolver.js, the Resolver is like this

const {pubsub, UPDATE_PARKING}        = require("../utils/subscription");

Subscription: {
    updateParking: {
      subscribe: () => pubsub.asyncIterator(UPDATE_PARKING)
    }
}

Now to publish the data in the service file named parking.service.js, I did this:-

const {pubsub, UPDATE_PARKING}   = require("../utils/subscription");

.........................
.........................
.........................
pubsub.publish(UPDATE_PARKING, {updateParking: parkingDetail});
.........................

The above code runs fine. However, when I an trying to publish a new subscription, I am facing a strange problem. Let, me explain.

In the file subscription.js, I made the following changes:-

const { GooglePubSub }    = require('@axelspringer/graphql-google-pubsub');
const pubsub              = new GooglePubSub();
const UPDATE_PARKING      = "UPDATE_PARKING";
const UPDATE_USER         = "UPDATE_USER";

module.exports = {pubsub, UPDATE_PARKING, UPDATE_USER};

In the file named user.queries.js, the Graphql is like this:-

type Subscription {
   updateUser: User
}

In the file named user.resolver.js, the Resolver is like this,

const {pubsub, UPDATE_USER}           = require("../utils/subscription");

Subscription: {
    updateUser: {
      subscribe: () => pubsub.asyncIterator(UPDATE_USER)
    }
}

In the service file user_subscription.service.js, I have the code like this:-

const {pubsub, UPDATE_USER} = require("../utils/subscription");

............................
............................
pubsub.publish(UPDATE_USER, {updateUser: newUserData});
............................

Whenever the code pubsub.publish(UPDATE_USER, {updateUser: newUserData}); is getting executed, I am getting this strange error:-

/var/www/example/html/node_modules/google-auth-library/build/src/auth/googleauth.js:89
                        throw new Error('Unable to detect a Project Id in the current environment. \n' +
                              ^

Error: Unable to detect a Project Id in the current environment.
To learn more about authentication and Google APIs, visit:
https://cloud.google.com/docs/authentication/getting-started
    at /var/www/example/html/node_modules/google-auth-library/build/src/auth/googleauth.js:89:31
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
[nodemon] app crashed - waiting for file changes before starting...

enter image description here

However, whenever this code is executed pubsub.publish(UPDATE_PARKING, {updateParking: parkingDetail}); the above error doesn't happen.

What am I doing wrong?

Saswat
  • 12,320
  • 16
  • 77
  • 156

0 Answers0