moleculerjs/moleculer-apollo-server

ctx undefined on subscription

palpinter opened this issue · 5 comments

Hi,

In the code below ctx is undefined when subscription called:

createAsyncIteratorResolver(actionName, tags = [], filter) {
	return {
		subscribe: filter
			? withFilter(
					() => this.pubsub.asyncIterator(tags),
					async (payload, params, { ctx }) =>
						payload !== undefined
							? ctx.call(filter, { ...params, payload })
							: false
			  )
			: () => this.pubsub.asyncIterator(tags),
		resolve: (payload, params, { ctx }) =>
			ctx.call(actionName, { ...params, payload }),
	};
},

However when I change ctx.call to this.broker.call it works.

Thx
palpinter

@palpinter How you solved that issue? Did you publish a custom package? If yes, Let me know.

I am facing also same issue,
I put this.broker.call to fix it temporally in this function.

If I don't use my own custom ws hook to authenticate socket, then I don't need to write this.broker.call.
It return data but not resolving attributes.

My code to authenticate ws.

  hooks: {
    after: {
       ws: 'onConnect'
    }
  },
  methods: {

    async onConnect (ctx, res) {
      const auth = ctx.params.connectionParams['authorization'] || '';
      if (auth && auth.startsWith('Bearer')) {
          return ctx;
      } else {
        // No token. Throw an error or do nothing if anonymous access is allowed.
        throw new MoleculerError(
          'Missing access token!',
          401,
          'NO_TOKEN'
        );
      }
    }
  },

@icebob is there any way to pass context ?

@mabc224 I have not found a good solution yet. My dirty hack is, that I resolve the necessary entities in the action handler body, and removed resolvers from service graphql property. I know it is a wrong solution, but the moleculer-apollo-server service.js code is complicated enough to write a brand new context factory. It is faster and easier, because I have a strict deadline.

Sorry, but the ws part is not written by me and I don't know too much about it. @Hugome ?

@mabc224 @palpinter Can either of you provide any more information on your scenario where this is undefined? I'm running the local subscription example and its defined and working properly.