Skip to content

Commit

Permalink
Merge pull request #91 from arash16/patch-1
Browse files Browse the repository at this point in the history
stop previous apolloServer before creating a new one
  • Loading branch information
icebob committed Mar 21, 2021
2 parents 1af1c37 + dc915d3 commit f80ac10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
18 changes: 11 additions & 7 deletions src/service.js
Expand Up @@ -607,12 +607,16 @@ module.exports = function (mixinOptions) {
/**
* Prepare GraphQL schemas based on Moleculer services.
*/
prepareGraphQLSchema() {
async prepareGraphQLSchema() {
// Schema is up-to-date
if (!this.shouldUpdateGraphqlSchema && this.graphqlHandler) {
return;
}

if (this.apolloServer) {
await this.apolloServer.stop();
}

// Create new server & regenerate GraphQL schema
this.logger.info(
"♻ Recreate Apollo GraphQL server and regenerate GraphQL schema..."
Expand Down Expand Up @@ -730,17 +734,17 @@ module.exports = function (mixinOptions) {

const route = _.defaultsDeep(mixinOptions.routeOptions, {
aliases: {
"/"(req, res) {
async "/"(req, res) {
try {
this.prepareGraphQLSchema();
await this.prepareGraphQLSchema();
return this.graphqlHandler(req, res);
} catch (err) {
this.sendError(req, res, err);
}
},
"GET /.well-known/apollo/server-health"(req, res) {
async "GET /.well-known/apollo/server-health"(req, res) {
try {
this.prepareGraphQLSchema();
await this.prepareGraphQLSchema();
} catch (err) {
res.statusCode = 503;
return this.sendResponse(
Expand Down Expand Up @@ -779,8 +783,8 @@ module.exports = function (mixinOptions) {
query: { type: "string" },
variables: { type: "object", optional: true },
},
handler(ctx) {
this.prepareGraphQLSchema();
async handler(ctx) {
await this.prepareGraphQLSchema();
return GraphQL.graphql(
this.graphqlSchema,
ctx.params.query,
Expand Down
32 changes: 16 additions & 16 deletions test/unit/service.spec.js
Expand Up @@ -35,7 +35,7 @@ async function startService(mixinOptions, baseSchema) {
describe("Test Service", () => {
describe("Test created handler", () => {
it("should register a route with default options", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

expect(svc.shouldUpdateGraphqlSchema).toBe(true);

Expand All @@ -59,15 +59,15 @@ describe("Test Service", () => {

describe("Test `/` route handler", () => {
it("should prepare graphql schema & call handler", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

// Test `/` alias
svc.prepareGraphQLSchema = jest.fn();
svc.graphqlHandler = jest.fn(() => "result");
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);
const res = await svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);

expect(res).toBe("result");
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
Expand All @@ -78,7 +78,7 @@ describe("Test Service", () => {
});

it("should call sendError if error occured", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

const err = new Error("Something happened");
svc.sendError = jest.fn();
Expand All @@ -89,7 +89,7 @@ describe("Test Service", () => {
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);
const res = await svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);

expect(res).toBeUndefined();
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
Expand All @@ -103,15 +103,15 @@ describe("Test Service", () => {

describe("Test `GET /.well-known/apollo/server-health` route handler", () => {
it("should prepare graphql schema & call handler", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

// Test `/` alias
svc.prepareGraphQLSchema = jest.fn();
svc.graphqlHandler = jest.fn(() => "result");
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = svc.settings.routes[0].aliases[
const res = await svc.settings.routes[0].aliases[
"GET /.well-known/apollo/server-health"
].call(svc, fakeReq, fakeRes);

Expand All @@ -124,7 +124,7 @@ describe("Test Service", () => {
});

it("should call sendError if error occured", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

const err = new Error("Something happened");
svc.sendResponse = jest.fn();
Expand All @@ -135,7 +135,7 @@ describe("Test Service", () => {
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = svc.settings.routes[0].aliases[
const res = await svc.settings.routes[0].aliases[
"GET /.well-known/apollo/server-health"
].call(svc, fakeReq, fakeRes);

Expand All @@ -160,7 +160,7 @@ describe("Test Service", () => {
});

it("should register a route with custom options", async () => {
const { broker, svc, stop } = await startService({
const { svc, stop } = await startService({
routeOptions: {
path: "/apollo-server",

Expand Down Expand Up @@ -314,7 +314,7 @@ describe("Test Service", () => {
describe("Test methods", () => {
describe("Test 'invalidateGraphQLSchema'", () => {
it("should create the 'graphql' action", async () => {
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

svc.shouldUpdateGraphqlSchema = false;

Expand Down Expand Up @@ -395,7 +395,7 @@ describe("Test Service", () => {
it("should call actionResolvers", async () => {
const { svc, stop } = await startService();

svc.createActionResolver = jest.fn((name, r) => jest.fn());
svc.createActionResolver = jest.fn(() => jest.fn());

const resolvers = {
author: {
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe("Test Service", () => {
describe("Test 'generateGraphQLSchema'", () => {
it("should create an empty schema", async () => {
makeExecutableSchema.mockImplementation(() => "generated-schema");
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

const res = svc.generateGraphQLSchema([]);
expect(res).toBe("generated-schema");
Expand All @@ -1048,7 +1048,7 @@ describe("Test Service", () => {
it("should create a schema with schemaDirectives", async () => {
makeExecutableSchema.mockClear();
const UniqueIdDirective = jest.fn();
const { broker, svc, stop } = await startService({
const { svc, stop } = await startService({
schemaDirectives: {
uid: UniqueIdDirective,
},
Expand Down Expand Up @@ -1081,7 +1081,7 @@ describe("Test Service", () => {
},
},
};
const { broker, svc, stop } = await startService({
const { svc, stop } = await startService({
typeDefs: `
scalar Date
`,
Expand Down Expand Up @@ -1310,7 +1310,7 @@ describe("Test Service", () => {
makeExecutableSchema.mockImplementation(() => {
throw new Error("Something is wrong");
});
const { broker, svc, stop } = await startService();
const { svc, stop } = await startService();

expect(() => svc.generateGraphQLSchema([])).toThrow(Errors.MoleculerServerError);

Expand Down

0 comments on commit f80ac10

Please sign in to comment.