Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctx undefined on subscription #84

Open
palpinter opened this issue Sep 20, 2020 · 5 comments
Open

ctx undefined on subscription #84

palpinter opened this issue Sep 20, 2020 · 5 comments

Comments

@palpinter
Copy link

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

@mabc224
Copy link

mabc224 commented Apr 7, 2021

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

@mabc224
Copy link

mabc224 commented Apr 7, 2021

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 ?

@palpinter
Copy link
Author

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

@icebob
Copy link
Member

icebob commented Apr 8, 2021

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

@shawnmcknight
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants