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 4 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
59 changes: 40 additions & 19 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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
Hugome marked this conversation as resolved.
Show resolved Hide resolved
socket.$ctx = ctx;
socket.$params = { body: connectionParams, query: socket.upgradeReq.query };
return connectionParams;
}
}
},

events: {
"$services.changed"() {
if (mixinOptions.autoUpdateSchema) {
Expand Down Expand Up @@ -303,14 +323,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 })
Hugome marked this conversation as resolved.
Show resolved Hide resolved
};
},

Expand Down Expand Up @@ -586,22 +605,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
}
: {
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 }),
shawnmcknight marked this conversation as resolved.
Show resolved Hide resolved
socket,
$service: this
}),
},
}),
Expand Down Expand Up @@ -724,6 +744,7 @@ module.exports = function(mixinOptions) {

if (mixinOptions.createAction) {
serviceSchema.actions = {
...serviceSchema.actions,
graphql: {
params: {
query: { type: "string" },
Expand Down
52 changes: 33 additions & 19 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,21 @@ describe("Test Service", () => {
expect(
contextFn({
connection: {
$service: "service",
context: {
$service: "service",
socket: {
$ctx: "context",
Hugome marked this conversation as resolved.
Show resolved Hide resolved
$params: { a: 5 },
},
},
},
})
).toEqual({
ctx: "context",
dataLoaders: new Map(),
params: {
a: 5,
},
service: "service",
});

Expand Down Expand Up @@ -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();
});
Expand Down