How to use the moleculer.Context function in moleculer

To help you get started, we’ve selected a few moleculer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github moleculerjs / moleculer-apollo-server / test / unit / service.spec.js View on Github external
expect(res).toEqual({
				subscribe: [expect.any(Function), expect.any(Function)],
				resolve: expect.any(Function),
			});

			// Test first function
			expect(res.subscribe[0]()).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);

			// 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
			);
		});
	});
github moleculerjs / moleculer-apollo-server / test / unit / service.spec.js View on Github external
const res = svc.createAsyncIteratorResolver("posts.find");

			expect(res).toEqual({
				subscribe: expect.any(Function),
				resolve: expect.any(Function),
			});

			// Test subscribe
			const res2 = res.subscribe();

			expect(res2).toBe("iterator-result");
			expect(svc.pubsub.asyncIterator).toBeCalledTimes(1);
			expect(svc.pubsub.asyncIterator).toBeCalledWith([]);

			// Test resolve
			const ctx = new Context(broker);
			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);
		});
github moleculerjs / moleculer-apollo-server / test / unit / service.spec.js View on Github external
it("should make multiple loaders for multiple calls with different args", async () => {
			const resolver = svc.createActionResolver("users.resolve", {
				rootParams: {
					author: "id",
				},

				dataLoader: true,
			});

			const ctx = new Context(broker);
			ctx.call = jest
				.fn()
				.mockResolvedValueOnce(["res1", "res2", "res5"])
				.mockResolvedValueOnce(["res3", "res4", "res6"]);

			const fakeRoot1 = { author: [1, 2, 5] };
			const fakeRoot2 = { author: [3, 4, 6] };

			const fakeContext = { ctx, dataLoaders: new Map() };
			const res1 = await resolver(fakeRoot1, { a: 5 }, fakeContext);
			expect(fakeContext.dataLoaders.size).toBe(1);
			const res2 = await resolver(fakeRoot2, { a: 10 }, fakeContext);
			expect(fakeContext.dataLoaders.size).toBe(2);

			expect(res1).toEqual(["res1", "res2", "res5"]);
			expect(res2).toEqual(["res3", "res4", "res6"]);