moleculerjs/moleculer-apollo-server

Receive custom variables while uploading a file on server

Opened this issue · 2 comments

dg92 commented

Is there a way to get variables that are passed other than a file related stuff in the ctx object?

I have a mutation
mutation upload($file: Upload!, $entityId: String!) { uploadFile(file: $file, entityId: $entityId) { publicURL } }

I am passing entityId but when I try to look for it in ctx.params it's just a stream obj and ctx.meta contain file meta. I also need to get entityId. Is there a way to get that?

@shawnmcknight

The short answer is that this is not supported today. Since passed streams in moleculer must be the entirety of ctx.params, there is no mechanism to pass additional params. The only way to pass additional data is via ctx.meta, which is not ideal due to the built-in merging of meta for all future action calls and lack of validation of meta.

@icebob We've hit this limitation on streams recently ourselves. We did not hit it within our use of moleculer-apollo-server but rather in regular passing of streams between moleculer services. In our scenario the best solution we found was to use meta to pass additional arguments, write a custom hook to validate the meta and use broker.call to initiate the call to avoid merging meta within the moleculer context. This was not ideal.

Do you think it might be reasonable to support another mechanism for passing a stream between services rather than as the entirety of ctx.params and allow for other params to still be passed through?

The only other alternatives I can come up with for this specific problem would be to augment meta further, either putting all additional args into ctx.meta.$args or simply destructuring them all directly into ctx.meta, but neither is ideal for the aforementioned reasons.

I understand the issue. Currently, there is no better option. To change the stream sending logic, we should change the protocol which is a big breaking change. In the protocol, likewise, the transporter uses the params field to transfer the stream binary data.