forked from Akryum/vue-cli-plugin-apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
308 lines (268 loc) Β· 8.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const chalk = require('chalk')
const COMMAND_OPTIONS = {
'-h, --host': 'specify server host',
'-p, --port': 'specify server port',
'--run [command]': 'run another command in parallel',
'--mock': 'enables mocks',
'--engine': 'enables Apollo Engine',
'--delay': 'delays run by a small duration',
'--generate-schema': 'auto-generate JSON and GraphQL schema files',
}
const SCHEMA_OPTIONS = {
'--endpoint [endpoint]': 'URL of running server or path to JSON schema file',
'--key [key]': 'Engine service key',
'--tag [tag]': 'Schema Tag',
}
const DEFAULT_GENERATE_OUTPUT = './node_modules/.temp/graphql/schema'
function nullable (value) {
return value == null ? {} : value
}
module.exports = (api, options) => {
const apolloOptions = nullable(nullable(options.pluginOptions).apollo)
const useThreads = process.env.NODE_ENV === 'production' && options.parallel
const cacheDirectory = api.resolve('node_modules/.cache/cache-loader')
const { generateCacheIdentifier } = require('./utils')
api.chainWebpack(config => {
const rule = config.module
.rule('gql')
.test(/\.(gql|graphql)$/)
.use('cache-loader')
.loader('cache-loader')
.options({ cacheDirectory })
.end()
if (useThreads) {
rule
.use('thread-loader')
.loader('thread-loader')
}
rule
.use('gql-loader')
.loader('graphql-tag/loader')
.end()
if (api.hasPlugin('eslint') && config.module.rules.has('eslint')) {
if (apolloOptions.lintGQL) {
const id = generateCacheIdentifier(api.resolve('.'))
config.module
.rule('eslint')
.test(/\.(vue|(j|t)sx?|gql|graphql)$/)
.use('eslint-loader')
.tap(options => {
options.extensions.push('.gql', '.graphql')
return {
...options,
cacheIdentifier: options.cacheIdentifier + id,
}
})
} else if (apolloOptions.lintGQL !== false) {
console.log('To enable GQL files in ESLint, set the `pluginOptions.apollo.lintGQL` project option to `true` in `vue.config.js`. Put `false` to hide this message.')
console.log('You also need to install `eslint-plugin-graphql` and enable it in your ESLint configuration.')
}
}
config.resolve
.extensions
.prepend('.mjs')
config.module
.rule('mjs')
.test(/\.mjs$/)
.include
.add(/node_modules/)
.end()
.type('javascript/auto')
// Add string template tag transform to BublΓ©
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.transpileOptions = options.transpileOptions || {}
options.transpileOptions.transforms = options.transpileOptions.transforms || {}
options.transpileOptions.transforms.dangerousTaggedTemplateString = true
return options
})
})
api.registerCommand('apollo:dev', {
description: 'Run the Apollo server and watch the sources to restart automatically',
usage: 'vue-cli-service apollo:dev [options]',
options: COMMAND_OPTIONS,
details: 'For more info, see https://github.com/Akryum/vue-cli-plugin-apollo',
}, args => {
const {
runParallelCommand,
getFlatArgs,
runWatch,
sendIpcMessage,
} = require('./utils')
runParallelCommand(args)
if (args['generate-schema']) {
const execa = require('execa')
execa('vue-cli-service apollo:schema:generate', ['--watch'], {
stdio: ['inherit', 'inherit', 'inherit'],
cleanup: true,
shell: true,
})
}
// Pass the args along
const flatArgs = getFlatArgs(args, ['_', 'run', 'delay', 'generate-schema'])
return runWatch(api, options, {
script: 'apollo:start',
args: ['--delay', ...flatArgs],
onStart: () => {
sendIpcMessage({
error: false,
})
},
onCrash: () => {
console.log(chalk.bold(chalk.red(`π₯ GraphQL API crashed!`)))
sendIpcMessage({
urls: null,
error: true,
})
},
onRestart: () => {
console.log(chalk.bold(chalk.green(`β³ GraphQL API is restarting...`)))
sendIpcMessage({
error: false,
})
},
})
})
api.registerCommand('apollo:start', {
description: 'Run the Apollo server',
usage: 'vue-cli-service apollo:start [options]',
options: COMMAND_OPTIONS,
details: 'For more info, see https://github.com/Akryum/vue-cli-plugin-apollo',
}, args => {
const {
runParallelCommand,
sendIpcMessage,
getServerOptions,
} = require('./utils')
runParallelCommand(args)
if (args['generate-schema']) {
const execa = require('execa')
execa('vue-cli-service apollo:schema:generate', {
stdio: ['inherit', 'inherit', 'inherit'],
cleanup: true,
shell: true,
})
}
const run = () => {
let server = require('./graphql-server')
server = server.default || server
const opts = getServerOptions(api, options, args)
server(opts, () => {
sendIpcMessage({
urls: {
playground: `http://localhost:${opts.port}${opts.graphqlPath}`,
},
})
})
}
if (args.delay) {
setTimeout(run, 300)
} else {
run()
}
})
api.registerCommand('apollo:schema:generate', {
description: 'Generates full schema JSON and GraphQL files',
usage: 'vue-cli-service apollo:schema:generate [options]',
options: {
'--watch': 'Watch server files and re-generate schema JSON',
'--output [path]': 'Path to the output files',
},
details: 'For more info, see https://github.com/Akryum/vue-cli-plugin-apollo',
}, async args => {
if (args.watch) {
const {
getFlatArgs,
runWatch,
} = require('./utils')
const flatArgs = getFlatArgs(args, ['watch'])
return runWatch(api, options, {
script: 'apollo:schema:generate',
args: flatArgs,
})
} else {
const {
getServerOptions,
} = require('./utils')
const opts = getServerOptions(api, options, args)
const output = args.output || DEFAULT_GENERATE_OUTPUT
const jsonOutput = `${output}.json`
const graphqlOutput = `${output}.graphql`
const generateSchema = require('./utils/generate-schema')
await generateSchema({
paths: opts.paths,
jsonOutput,
graphqlOutput,
typescript: opts.typescript,
})
}
})
api.registerCommand('apollo:client:check', {
description: 'Compare schema from Apollo Engine',
usage: 'vue-cli-service apollo:client:check [options]',
options: SCHEMA_OPTIONS,
details: 'For more info, see https://github.com/Akryum/vue-cli-plugin-apollo',
}, async args => {
throw new Error(`Not implemented yet`)
/* eslint-disable no-unreachable */
const endpoint = args.endpoint || `${DEFAULT_GENERATE_OUTPUT}.json`
const key = args.key || process.env.VUE_APP_APOLLO_ENGINE_KEY
const tag = args.tag || process.env.VUE_APP_APOLLO_ENGINE_TAG
const engineEndpoint = process.env.APOLLO_ENGINE_API_ENDPOINT
await autoGenerateSchema(endpoint)
const checkSchema = require('./utils/check-schema')
await checkSchema({
endpoint,
key,
tag,
engineEndpoint,
})
})
api.registerCommand('apollo:schema:publish', {
description: 'Publish schema to Apollo Engine',
usage: 'vue-cli-service apollo:schema:publish [options]',
options: SCHEMA_OPTIONS,
details: 'For more info, see https://github.com/Akryum/vue-cli-plugin-apollo',
}, async args => {
const endpoint = args.endpoint || `${DEFAULT_GENERATE_OUTPUT}.json`
const key = args.key || process.env.VUE_APP_APOLLO_ENGINE_KEY
const tag = args.tag || process.env.VUE_APP_APOLLO_ENGINE_TAG
const engineEndpoint = process.env.APOLLO_ENGINE_API_ENDPOINT
await autoGenerateSchema(endpoint)
const publishSchema = require('./utils/publish-schema')
await publishSchema({
endpoint,
key,
tag,
engineEndpoint,
})
})
async function autoGenerateSchema (endpoint) {
// Auto-generate if json file doesn't exist
if (endpoint.match(/\.json$/i)) {
const fs = require('fs')
const file = api.resolve(endpoint)
if (!fs.existsSync(file)) {
const path = require('path')
const output = path.join(path.dirname(file), path.basename(file, path.extname(file)))
const execa = require('execa')
await execa('vue-cli-service apollo:schema:generate', [
'--output',
output,
], {
stdio: ['inherit', 'inherit', 'inherit'],
cleanup: true,
shell: true,
})
const { info } = require('@vue/cli-shared-utils')
info(`The JSON schema was automatically generated in '${file}'.`, 'apollo')
}
}
}
}
module.exports.defaultModes = {
'apollo:dev': 'development',
}