axelspringer / graphql-google-pubsub

A graphql-subscriptions PubSub Engine using Google PubSub
MIT License
136 stars 43 forks source link

commonMessageHandler returning null #32

Open thespacedeck opened 3 years ago

thespacedeck commented 3 years ago

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"
  },
kacy commented 3 years ago

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(
....
thespacedeck commented 3 years ago

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

inc16sec commented 2 years ago

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.