Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commonMessageHandler returning null #32

Open
thespacedeck opened this issue Jun 22, 2021 · 3 comments
Open

commonMessageHandler returning null #32

thespacedeck opened this issue Jun 22, 2021 · 3 comments

Comments

@thespacedeck
Copy link

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
Copy link

kacy commented Jun 26, 2021

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(
....

@thespacedeck
Copy link
Author

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

@inc16sec
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants