1 | const { Mixin } = require('hops-mixin');
|
2 | const strip = require('strip-indent');
|
3 | const {
|
4 | internal: { createWebpackMiddleware, StatsFilePlugin },
|
5 | } = require('@untool/webpack');
|
6 |
|
7 | function exists(path) {
|
8 | try {
|
9 | require.resolve(path);
|
10 | return true;
|
11 | } catch (e) {
|
12 | return false;
|
13 | }
|
14 | }
|
15 |
|
16 | class GraphQLMixin extends Mixin {
|
17 | registerCommands(yargs) {
|
18 | yargs.command('graphql', 'Execute GraphQL specific tasks', yargs =>
|
19 | yargs
|
20 | .usage('Usage: hops graphql <command>')
|
21 | .command({
|
22 | command: 'introspect',
|
23 | describe: 'Fetches GraphQL schema information for introspection',
|
24 | builder: {
|
25 | header: {
|
26 | alias: 'H',
|
27 | type: 'array',
|
28 | default: [],
|
29 | describe: strip(`
|
30 | Additional HTTP headers to be used when executing the schema
|
31 | introspection on the remote server. Specify this multiple
|
32 | times to add more headers.\nFormat: "Header: Value"
|
33 | `),
|
34 | },
|
35 | },
|
36 | handler: argv => {
|
37 | require('./lib/fragments')({
|
38 | graphqlUri: this.config.graphqlUri,
|
39 | schemaFile: this.config.graphqlSchemaFile,
|
40 | fragmentsFile: this.config.fragmentsFile,
|
41 | headers: argv.header,
|
42 | })
|
43 | .then(() => {
|
44 | console.log('Fetched and saved GraphQL fragments');
|
45 | })
|
46 | .catch(err => {
|
47 | console.error('Could not fetch GraphQL fragments:');
|
48 | console.trace(err);
|
49 | });
|
50 | },
|
51 | })
|
52 | .help('help')
|
53 | .alias('h', 'help')
|
54 | .demandCommand()
|
55 | );
|
56 | }
|
57 |
|
58 | configureServer(rootApp, middleware) {
|
59 | if (
|
60 | !exists(this.config.graphqlMockSchemaFile) ||
|
61 | process.env.NODE_ENV === 'production'
|
62 | ) {
|
63 | return;
|
64 | }
|
65 |
|
66 | middleware.initial.push({
|
67 | path: this.config.graphqlMockServerPath,
|
68 | handler: createWebpackMiddleware(
|
69 | this.getBuildConfig('graphql-mock-server', 'node'),
|
70 | true
|
71 | ),
|
72 | });
|
73 | }
|
74 |
|
75 | configureBuild(webpackConfig, loaderConfigs, target) {
|
76 | const { allLoaderConfigs } = loaderConfigs;
|
77 |
|
78 | allLoaderConfigs.splice(allLoaderConfigs.length - 1, 0, {
|
79 | test: /\.(graphql|gql)$/,
|
80 | loader: 'graphql-tag/loader',
|
81 | });
|
82 |
|
83 | webpackConfig.externals.push('encoding');
|
84 |
|
85 | if (process.env.NODE_ENV === 'production') {
|
86 | return;
|
87 | }
|
88 |
|
89 | if (exists(this.config.graphqlMockSchemaFile)) {
|
90 | webpackConfig.resolve.alias['hops-graphql/schema'] = require.resolve(
|
91 | this.config.graphqlMockSchemaFile
|
92 | );
|
93 | }
|
94 |
|
95 | if (target === 'graphql-mock-server') {
|
96 | webpackConfig.externals.push(
|
97 | 'apollo-server-express',
|
98 | 'express',
|
99 | 'graphql'
|
100 | );
|
101 |
|
102 | webpackConfig.output.filename = 'hops-graphql-mock-server.js';
|
103 | webpackConfig.resolve.alias['@untool/entrypoint'] = require.resolve(
|
104 | './lib/mock-server-middleware'
|
105 | );
|
106 |
|
107 | webpackConfig.plugins = webpackConfig.plugins.filter(
|
108 | p => !(p instanceof StatsFilePlugin)
|
109 | );
|
110 | }
|
111 | }
|
112 | }
|
113 |
|
114 | module.exports = GraphQLMixin;
|