Skip to content

Commit daed24a

Browse files
committed
add new checkActionVisibility action
1 parent b01e1b4 commit daed24a

5 files changed

Lines changed: 97 additions & 10 deletions

File tree

examples/simple/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ broker.createService({
2424
mappingPolicy: "restrict",
2525
},
2626

27+
checkActionVisibility: true,
28+
2729
// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
2830
serverOptions: {},
2931
}),
@@ -78,6 +80,26 @@ broker.createService({
7880
throw new MoleculerClientError("I've said it's a danger action!", 422, "DANGER");
7981
},
8082
},
83+
84+
secret: {
85+
visibility: "protected",
86+
graphql: {
87+
query: "secret: String!",
88+
},
89+
async handler() {
90+
return "! TOP SECRET !";
91+
},
92+
},
93+
94+
visible: {
95+
visibility: "published",
96+
graphql: {
97+
query: "visible: String!",
98+
},
99+
async handler() {
100+
return "Not secret";
101+
},
102+
},
81103
},
82104
});
83105

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ declare module "moleculer-apollo-server" {
4141
};
4242
dataLoader?: boolean;
4343
nullIfError?: boolean;
44+
skipNullKeys?: boolean;
4445
params?: { [key: string]: any };
4546
}
4647

@@ -94,6 +95,7 @@ declare module "moleculer-apollo-server" {
9495
};
9596
routeOptions?: ServiceRouteOptions;
9697
serverOptions?: Config;
98+
checkActionVisibility?: boolean;
9799
autoUpdateSchema?: boolean;
98100
}
99101

package-lock.json

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/service.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = function (mixinOptions) {
2525
createAction: true,
2626
subscriptionEventName: "graphql.publish",
2727
autoUpdateSchema: true,
28+
checkActionVisibility: false,
2829
});
2930

3031
const serviceSchema = {
@@ -450,6 +451,13 @@ module.exports = function (mixinOptions) {
450451

451452
Object.values(service.actions).forEach(action => {
452453
const { graphql: def } = action;
454+
if (
455+
mixinOptions.checkActionVisibility &&
456+
action.visibility != null &&
457+
action.visibility != "published"
458+
)
459+
return;
460+
453461
if (def && _.isObject(def)) {
454462
if (def.query) {
455463
if (!resolver["Query"]) resolver.Query = {};

test/integration/greeter.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ describe("Integration test for greeter service", () => {
2626
mappingPolicy: "restrict",
2727
},
2828

29+
checkActionVisibility: true,
30+
2931
// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
3032
serverOptions: {},
3133
}),
@@ -104,6 +106,16 @@ describe("Integration test for greeter service", () => {
104106
);
105107
},
106108
},
109+
110+
secret: {
111+
visibility: "protected",
112+
graphql: {
113+
query: "secret: String!",
114+
},
115+
async handler() {
116+
return "! TOP SECRET !";
117+
},
118+
},
107119
},
108120
});
109121

@@ -227,4 +239,41 @@ describe("Integration test for greeter service", () => {
227239
],
228240
});
229241
});
242+
243+
it("should not call the greeter.secret", async () => {
244+
const res = await fetch(`http://localhost:${port}/graphql`, {
245+
method: "post",
246+
body: JSON.stringify({
247+
operationName: null,
248+
variables: {},
249+
query: "query { danger }",
250+
}),
251+
headers: { "Content-Type": "application/json" },
252+
});
253+
254+
expect(res.status).toBe(200);
255+
expect(await res.json()).toStrictEqual({
256+
data: null,
257+
errors: [
258+
{
259+
extensions: {
260+
code: "INTERNAL_SERVER_ERROR",
261+
exception: {
262+
code: 422,
263+
retryable: false,
264+
type: "DANGER",
265+
},
266+
},
267+
locations: [
268+
{
269+
column: 9,
270+
line: 1,
271+
},
272+
],
273+
message: "I've said it's a danger action!",
274+
path: ["danger"],
275+
},
276+
],
277+
});
278+
});
230279
});

0 commit comments

Comments
 (0)