Monday, 13 May 2019

apollo subscription not receiving events

This is my first apollo subscription - I can't figure out what I'm missing.

Essentially, I have a mutation that fires the channel event 'countIncr', but I don't see the active corresponding subscription fire with the event payload.

Here is a repro: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here Frontend: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-ui/src/app/routes/Home/HomePage.tsx

const COUNTER_SUBSCRIPTION = gql`
subscription onCountIncr {
  count
}
`

const Counter = () => (
  <Subscription
    subscription={COUNTER_SUBSCRIPTION}
    // variables=
  >
    {({ data, loading }) => {
      console.log({loading, data})
      return loading
        ? <h1>Loading ...</h1>
        : data.count
          ? <h2>Counter: {data.count}</h2>
          : <h1>Counter Subscription Not Available</h1>
    }}
  </Subscription>
)

BE Subscription: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-service/src/gql/counter/Subscriptions.ts

const count = {
  resolve: data => {
    console.log('CounterSub>', {data})
    return data
  },
  subscribe: () => pubsub.asyncIterator(['countIncr'])
  // subscribe: () => pubsub.asyncIterator([Channel])
}

const CounterSubscriptions = {
  count
}

BE Mutation: https://github.com/TGRstack/tgr-apollo-subscription-example-microservice/blob/master/counter-service/src/gql/counter/mutations/countIncr.ts

async function countIncr(root: any, args: any, context: any) {
  const count = Counter.increment()
  await pubsub.publish('countIncr', { count })
  // await pubsub.publish(Channel, { count })
  console.log('countIncr', '>>>', { count })
  return count
}

Here is the service log after you've run through the #getting started instructions in the Readme.md

[FE] GET /favicon.ico 200 2.465 ms - 1551                   # WEBCLIENT LOADED
[BE] CounterSub> { data: undefined }                        # SUBSCRIPTION REQUEST
[BE] { data: [Object: null prototype] { count: null } }     # SUBSCRIPTION RESULT
[BE] POST / 200 21.254 ms - 24
[BE] 2019-05-10 11:37:20 [info]:     HELLO                  # APOLLO CLIENT CONNECTED AGAIN (why always 2?)
[BE] countIncr >>> { count: 1 }                             # MUTATION REQUEST
[BE] { data: [Object: null prototype] { countIncr: 1 } }    # MUTATION RESPONSE
[BE] POST / 200 13.159 ms - 25
[BE] countIncr >>> { count: 2 }                             # MUTATION REQUEST
[BE] { data: [Object: null prototype] { countIncr: 2 } }    # MUTATION RESPONSE
[BE] POST / 200 4.380 ms - 25

UPDATE Incase you've tried to clone the repo and after running nps it didn't work its because there was a step missing in nps setup. I've pushed an update to the stack with the nps setup improved.

UPDATE 2 updated code and links in question per latest commit



from apollo subscription not receiving events

No comments:

Post a Comment