UNPKG

2.81 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
5
6const yargs = _interopDefault(require('yargs'));
7const helpers = require('yargs/helpers');
8const graphqlConfig = require('graphql-config');
9const codeFileLoader = require('@graphql-tools/code-file-loader');
10const node = require('@graphql-yoga/node');
11const mock = require('@graphql-tools/mock');
12
13const terminateEvents = ['SIGINT', 'SIGTERM'];
14function registerTerminateHandler(callback) {
15 for (const eventName of terminateEvents) {
16 process.on(eventName, () => callback(eventName));
17 }
18}
19const 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};
29function 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
74try {
75 graphqlYoga();
76}
77catch (e) {
78 console.error(e);
79 process.exit(1);
80}