davidyaha / graphql-mqtt-subscriptions

graphql-subscriptions implementation for MQTT protocol
MIT License
149 stars 40 forks source link

A practical Example as a form of Documentation #10

Closed shantanoo-desai closed 5 years ago

shantanoo-desai commented 5 years ago

Hi @davidyaha I am working on this repository with express-graphql and was looking into documenting a practical example via MQTT. I am not using typescript but standard ES6 stuff. Would you be able to just guide me with how do I get the data from the Broker out?

Progress

Defined a Schema as following:

schema.js

        type Result {
            payload: String
        }

        input TopicInput {
            topic: String!
        }

        type RootSubscription {
            subscribeSite(topicInput: TopicInput): Result
        }

       schema {
            query: RootQuery
            mutation: RootMutation
            subscription: RootSubscription
        }

resolvers.js

I did the following:

const { MQTTPubSub } = require('graphql-mqtt-subscriptions');
const { connect } = require('mqtt');

const client = connect('my.mqtt.broker.address', {
    reconnectPeriod: 1000
});

const pubsub = new MQTTPubSub({
    client
});

within the resolver function:

subscribeSite: (args) => {
    pubsub.asyncIterator(args.topicInput.topic)
}

I am not sure how do I get the value from the asynIterator at this point and return it to the GraphiQL UI.

I already have a setup where live data is sent via sensor nodes and maybe the repo could benefit from a nice working example writeup

shantanoo-desai commented 5 years ago

will reopen it once a concrete setup is up and running

shantanoo-desai commented 5 years ago

Based on #11 I will write a blog post to practically use this repo alongside graphql

davidyaha commented 5 years ago

Sounds great @shantanoo-desai! Please link it here when it's up!

btw, all subscription packages work similarly so you might want to head over to apollo's graphql-subscriptions repo to get information about asyncIterables and the api to transform and filter them. This package is intended to be more like an addon to graphql-subscriptions because the actual pubsub manager can be easily replaced for something like redis / rabbitmq or whatever actually.

Regarding your issue here:

subscribeSite: (args) => {
    pubsub.asyncIterator(args.topicInput.topic)
}

Please note that you are not returning anything in this arrow function. The simplest form of subscription resolver should return an object with a subscribe property that has an iterable factory.

There are good examples here: https://github.com/apollographql/graphql-subscriptions#getting-started-with-your-first-subscription

Let me know if you need anymore help there and I would love to merge a working example to this repo's README file.

shantanoo-desai commented 5 years ago

Hi @davidyaha I have written a blog post documenting a live IoT Data along side MQTT GraphQL Endpoint with Apollo Server here. Let me know if you find this useful or not.

davidyaha commented 5 years ago

@shantanoo-desai 🎉 🎉 Awesome use case!! Would love to see an app that utilizes this graphql api!

shantanoo-desai commented 5 years ago

@davidyaha I do actually have something in mind but requires a bit more thought. I figured out that GraphQL Schemas can Adapt EPCIS aka. product RFID very well.

An really practical example might be Subscribing to live product tracking within companies or factories via MQTTPubSub.