From 6fe96316d85e3267a274f8ea631d8e0df6cfc484 Mon Sep 17 00:00:00 2001 From: Hugome Date: Sat, 25 Apr 2020 20:21:15 +0200 Subject: [PATCH 1/6] Fix XS missing 'ctx' into context --- src/service.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/service.js b/src/service.js index 32b07d5..6d71fd2 100644 --- a/src/service.js +++ b/src/service.js @@ -7,7 +7,8 @@ "use strict"; const _ = require("lodash"); -const { MoleculerServerError } = require("moleculer").Errors; +const { Context, Errors } = require("moleculer"); +const { MoleculerServerError } = Errors; const { ApolloServer } = require("./ApolloServer"); const DataLoader = require("dataloader"); const { makeExecutableSchema } = require("graphql-tools"); @@ -595,6 +596,7 @@ module.exports = function(mixinOptions) { dataLoaders: new Map(), // create an empty map to load DataLoader instances into } : { + ctx: Context.create(this.broker), service: connection.$service, }; }, From a84262086949ac5e13ba468067412928a393c75d Mon Sep 17 00:00:00 2001 From: Hugome Date: Mon, 27 Apr 2020 20:06:23 +0200 Subject: [PATCH 2/6] Improve ws context --- src/service.js | 53 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/service.js b/src/service.js index 6d71fd2..ce6b78b 100644 --- a/src/service.js +++ b/src/service.js @@ -29,6 +29,26 @@ module.exports = function(mixinOptions) { }); const serviceSchema = { + actions: { + ws: { + visibility: "private", + tracing: { + tags: { + params: [ + "socket.upgradeReq.url" + ] + }, + spanName: ctx => `UPGRADE ${ctx.params.socket.upgradeReq}` + }, + handler(ctx) { + const { socket, connectionParams } = ctx.params + socket.$ctx = ctx; + socket.$params = { body: connectionParams, query: socket.upgradeReq.query }; + return connectionParams; + } + } + }, + events: { "$services.changed"() { if (mixinOptions.autoUpdateSchema) { @@ -587,23 +607,23 @@ module.exports = function(mixinOptions) { this.apolloServer = new ApolloServer({ schema, ..._.defaultsDeep({}, mixinOptions.serverOptions, { - context: ({ req, connection }) => { - return req - ? { - ctx: req.$ctx, - service: req.$service, - params: req.$params, - dataLoaders: new Map(), // create an empty map to load DataLoader instances into - } - : { - ctx: Context.create(this.broker), - service: connection.$service, - }; - }, + context: ({ req, connection }) => ({ + ...(req ? { + ctx: req.$ctx, + service: req.$service, + params: req.$params, + } : { + ctx: connection.context.socket.$ctx, + service: connection.context.$service, + params: connection.context.socket.$params, + }), + dataLoaders: new Map(), // create an empty map to load DataLoader instances into + }), subscriptions: { - onConnect: connectionParams => ({ - ...connectionParams, - $service: this, + onConnect: async (connectionParams, socket) => ({ + ...await this.actions.ws({ connectionParams, socket }), + socket, + $service: this }), }, }), @@ -726,6 +746,7 @@ module.exports = function(mixinOptions) { if (mixinOptions.createAction) { serviceSchema.actions = { + ...serviceSchema.actions, graphql: { params: { query: { type: "string" }, From b865ef93457734a5a4c31f28458601d1b2f9061d Mon Sep 17 00:00:00 2001 From: Hugome Date: Mon, 27 Apr 2020 20:11:01 +0200 Subject: [PATCH 3/6] createAsyncIteratorResolver use a real ctx intead of broker.call --- src/service.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/service.js b/src/service.js index ce6b78b..772be7b 100644 --- a/src/service.js +++ b/src/service.js @@ -324,14 +324,13 @@ module.exports = function(mixinOptions) { subscribe: filter ? withFilter( () => this.pubsub.asyncIterator(tags), - async (payload, params, ctx) => + async (payload, params, { ctx }) => payload !== undefined - ? this.broker.call(filter, { ...params, payload }, ctx) + ? ctx.call(filter, { ...params, payload }) : false ) : () => this.pubsub.asyncIterator(tags), - resolve: async (payload, params, ctx) => - this.broker.call(actionName, { ...params, payload }, ctx), + resolve: async (payload, params, { ctx }) => ctx.call(actionName, { ...params, payload }) }; }, From 7eaac26d43f24c1b5f3499c4c356657bb9055fdd Mon Sep 17 00:00:00 2001 From: Hugome Date: Mon, 27 Apr 2020 20:44:23 +0200 Subject: [PATCH 4/6] Adapt tests --- src/service.js | 3 +-- test/unit/service.spec.js | 52 +++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/service.js b/src/service.js index 772be7b..8382ea4 100644 --- a/src/service.js +++ b/src/service.js @@ -7,8 +7,7 @@ "use strict"; const _ = require("lodash"); -const { Context, Errors } = require("moleculer"); -const { MoleculerServerError } = Errors; +const { MoleculerServerError } = require("moleculer").Errors; const { ApolloServer } = require("./ApolloServer"); const DataLoader = require("dataloader"); const { makeExecutableSchema } = require("graphql-tools"); diff --git a/test/unit/service.spec.js b/test/unit/service.spec.js index e915129..250c9d6 100644 --- a/test/unit/service.spec.js +++ b/test/unit/service.spec.js @@ -965,11 +965,12 @@ describe("Test Service", () => { // Test resolve const ctx = new Context(broker); - const res3 = await res.resolve({ a: 5 }, { b: "John" }, ctx); + ctx.call = jest.fn(async () => "action response"); + const res3 = await res.resolve({ a: 5 }, { b: "John" }, { ctx }); expect(res3).toBe("action response"); - expect(broker.call).toBeCalledTimes(1); - expect(broker.call).toBeCalledWith("posts.find", { b: "John", payload: { a: 5 } }, ctx); + expect(ctx.call).toBeCalledTimes(1); + expect(ctx.call).toBeCalledWith("posts.find", { b: "John", payload: { a: 5 } }); }); it("should create resolver with tags", async () => { @@ -1003,24 +1004,23 @@ describe("Test Service", () => { }); // Test first function - expect(res.subscribe[0]()).toBe("iterator-result"); + const ctx = new Context(broker); + expect(res.subscribe[0](undefined, undefined, { ctx })).toBe("iterator-result"); expect(svc.pubsub.asyncIterator).toBeCalledTimes(1); expect(svc.pubsub.asyncIterator).toBeCalledWith(["a", "b"]); // Test second function without payload - expect(await res.subscribe[1]()).toBe(false); + expect(await res.subscribe[1](undefined, undefined, { ctx })).toBe(false); // Test second function with payload - const ctx = new Context(broker); - expect(await res.subscribe[1]({ a: 5 }, { b: "John" }, ctx)).toBe("action response"); - - expect(broker.call).toBeCalledTimes(1); - expect(broker.call).toBeCalledWith( - "posts.filter", - { b: "John", payload: { a: 5 } }, - ctx + ctx.call = jest.fn(async () => "action response"); + expect(await res.subscribe[1]({ a: 5 }, { b: "John" }, { ctx })).toBe( + "action response" ); + + expect(ctx.call).toBeCalledTimes(1); + expect(ctx.call).toBeCalledWith("posts.filter", { b: "John", payload: { a: 5 } }); }); }); @@ -1442,10 +1442,21 @@ describe("Test Service", () => { expect( contextFn({ connection: { - $service: "service", + context: { + $service: "service", + socket: { + $ctx: "context", + $params: { a: 5 }, + }, + }, }, }) ).toEqual({ + ctx: "context", + dataLoaders: new Map(), + params: { + a: 5, + }, service: "service", }); @@ -1474,11 +1485,14 @@ describe("Test Service", () => { const onConnect = ApolloServer.mock.calls[0][0].subscriptions.onConnect; const connectionParams = { b: 100 }; - - expect(onConnect(connectionParams)).toEqual({ - b: 100, - $service: svc, - }); + const socket = { connectionParams, upgradeReq: { query: 101 } }; + const connect = await onConnect(connectionParams, socket); + + expect(connect.$service).toEqual(svc); + expect(connect.b).toEqual(100); + expect(socket.$ctx).toBeDefined(); + expect(socket.$params.body).toEqual(connectionParams); + expect(socket.$params.query).toEqual(socket.upgradeReq.query); await stop(); }); From 566371fbb8acfeb29577a63e5b96a50ec823df01 Mon Sep 17 00:00:00 2001 From: Hugome Date: Mon, 4 May 2020 09:03:45 +0200 Subject: [PATCH 5/6] Repair lint/prettier --- src/service.js | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/service.js b/src/service.js index 8382ea4..b7abd04 100644 --- a/src/service.js +++ b/src/service.js @@ -33,19 +33,17 @@ module.exports = function(mixinOptions) { visibility: "private", tracing: { tags: { - params: [ - "socket.upgradeReq.url" - ] + params: ["socket.upgradeReq.url"], }, - spanName: ctx => `UPGRADE ${ctx.params.socket.upgradeReq}` + spanName: ctx => `UPGRADE ${ctx.params.socket.upgradeReq.url}`, }, handler(ctx) { - const { socket, connectionParams } = ctx.params + const { socket, connectionParams } = ctx.params; socket.$ctx = ctx; socket.$params = { body: connectionParams, query: socket.upgradeReq.query }; return connectionParams; - } - } + }, + }, }, events: { @@ -329,7 +327,8 @@ module.exports = function(mixinOptions) { : false ) : () => this.pubsub.asyncIterator(tags), - resolve: async (payload, params, { ctx }) => ctx.call(actionName, { ...params, payload }) + resolve: (payload, params, { ctx }) => + ctx.call(actionName, { ...params, payload }), }; }, @@ -606,22 +605,24 @@ module.exports = function(mixinOptions) { schema, ..._.defaultsDeep({}, mixinOptions.serverOptions, { context: ({ req, connection }) => ({ - ...(req ? { - ctx: req.$ctx, - service: req.$service, - params: req.$params, - } : { - ctx: connection.context.socket.$ctx, - service: connection.context.$service, - params: connection.context.socket.$params, - }), + ...(req + ? { + ctx: req.$ctx, + service: req.$service, + params: req.$params, + } + : { + ctx: connection.context.socket.$ctx, + service: connection.context.$service, + params: connection.context.socket.$params, + }), dataLoaders: new Map(), // create an empty map to load DataLoader instances into }), subscriptions: { onConnect: async (connectionParams, socket) => ({ - ...await this.actions.ws({ connectionParams, socket }), + ...(await this.actions.ws({ connectionParams, socket })), socket, - $service: this + $service: this, }), }, }), From 4fc587736b2b9a1bfe1e3d7e8f60610f9ab15b24 Mon Sep 17 00:00:00 2001 From: Hugome Date: Thu, 4 Jun 2020 16:51:04 +0200 Subject: [PATCH 6/6] Improve the construction of WS context --- src/service.js | 20 ++++++++++---------- test/unit/service.spec.js | 13 +++++-------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/service.js b/src/service.js index b7abd04..d433e20 100644 --- a/src/service.js +++ b/src/service.js @@ -39,9 +39,12 @@ module.exports = function(mixinOptions) { }, handler(ctx) { const { socket, connectionParams } = ctx.params; - socket.$ctx = ctx; - socket.$params = { body: connectionParams, query: socket.upgradeReq.query }; - return connectionParams; + return { + $ctx: ctx, + $socket: socket, + $service: this, + $params: { body: connectionParams, query: socket.upgradeReq.query }, + }; }, }, }, @@ -612,18 +615,15 @@ module.exports = function(mixinOptions) { params: req.$params, } : { - ctx: connection.context.socket.$ctx, + ctx: connection.context.$ctx, service: connection.context.$service, - params: connection.context.socket.$params, + params: connection.context.$params, }), dataLoaders: new Map(), // create an empty map to load DataLoader instances into }), subscriptions: { - onConnect: async (connectionParams, socket) => ({ - ...(await this.actions.ws({ connectionParams, socket })), - socket, - $service: this, - }), + onConnect: (connectionParams, socket) => + this.actions.ws({ connectionParams, socket }), }, }), }); diff --git a/test/unit/service.spec.js b/test/unit/service.spec.js index 250c9d6..b5b8e64 100644 --- a/test/unit/service.spec.js +++ b/test/unit/service.spec.js @@ -1444,10 +1444,8 @@ describe("Test Service", () => { connection: { context: { $service: "service", - socket: { - $ctx: "context", - $params: { a: 5 }, - }, + $ctx: "context", + $params: { a: 5 }, }, }, }) @@ -1489,10 +1487,9 @@ describe("Test Service", () => { const connect = await onConnect(connectionParams, socket); expect(connect.$service).toEqual(svc); - expect(connect.b).toEqual(100); - expect(socket.$ctx).toBeDefined(); - expect(socket.$params.body).toEqual(connectionParams); - expect(socket.$params.query).toEqual(socket.upgradeReq.query); + expect(connect.$ctx).toBeDefined(); + expect(connect.$params.body).toEqual(connectionParams); + expect(connect.$params.query).toEqual(socket.upgradeReq.query); await stop(); });