graphql-google-pubsub icon indicating copy to clipboard operation
graphql-google-pubsub copied to clipboard

commonMessageHandler returning null

Open thespacedeck opened this issue 4 years ago • 3 comments

Hi there,

My luck that this is not maintained anymore.... :/

If anyone would be able to assist it would be great:

//subscribing to this endpoint:
subscription Subscription {
  organizationsChanged {
    id
  }
}

results in organizationsChanged to be null:

// Response received at 23:24:00
{
  "data": {
    "organizationsChanged": null
  }
}

// Response received at 23:23:59
{
  "data": {
    "organizationsChanged": null
  }
}

// Response received at 23:23:58
{
  "data": {
    "organizationsChanged": null
  }
}

Project setup is as follows:

//schema.ts
  type Subscription {
    organizationsChanged: testObject
  }

  type testObject {
    id: String
  }
//resolver
Subscription: {
    // get all tracks, will be used to populate the homepage grid of our web client
    organizationsChanged: {
      subscribe: () => pubsub.asyncIterator(["ORGANISATION_CHANGED2"]),
    },
  },
//init
import { GooglePubSub } from '@axelspringer/graphql-google-pubsub';
// SETUP subscriptions
function commonMessageHandler (payload:any) {
  console.log(payload.id); /// get the correct data from publish here - whohoo!
  return {
    id: payload.id
  }
}

const pubsub = new GooglePubSub({
    projectId: 'PROJECT_ID',
    credentials:{
      client_email: 'SERV_ACC_EMAIL,
      private_key: "THE_KEY"
    }
  }, topicName => `ORGANISATION_CHANGED2`, commonMessageHandler)
}
//package.json
"dependencies": {
    "@axelspringer/graphql-google-pubsub": "^2.1.0",
    "apollo-datasource-rest": "^0.14.0",
    "apollo-server": "2.19.1",
    "dataloader": "^2.0.0",
    "graphql": "15.4.0"
  },

thespacedeck avatar Jun 22 '21 21:06 thespacedeck

Hi @thespacedeck -- You may have already found it, but check out the discussion in https://github.com/axelspringer/graphql-google-pubsub/issues/14.

I ran into this as well and fixed it with the suggestion of adding this to the schema:

...
			resolve: (payload) => {
				const utf8 = Buffer.from(payload.data, "base64").toString("utf-8");
				return JSON.parse(utf8);
			  },
			subscribe: withFilter(
....

kacy avatar Jun 26 '21 20:06 kacy

Hi @kacy, thanks for that - adding the resolve did it for me! Rgds.

thespacedeck avatar Aug 16 '21 20:08 thespacedeck

Hi @thespacedeck -- You may have already found it, but check out the discussion in #14.

I ran into this as well and fixed it with the suggestion of adding this to the schema:

...
			resolve: (payload) => {
				const utf8 = Buffer.from(payload.data, "base64").toString("utf-8");
				return JSON.parse(utf8);
			  },
			subscribe: withFilter(
....

Life changing.. After 3 days of scratching my head and trying all possible tricks to figure out why "null" was returned.. This was the answer and now it is working as expected.

inc16sec avatar Aug 19 '22 03:08 inc16sec