1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
5 |
|
6 | const yargs = _interopDefault(require('yargs'));
|
7 | const helpers = require('yargs/helpers');
|
8 | const graphqlConfig = require('graphql-config');
|
9 | const codeFileLoader = require('@graphql-tools/code-file-loader');
|
10 | const node = require('@graphql-yoga/node');
|
11 | const mock = require('@graphql-tools/mock');
|
12 |
|
13 | const terminateEvents = ['SIGINT', 'SIGTERM'];
|
14 | function registerTerminateHandler(callback) {
|
15 | for (const eventName of terminateEvents) {
|
16 | process.on(eventName, () => callback(eventName));
|
17 | }
|
18 | }
|
19 | const YogaExtensions = (api) => {
|
20 | const codeFileLoader$1 = new codeFileLoader.CodeFileLoader({
|
21 | noPluck: true,
|
22 | });
|
23 | api.loaders.schema.register(codeFileLoader$1);
|
24 | api.loaders.documents.register(codeFileLoader$1);
|
25 | return {
|
26 | name: 'Yoga',
|
27 | };
|
28 | };
|
29 | function graphqlYoga() {
|
30 | return yargs(helpers.hideBin(process.argv)).command('$0', 'Serves GraphQL over HTTP using your GraphQL Config', (builder) => {
|
31 | builder.option('project', {
|
32 | type: 'string',
|
33 | description: 'Project name',
|
34 | });
|
35 | builder.option('mock', {
|
36 | type: 'boolean',
|
37 | description: 'Mock the given schema',
|
38 | });
|
39 | }, async ({ project = 'default', mock: mock$1 }) => {
|
40 | console.info(`Loading GraphQL Config from ${process.cwd()}`);
|
41 | const config = await graphqlConfig.loadConfig({
|
42 | extensions: [YogaExtensions],
|
43 | throwOnMissing: false,
|
44 | throwOnEmpty: false,
|
45 | });
|
46 | console.log(`Loading project: ${project}`);
|
47 | const projectConfig = config === null || config === void 0 ? void 0 : config.getProject(project);
|
48 | console.log(`Loading GraphQL Schema of ${project}`);
|
49 | let schema = await (projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.getSchema());
|
50 | if (!schema) {
|
51 | console.warn(`Could not find schema for project ${project} fallback to default schema`);
|
52 | }
|
53 | if (mock$1) {
|
54 | if (!schema) {
|
55 | console.warn('No schema found for mocking. Skipping mocking.');
|
56 | }
|
57 | else {
|
58 | console.log(`Adding mocks to the schema`);
|
59 | schema = mock.addMocksToSchema({ schema });
|
60 | }
|
61 | }
|
62 | console.log(`Building GraphQL Server`);
|
63 | const graphQLServer = node.createServer({
|
64 | schema,
|
65 | });
|
66 | console.log(`Starting GraphQL Server`);
|
67 | await graphQLServer.start();
|
68 | registerTerminateHandler(() => {
|
69 | graphQLServer.stop();
|
70 | });
|
71 | }).argv;
|
72 | }
|
73 |
|
74 | try {
|
75 | graphqlYoga();
|
76 | }
|
77 | catch (e) {
|
78 | console.error(e);
|
79 | process.exit(1);
|
80 | }
|