Gmail Subscriptions
OneGraph supports GraphQL subscriptions for new messages on Gmail. Follow the steps below to enable them.
Step 1
Follow the instructions to create a custom OAuth client for Gmail.
Step 2
Enable the Gmail API on the Google Cloud console.
Step 3
Google requires a Google Cloud pub/sub topic to receive notifications about new Gmail messages.
Create a new pull pub/sub topic on Google Cloud with topic id "onegraph-gmail".
Grant gmail-api-push@system.gserviceaccount.com
the role of Pub/Sub Publisher
on the pubsub topic so that gmail can push messages to it.
Step 4
Set the pubsub topic name (e.g. projects/YOUR_PROJECT_NAME/topics/onegraph-gmail
) in the custom OAuth client form for Gmail from step 1.
Step 5
Create a new cloud function that will forward messages to OneGraph.
Set the configuration to the following:
- Name
- onegraph-gmail
- Memory allocated
- 128 MB
- Trigger
- Cloud Pub/Sub
- Topic
- onegraph-gmail
- Source code
- Inline editor
- Runtime
- Node.js 10 (Beta)
- index.js
const https = require('https');
exports.pubSubForwarder = (event, context) => {
return new Promise((resolve, reject) => {
try {
const bodyJson = JSON.stringify({
data: event.data,
messageId: context.eventId,
publishTime: context.timestamp,
});
const body = Buffer.from(bodyJson, 'utf-8');
const options = {
hostname: 'serve.onegraph.com',
port: 443,
path: '/webhooks/gmail/watch',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length,
},
};
const req = https.request(options, (res) => {
res.on('data', (d) => console.log('Response:', d.toString('utf-8')));
res.on('error', (e) => {
console.error('error posting message to OneGraph', e);
reject(e);
});
res.on('end', () => {
if (res.statusCode != 200) {
console.error('Got non-200 status code', res.statusCode);
reject(new Error('Error forwarding request to OneGraph'));
} else {
console.log('Successfully forwarded request to OneGraph');
resolve();
}
});
});
req.on('error', (e) => {
console.error('error posting message to OneGraph', e);
callback(e);
});
req.write(body);
req.end();
} catch (e) {
reject(e);
}
});
};
- Function to execute
- pubSubForwarder
Click create.
Step 6
Go to the Data Explorer tab on the OneGraph dashboard and run the following subscription:
subscription GmailNewMessage {
gmail {
newMessage {
id
snippet
}
}
}
Every time you receive a new message on Gmail, you will get a GraphQL response:
{
"data": {
"gmail": {
"newMessage": {
"id": "170ea4ad7bf928327",
"snippet": "Test On Tue, Mar 17, 2020 at 1:58 PM Daniel Woelfel <daniel@example.com> wrote: New message"
}
}
},
"extensions": {
"eventId": "e8efce56-4ee2-4dd2-81c8-f4e51e018f40",
"subscriptionId": "2e355868-13bd-44cf-b609-7954ede17729"
}
}