Skip to content

Commit

Permalink
Add start command to avoid make subgraphs (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Aug 14, 2024
1 parent 9235eed commit f09a5bc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endef

define RUN_GATEWAY
run-$(1):
(cd gateways/$(1) && ./run.sh $(TEST_SUITE))
npm start -- start --test $(TEST_SUITE) --cwd ./gateways/$(1) --run-script ./run.sh --graphql $(shell jq -r .graphql ./gateways/$(1)/gateway.json) --healthcheck $(shell jq -r .health ./gateways/$(1)/gateway.json)
endef

install:
Expand Down
104 changes: 89 additions & 15 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function resolvePath(
argv: {
cwd: string;
},
path: string,
path: string
) {
if (path.startsWith("/")) {
return path;
Expand All @@ -47,7 +47,7 @@ function resolvePath(
yargs(hideBin(process.argv))
.scriptName("graphql-federation-audit")
.epilogue(
"for more information, find our manual at https://github.com/the-guild-org/federation-compatibility",
"for more information, find our manual at https://github.com/the-guild-org/federation-compatibility"
)
.version(readVersion() ?? "local")
.recommendCommands()
Expand All @@ -68,7 +68,7 @@ yargs(hideBin(process.argv))
async (argv) => {
await serve(argv.port);
console.log("Server started on port", argv.port);
},
}
)
.command(
"supergraph",
Expand All @@ -87,7 +87,7 @@ yargs(hideBin(process.argv))
},
async (argv) => {
const res = await fetch(
`http://localhost:${argv.port}/${argv.test}/supergraph`,
`http://localhost:${argv.port}/${argv.test}/supergraph`
);

if (!res.ok) {
Expand All @@ -99,7 +99,7 @@ yargs(hideBin(process.argv))

writeFileSync(resolvePath(argv, "supergraph.graphql"), await res.text());
process.exit(0);
},
}
)
.command(
"subgraphs",
Expand Down Expand Up @@ -133,7 +133,81 @@ yargs(hideBin(process.argv))

writeFileSync(resolvePath(argv, "subgraphs.json"), await res.text());
process.exit(0);
}
)
.command(
"start",
"start a gateway for a requested test group",
(yargs) => {
return yargs
.option("test", {
describe: "Test group id",
type: "string",
})
.option("run-script", {
describe: "Path to a bash script to run before each test",
type: "string",
})
.option("graphql", {
describe: "GraphQL endpoint serving the supergraph",
type: "string",
})
.option("healthcheck", {
describe: "Health check endpoint",
type: "string",
})
.option("port", {
describe: "Port to bind on",
default: defaultPort,
})
.demandOption("test")
.demandOption("graphql")
.demandOption("healthcheck")
.demandOption("run-script");
},
async (argv) => {
const abortSignal = new AbortController();
process.once("SIGINT", () => {
if (!abortSignal.signal.aborted) {
abortSignal.abort();
}
});
process.once("SIGTERM", () => {
if (!abortSignal.signal.aborted) {
abortSignal.abort();
}
});

const port = argv.port ?? (await getPort());
await serve(port);

process.stdout.write("\n");

await killPortProcess(readPort(argv.graphql)).catch(() => {});

const gatewayExit = Promise.withResolvers<void>();
let gatewayExited = false;
const gateway = spawn("sh", [argv.runScript, argv.test], {
signal: abortSignal.signal,
stdio: "inherit",
cwd: dirname(resolvePath(argv, argv.runScript)),
});

gateway.on("error", (err) => {
if (err.message.includes("aborted")) {
return;
}

process.stderr.write(err.message);
});

gateway.once("exit", () => {
gatewayExited = true;
gatewayExit.resolve();
});

await gatewayExit.promise;
}
)
.command(
"test-suite",
Expand Down Expand Up @@ -204,7 +278,7 @@ yargs(hideBin(process.argv))
resolvePath(argv, `./logs/${argv.test}-gateway.log`),
{
flags: "w+",
},
}
);

const gatewayExit = Promise.withResolvers<void>();
Expand Down Expand Up @@ -240,7 +314,7 @@ yargs(hideBin(process.argv))
} else {
process.exit(0);
}
},
}
)
.command(
"test",
Expand Down Expand Up @@ -328,7 +402,7 @@ yargs(hideBin(process.argv))
resolvePath(argv, `./logs/${id}-gateway.log`),
{
flags: "w+",
},
}
);

const gatewayExit = Promise.withResolvers<void>();
Expand Down Expand Up @@ -384,22 +458,22 @@ yargs(hideBin(process.argv))
process.stdout.write("-----------\n");
process.stdout.write(`Total: ${total}\n`);
process.stdout.write(
`Passed: ${styleText("greenBright", passed + "")}\n`,
`Passed: ${styleText("greenBright", passed + "")}\n`
);
if (failed > 0) {
process.stdout.write(
`Failed: ${styleText("redBright", failed + "")}\n`,
`Failed: ${styleText("redBright", failed + "")}\n`
);
}
process.stdout.write("\n");

if (failed > 0) {
process.stdout.write(
styleText("redBright", "Your gateway is not fully compatible\n"),
styleText("redBright", "Your gateway is not fully compatible\n")
);
} else {
process.stdout.write(
styleText("greenBright", "Your gateway is fully compatible\n"),
styleText("greenBright", "Your gateway is fully compatible\n")
);
}

Expand All @@ -414,14 +488,14 @@ yargs(hideBin(process.argv))
`Passed: ${passed}`,
`Failed: ${failed}`,
])
.join("\n"),
.join("\n")
);

if (argv["exit-on-fail"] && failed > 0) {
process.exit(1);
}
process.exit(0);
},
}
)
.demandCommand(1)
.parse();
Expand All @@ -442,7 +516,7 @@ async function runTest(args: {
resolvePath({ cwd: args.cwd }, `./logs/${args.test}-tests.log`),
{
flags: "w+",
},
}
);

if (args.healthcheck) {
Expand Down
2 changes: 1 addition & 1 deletion src/test-suites/requires-requires/d.subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default createSubgraph("d", {
typeof key.isExpensiveWithDiscount !== "undefined"
) {
return new GraphQLError(
"Product.isExpensiveWithDiscount must be a boolean"
"Product.isExpensiveWithDiscount must be a boolean",
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/test-suites/requires-requires/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default [
canAfford: false,
},
},
}
},
),
createTest(
/* GraphQL */ `
Expand All @@ -31,7 +31,7 @@ export default [
isExpensive: true,
},
},
}
},
),
createTest(
/* GraphQL */ `
Expand All @@ -49,7 +49,7 @@ export default [
canAfford: false,
},
},
}
},
),
createTest(
/* GraphQL */ `
Expand All @@ -65,7 +65,7 @@ export default [
canAffordWithDiscount: true,
},
},
}
},
),
createTest(
/* GraphQL */ `
Expand All @@ -83,6 +83,6 @@ export default [
canAffordWithDiscount: true,
},
},
}
},
),
];

0 comments on commit f09a5bc

Please sign in to comment.