-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
90 lines (84 loc) · 2.32 KB
/
gatsby-node.js
File metadata and controls
90 lines (84 loc) · 2.32 KB
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
module.exports = {
onCreateNode: async ({
actions: { createNode, createParentChildLink },
createContentDigest,
createNodeId,
loadNodeContent,
node
}) => {
if (node.internal.mediaType !== `application/json`) {
return
}
const content = await loadNodeContent(node)
let ancestor = JSON.parse(content)
const ancestorNodeId = createNodeId(`${node.id}-ancestor`)
const ancestorNode = {
...ancestor,
id: ancestorNodeId,
parent: node.id,
internal: {
type: 'Ancestor',
contentDigest: createContentDigest(ancestor)
}
}
descendantNodeIds = ancestor.descendants.map((descendant, i) => {
const descendantNodeId = createNodeId(`${ancestorNodeId}-descendant-${i}`)
const descendantNode = {
...descendant,
id: descendantNodeId,
parent: ancestorNodeId,
internal: {
type: 'Descendant',
contentDigest: createContentDigest(descendant)
},
ancestor___NODE: ancestorNodeId
}
createNode(descendantNode)
return descendantNodeId
})
ancestorNode.descendants___NODE = descendantNodeIds
createNode(ancestorNode)
createParentChildLink({
parent: node,
child: ancestorNode
})
},
createSchemaCustomization: ({ actions, schema }) => {
actions.createTypes([
schema.buildObjectType({
name: 'Descendant',
fields: {
firstName: 'String!',
lastName: {
type: 'String!',
resolve: (source, args, context, info) => {
const ancestor = context.nodeModel.getNodeById({
id: source.parent
})
return source.lastName || ancestor.lastName
}
},
ancestor: {
type: 'Ancestor!',
extensions: { link: { from: 'ancestor___NODE' } }
}
},
interfaces: ['Node'],
extensions: { infer: false }
}),
schema.buildObjectType({
name: 'Ancestor',
fields: {
firstName: 'String!',
lastName: 'String!',
descendants: {
type: '[Descendant!]!',
extensions: { link: { from: 'descendants___NODE' } }
}
},
interfaces: ['Node'],
extensions: { infer: false }
})
])
}
}