0

I am trying to use graphql subscriptions using the following libraries:

graphql-subscription on the server-side

subscription-transport-ws on the client-side

and when not using the filtering mechanism every thing goes right. But when I add the withFilter function into it, encounter the issue that no variable or context object receives to the filtering function.

For example I want to filter data according to the group. The client requests the server with the following query:

request body

And at the server-side the implemented method is like this:

        this.resolvers.Subscription[subscription.name] = {
            subscribe: withFilter((...params) => {console.log('subscription params: ', ...params); return this.pubsub.asyncIterator(subscription.name.toUpperCase());}, (...params) => {
                console.log('filtering params: ', ...params)
                return true // payload[subscription.name]._group === variables._group;
            }),
        };

which is creating some subscriptions dynamically (and its dynamicity does not relate to this issue). Now the two logs that I have made above will be as following:

subscription params: null {} null null

filtering params: { message_onMessage: { text: 'test', _offline_id: '1587981902663', _status: 3, time: 1587981902713, _id: 1587981902716, _rev: 1587981902716 } } {} null null

So that in the callback that is passed to the withFilter function which is used to filter the data, I do not have the variables object and the context object to fetch the requester _group parameter.

And the installed version of the libraries is as bellow:

// server
"graphql": "^14.6.0",
"graphql-subscriptions": "^1.1.0",
// client
"graphql": "^15.0.0",
"graphql-tag": "^2.10.3",
"subscriptions-transport-ws": "^0.9.16"
ConductedClever
  • 4,175
  • 2
  • 35
  • 69
  • I do not understand your comment @xadm. Either if it is a question or what. But in this case `...params` is used to catch all the parameters as a debug technique. – ConductedClever Apr 27 '20 at 10:48
  • try "normal" `(value, args, context, info)` args definition? – xadm Apr 27 '20 at 11:35

1 Answers1

0

I have gave up to use variables as I figured out that I can send parameters like query and mutation requests. So I have changed the request so that it now makes queries like this:

new query parameters

And also server accepts that parameter in the type definition of the subscriptions. Now the input parameters of the withFilter filter function (the second callback) are payload and variables which are respectively, data published by a mutation and data retrieved from the subscriber.

So now with the above request, current function works perfect:

        this.resolvers.Subscription[subscription.name] = {
            subscribe: withFilter(_ => this.pubsub.asyncIterator(subscription.name.toUpperCase()), (payload, variables) => {
                console.log(payload, variables);
                return payload[subscription.name]._group === variables._group;
            }),
        };

Although I didn't figure out how to make use of variables sent in request, my problem got solved.

ConductedClever
  • 4,175
  • 2
  • 35
  • 69
  • ... but it doesn't cover eventual security checks (owner compared with logged user - context) ? – xadm Apr 27 '20 at 14:42