-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
159 lines (148 loc) · 3.76 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
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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/node-apis/
*/
// You can delete this file if you're not using it
/**
* You can uncomment the following line to verify that
* your plugin is being loaded in your site.
*
* See: https://www.gatsbyjs.com/docs/creating-a-local-plugin/#developing-a-local-plugin-that-is-outside-your-project
*/
const { ApolloClient } = require("apollo-client")
const { InMemoryCache } = require("apollo-cache-inmemory")
const { split } = require("apollo-link")
const { HttpLink } = require("apollo-link-http")
const { WebSocketLink } = require("apollo-link-ws")
const { getMainDefinition } = require("apollo-utilities")
const fetch = require("node-fetch")
const gql = require("graphql-tag")
const WebSocket = require("ws")
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const POST_NODE_TYPE = `Post`
const AUTHOR_NODE_TYPE = `Author`
const client = new ApolloClient({
link: split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
)
},
new WebSocketLink({
uri: `ws://gatsby-source-plugin-api.glitch.me/`,
// or `ws://localhost:4000`
options: {
reconnect: true,
},
webSocketImpl: WebSocket,
}),
new HttpLink({
uri: "https://gatsby-source-plugin-api.glitch.me/",
// or `http://localhost:4000`
fetch,
})
),
cache: new InMemoryCache(),
})
exports.onPreInit = () => console.log("Loaded gatsby-starter-plugin")
exports.sourceNodes = async ({
actions,
createContentDigest,
createNodeId,
getNodesByType,
}) => {
const { createNode } = actions
const { data } = await client.query({
query: gql`
query {
posts {
id
description
slug
imgUrl
imgAlt
author {
id
name
}
}
authors {
id
name
}
}
`,
})
// Recurse through data and create Gatsby nodes.
data.posts.forEach(post =>
createNode({
...post,
id: createNodeId(`${POST_NODE_TYPE}-${post.id}`),
parent: null,
children: [],
internal: {
type: POST_NODE_TYPE,
content: JSON.stringify(post),
contentDigest: createContentDigest(post),
},
})
)
data.authors.forEach(author =>
createNode({
...author,
id: createNodeId(`${AUTHOR_NODE_TYPE}-${author.id}`),
parent: null,
children: [],
internal: {
type: AUTHOR_NODE_TYPE,
content: JSON.stringify(author),
contentDigest: createContentDigest(author),
},
})
)
return
}
exports.onCreateNode = async ({
node, // i.e. the just-created node
actions: { createNode },
createNodeId,
getCache,
}) => {
if (node.internal.type === POST_NODE_TYPE) {
const fileNode = await createRemoteFileNode({
// The remote image URL for which to generate a node.
url: node.imgUrl,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
})
if (fileNode) {
node.remoteImage___NODE = fileNode.id
}
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type Post implements Node {
id: ID!
slug: String!
description: String!
imgUrl: String!
imgAlt: String!
# Create relationships between Post and File nodes
# for optimized images.
remoteImage: File @link
# Create relationships between Post and Author nodes.
author: Author @link(from: "author.name" by: "name")
}
type Author implements Node {
id: ID!
name: String!
}`
)
}