moleculer-apollo-server icon indicating copy to clipboard operation
moleculer-apollo-server copied to clipboard

ctx undefined on subscription

Open palpinter opened this issue 5 years ago • 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 avatar Sep 20 '20 07:09 palpinter

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

mabc224 avatar Apr 07 '21 10:04 mabc224

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 avatar Apr 07 '21 10:04 mabc224

@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.

palpinter avatar Apr 07 '21 11:04 palpinter

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

icebob avatar Apr 08 '21 12:04 icebob

@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.

shawnmcknight avatar Apr 10 '21 13:04 shawnmcknight