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

Fix XS missing 'ctx' into context #73

Merged
merged 6 commits into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ module.exports = function(mixinOptions) {
});

const serviceSchema = {
actions: {
ws: {
visibility: "private",
tracing: {
tags: {
params: ["socket.upgradeReq.url"],
},
spanName: ctx => `UPGRADE ${ctx.params.socket.upgradeReq.url}`,
},
handler(ctx) {
const { socket, connectionParams } = ctx.params;
return {
$ctx: ctx,
$socket: socket,
$service: this,
$params: { body: connectionParams, query: socket.upgradeReq.query },
};
},
},
},

events: {
"$services.changed"() {
if (mixinOptions.autoUpdateSchema) {
Expand Down Expand Up @@ -303,14 +324,14 @@ 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: (payload, params, { ctx }) =>
ctx.call(actionName, { ...params, payload }),
};
},

Expand Down Expand Up @@ -586,23 +607,23 @@ module.exports = function(mixinOptions) {
this.apolloServer = new ApolloServer({
schema,
..._.defaultsDeep({}, mixinOptions.serverOptions, {
context: ({ req, connection }) => {
return req
context: ({ req, connection }) => ({
...(req
? {
ctx: req.$ctx,
service: req.$service,
params: req.$params,
dataLoaders: new Map(), // create an empty map to load DataLoader instances into
}
: {
service: connection.$service,
};
},
ctx: connection.context.$ctx,
service: connection.context.$service,
params: connection.context.$params,
}),
dataLoaders: new Map(), // create an empty map to load DataLoader instances into
}),
subscriptions: {
onConnect: connectionParams => ({
...connectionParams,
$service: this,
}),
onConnect: (connectionParams, socket) =>
this.actions.ws({ connectionParams, socket }),
},
}),
});
Expand Down Expand Up @@ -724,6 +745,7 @@ module.exports = function(mixinOptions) {

if (mixinOptions.createAction) {
serviceSchema.actions = {
...serviceSchema.actions,
graphql: {
params: {
query: { type: "string" },
Expand Down
47 changes: 29 additions & 18 deletions test/unit/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 } });
});
});

Expand Down Expand Up @@ -1442,10 +1442,19 @@ describe("Test Service", () => {
expect(
contextFn({
connection: {
$service: "service",
context: {
$service: "service",
$ctx: "context",
$params: { a: 5 },
},
},
})
).toEqual({
ctx: "context",
dataLoaders: new Map(),
params: {
a: 5,
},
service: "service",
});

Expand Down Expand Up @@ -1474,11 +1483,13 @@ describe("Test Service", () => {
const onConnect = ApolloServer.mock.calls[0][0].subscriptions.onConnect;

const connectionParams = { b: 100 };
const socket = { connectionParams, upgradeReq: { query: 101 } };
const connect = await onConnect(connectionParams, socket);

expect(onConnect(connectionParams)).toEqual({
b: 100,
$service: svc,
});
expect(connect.$service).toEqual(svc);
expect(connect.$ctx).toBeDefined();
expect(connect.$params.body).toEqual(connectionParams);
expect(connect.$params.query).toEqual(socket.upgradeReq.query);

await stop();
});
Expand Down