Skip to content

Commit

Permalink
add new checkActionVisibility action
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Oct 4, 2022
1 parent b01e1b4 commit daed24a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
22 changes: 22 additions & 0 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ broker.createService({
mappingPolicy: "restrict",
},

checkActionVisibility: true,

// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
serverOptions: {},
}),
Expand Down Expand Up @@ -78,6 +80,26 @@ broker.createService({
throw new MoleculerClientError("I've said it's a danger action!", 422, "DANGER");
},
},

secret: {
visibility: "protected",
graphql: {
query: "secret: String!",
},
async handler() {
return "! TOP SECRET !";
},
},

visible: {
visibility: "published",
graphql: {
query: "visible: String!",
},
async handler() {
return "Not secret";
},
},
},
});

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare module "moleculer-apollo-server" {
};
dataLoader?: boolean;
nullIfError?: boolean;
skipNullKeys?: boolean;
params?: { [key: string]: any };
}

Expand Down Expand Up @@ -94,6 +95,7 @@ declare module "moleculer-apollo-server" {
};
routeOptions?: ServiceRouteOptions;
serverOptions?: Config;
checkActionVisibility?: boolean;
autoUpdateSchema?: boolean;
}

Expand Down
26 changes: 16 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = function (mixinOptions) {
createAction: true,
subscriptionEventName: "graphql.publish",
autoUpdateSchema: true,
checkActionVisibility: false,
});

const serviceSchema = {
Expand Down Expand Up @@ -450,6 +451,13 @@ module.exports = function (mixinOptions) {

Object.values(service.actions).forEach(action => {
const { graphql: def } = action;
if (
mixinOptions.checkActionVisibility &&
action.visibility != null &&
action.visibility != "published"
)
return;

if (def && _.isObject(def)) {
if (def.query) {
if (!resolver["Query"]) resolver.Query = {};
Expand Down
49 changes: 49 additions & 0 deletions test/integration/greeter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe("Integration test for greeter service", () => {
mappingPolicy: "restrict",
},

checkActionVisibility: true,

// https://www.apollographql.com/docs/apollo-server/v2/api/apollo-server.html
serverOptions: {},
}),
Expand Down Expand Up @@ -104,6 +106,16 @@ describe("Integration test for greeter service", () => {
);
},
},

secret: {
visibility: "protected",
graphql: {
query: "secret: String!",
},
async handler() {
return "! TOP SECRET !";
},
},
},
});

Expand Down Expand Up @@ -227,4 +239,41 @@ describe("Integration test for greeter service", () => {
],
});
});

it("should not call the greeter.secret", async () => {
const res = await fetch(`http://localhost:${port}/graphql`, {
method: "post",
body: JSON.stringify({
operationName: null,
variables: {},
query: "query { danger }",
}),
headers: { "Content-Type": "application/json" },
});

expect(res.status).toBe(200);
expect(await res.json()).toStrictEqual({
data: null,
errors: [
{
extensions: {
code: "INTERNAL_SERVER_ERROR",
exception: {
code: 422,
retryable: false,
type: "DANGER",
},
},
locations: [
{
column: 9,
line: 1,
},
],
message: "I've said it's a danger action!",
path: ["danger"],
},
],
});
});
});

0 comments on commit daed24a

Please sign in to comment.