0

I'm using the Apollo graphql framework, seemingly getting the subscription up and running. But I'm unable to get any events triggered from the subscription and the documentation around Apollo client is rarely about pure nodejs and vanilla javascript.

The project is making a few nodes for node-red, which will use a vendors graphql interface to fetch different data on power consumption and electricity pricing. For this purpose, I need to use subscriptions as well. Quite new to graphql and was recommended the apollo framework.

result = tibber.getSubscription('subscription{ liveMeasurement(homeId:"c70dcbe5-4485-4821-933d-a8a86452737b"){timestamp power maxPower accumulatedConsumption accumulatedCost}}');
// returns an Observable from the apollo client subscribe()
// client.subscribe({ query: gql`${query}` });

let sub = result.subscribe({
    next (data) { console.log(data); },
    error (err) { console.log(err); }
  });

Would have expected the console.log to trigger with some data after a few seconds, as this subscription works with the demo account on the api explorer of the vendor. No errors returned and subscription object (sub) is in 'ready' state.

visvas
  • 11
  • 2
  • subsription will 'pass' the change from server to client, ok, suppose it works (one half done) ... but what will notify server that sth changed? cache/polling/pubsub... fetch is one time event, subscription need to bee 'feeded' – xadm Jun 01 '19 at 17:25
  • Not sure i got that... It´s a defined subscription on the server, so that is set up to relay information at changes and/or intervalls (not my server). I have a websocket link that then should be kept open and an observer on what changes on the query (either network or cache). Do you mean there is anything more i need with the apollo client to make this to work? – visvas Jun 01 '19 at 20:28
  • it's more about apollo server - do you have sth to triger/emit events to this (probably working) subscription (c-s) 'channel' ? – xadm Jun 01 '19 at 21:05
  • Not my server, a vendor called Tibber. But yes, its a valid subscription in their schema, verified using their API explorer (data is flowing regulary) and same demo account token I use for testing from my client. – visvas Jun 02 '19 at 12:46

1 Answers1

0

My advice: divide and conquer

I would start with standard react app with apollo client. You can use <Query/> component for simplicity. In this step the most important is gain familiarity with authentication

Next step: use client directly, without using components. Still using react use ApolloConsumer - your component gets access to client prop. You can use client.query() called from componentDidMount() (console.log, setState).

If you prefer vanilla this tutorial can be more suitable.

Next step would be working with subscriptions. Again, start with <Subscription/> component. After that you should have properly configured client (transport, auth).

At this step you're starting to move to node env, f.e. looking for examples like this and combining with earlier gained knowledge.

xadm
  • 8,219
  • 3
  • 14
  • 25