Skip to content

Commit 95cce43

Browse files
committed
remove default prepareContextParams method
1 parent 0252409 commit 95cce43

4 files changed

Lines changed: 49 additions & 76 deletions

File tree

.eslintrc.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = {
1414
},
1515
plugins: ["node", "promise", "security"],
1616
rules: {
17-
quotes: ["warn", "double"],
1817
semi: ["error", "always"],
1918
"no-var": ["error"],
2019
"no-console": ["error"],
@@ -28,14 +27,5 @@ module.exports = {
2827
"no-process-exit": ["off"],
2928
"node/no-unpublished-require": 0,
3029
"require-atomic-updates": 0,
31-
"space-before-function-paren": [
32-
"warn",
33-
{
34-
anonymous: "never",
35-
named: "never",
36-
asyncArrow: "always",
37-
},
38-
],
39-
"object-curly-spacing": ["warn", "always"],
4030
},
4131
};

src/service.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const GraphQL = require("graphql");
1515
const { PubSub, withFilter } = require("graphql-subscriptions");
1616
const hash = require("object-hash");
1717

18-
module.exports = function(mixinOptions) {
18+
module.exports = function (mixinOptions) {
1919
mixinOptions = _.defaultsDeep(mixinOptions, {
2020
routeOptions: {
2121
path: "/graphql",
@@ -266,17 +266,6 @@ module.exports = function(mixinOptions) {
266266
};
267267
},
268268

269-
/**
270-
* Prepare the context params before calling the action
271-
* @param {Object} params
272-
* @param {String} actionName
273-
* @param {Context} context
274-
* @returns {Promise<Object>}
275-
*/
276-
prepareContextParams(params, actionName, context) {
277-
return Promise.resolve(params);
278-
},
279-
280269
/**
281270
* Get the unique key assigned to the DataLoader map
282271
* @param {string} actionName - Fully qualified action name to bind to dataloader
@@ -361,6 +350,7 @@ module.exports = function(mixinOptions) {
361350
* @returns {Object} Generated schema
362351
*/
363352
generateGraphQLSchema(services) {
353+
let str;
364354
try {
365355
let typeDefs = [];
366356
let resolvers = {};
@@ -533,7 +523,7 @@ module.exports = function(mixinOptions) {
533523
enums.length > 0 ||
534524
inputs.length > 0
535525
) {
536-
let str = "";
526+
str = "";
537527
if (queries.length > 0) {
538528
str += `
539529
type Query {
@@ -597,7 +587,7 @@ module.exports = function(mixinOptions) {
597587
"Unable to compile GraphQL schema",
598588
500,
599589
"UNABLE_COMPILE_GRAPHQL_SCHEMA",
600-
{ err }
590+
{ err, str }
601591
);
602592
}
603593
},

test/integration/greeter.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ describe("Integration test for greeter service", () => {
3434
settings: {
3535
port: 0, // Random
3636
},
37+
38+
methods: {
39+
prepareContextParams(params, actionName) {
40+
if (actionName === "greeter.replace" && params.input) {
41+
return params.input;
42+
}
43+
return params;
44+
},
45+
},
3746
});
3847

3948
broker.createService({
@@ -68,6 +77,21 @@ describe("Integration test for greeter service", () => {
6877
},
6978
},*/
7079

80+
replace: {
81+
graphql: {
82+
input: `input GreeterInput {
83+
name: String!
84+
}`,
85+
type: `type GreeterOutput {
86+
name: String
87+
}`,
88+
mutation: "replace(input: GreeterInput!): GreeterOutput",
89+
},
90+
handler(ctx) {
91+
return ctx.params;
92+
},
93+
},
94+
7195
danger: {
7296
graphql: {
7397
query: "danger: String!",
@@ -146,6 +170,27 @@ describe("Integration test for greeter service", () => {
146170
});
147171
});
148172

173+
it("should call the greeter.welcome action with wrapped input params", async () => {
174+
const res = await fetch(`http://localhost:${port}/graphql`, {
175+
method: "post",
176+
body: JSON.stringify({
177+
operationName: null,
178+
variables: { name: "Moleculer GraphQL" },
179+
query: "mutation ($name: String!) { replace(input: { name: $name }) { name } }",
180+
}),
181+
headers: { "Content-Type": "application/json" },
182+
});
183+
184+
expect(res.status).toBe(200);
185+
expect(await res.json()).toStrictEqual({
186+
data: {
187+
replace: {
188+
name: "Moleculer GraphQL",
189+
},
190+
},
191+
});
192+
});
193+
149194
it("should call the greeter.danger and receives an error", async () => {
150195
const res = await fetch(`http://localhost:${port}/graphql`, {
151196
method: "post",

test/unit/service.spec.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -391,58 +391,6 @@ describe("Test Service", () => {
391391
});
392392
});
393393

394-
describe("Test 'prepareContextParams' method", () => {
395-
let broker, svc, stop;
396-
397-
beforeAll(async () => {
398-
const res = await startService();
399-
broker = res.broker;
400-
svc = res.svc;
401-
stop = res.stop;
402-
});
403-
404-
afterAll(async () => await stop());
405-
406-
it("should return the context params", async () => {
407-
const params = {
408-
a: 5,
409-
};
410-
411-
expect(await svc.prepareContextParams(params)).toEqual({ a: 5 });
412-
});
413-
414-
it("should return the modified params", async () => {
415-
svc.prepareContextParams = jest.fn(params => {
416-
return {
417-
...params,
418-
b: 10,
419-
};
420-
});
421-
422-
const params = {
423-
a: 5,
424-
};
425-
426-
expect(await svc.prepareContextParams(params)).toEqual({ a: 5, b: 10 });
427-
428-
expect(svc.prepareContextParams).toBeCalledTimes(1);
429-
expect(svc.prepareContextParams).toBeCalledWith(params);
430-
});
431-
432-
it("should return the unwrapped params", async () => {
433-
svc.prepareContextParams = jest.fn(async params => params.input);
434-
435-
const params = {
436-
input: { a: 5 },
437-
};
438-
439-
expect(await svc.prepareContextParams(params)).toEqual({ a: 5 });
440-
441-
expect(svc.prepareContextParams).toBeCalledTimes(1);
442-
expect(svc.prepareContextParams).toBeCalledWith(params);
443-
});
444-
});
445-
446394
describe("Test 'createServiceResolvers'", () => {
447395
it("should call actionResolvers", async () => {
448396
const { svc, stop } = await startService();

0 commit comments

Comments
 (0)