Created
August 11, 2019 17:12
-
-
Save shantanoo-desai/8933c0425b0fb25c6b2a6f760d00468f to your computer and use it in GitHub Desktop.
Resolvers for GraphQL Subscription and Query for MQTT Broker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MQTTPubSub } = require('graphql-mqtt-subscriptions'); | |
const { connect } = require('mqtt'); | |
const client = connect('mqtt://mqtt.eclipse.org', { | |
reconnectPeriod: 1000 | |
}); | |
const pubsub = new MQTTPubSub({ | |
client | |
}); | |
const resolvers = { | |
Query: { | |
sensors: () => { | |
return [{id: "Sensor1"}, {id: "Sensor2"}]; | |
} | |
}, | |
Subscription: { | |
subscribe2sensor: { | |
resolve: (payload) => { | |
return { | |
temp: payload.data.temp, | |
humid: payload.data.humid, | |
time: new Date(payload.data.time * 1000).toISOString() | |
}; | |
}, | |
subscribe: (_, args) => pubsub.asyncIterator([args.topic]) | |
} | |
} | |
} | |
module.exports = { resolvers }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment