-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
71 lines (62 loc) · 1.72 KB
/
gatsby-node.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
const fs = require("fs")
const minilog = require("minilog")
const path = require("path")
const write = require("write")
const { exec } = require("child_process")
const { printSchema } = require("gatsby/graphql")
minilog.enable()
const package = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "package.json"), "utf8")
)
const namespace = `\n[${package.name}]`
const logger = minilog(namespace)
const defaultArtifactDirectory = "./src/__generated__"
/**
* Add babel-plugin-relay to Babel pipeline
*/
exports.onCreateBabelConfig = ({ actions }, options) => {
const { artifactDirectory = defaultArtifactDirectory } = options
actions.setBabelPlugin({
name: "babel-plugin-relay",
options: {
artifactDirectory,
},
})
}
/**
* Generate GraphQL schema.json and execute the relay compiler.
*/
exports.onPostBootstrap = async ({ store }, options) => {
const {
src = ".",
schema: schemaDest = "./schema.graphql",
language = "typescript",
artifactDirectory = defaultArtifactDirectory,
watch = false,
} = options
try {
const gatsbySchema = store.getState().schema
const sdlSchema = printSchema(gatsbySchema)
// Emit schema
write.sync(schemaDest, sdlSchema, {})
logger.log(`wrote schema to ${schemaDest} \n`)
// Execute relay compiler
exec(
`relay-compiler \
--src ${src} \
--schema ${schemaDest} \
--language ${language} \
--artifactDirectory ${artifactDirectory} \
--watch ${watch}`,
(error, stdout) => {
if (error) {
logger.error("error emitting types \n\n", stdout)
} else {
logger.log(stdout)
}
}
)
} catch (error) {
logger.error("error emitting types: ", error)
}
}