-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
transform-object.js
114 lines (92 loc) · 2.9 KB
/
transform-object.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
import { buildTypeName } from "~/steps/create-schema-customization/helpers"
import { fetchAndCreateSingleNode } from "~/steps/source-nodes/update-nodes/wp-actions/update"
import { getQueryInfoByTypeName } from "~/steps/source-nodes/helpers"
import { getGatsbyApi } from "~/utils/get-gatsby-api"
import { inPreviewMode } from "~/steps/preview/index"
import { getPluginOptions } from "../../../utils/get-gatsby-api"
export const transformListOfGatsbyNodes = ({ field, fieldName }) => {
const typeName = buildTypeName(field.type.ofType.name)
return {
type: `[${typeName}]`,
resolve: (source, args, context) => {
let nodes = null
const field = source[fieldName]
if (field && Array.isArray(field)) {
nodes = field
} else if (Array.isArray(source?.nodes)) {
nodes = source.nodes
}
if (!nodes) {
return null
}
return context.nodeModel.getNodesByIds({
ids: nodes.map(node => node?.id),
type: typeName,
})
},
}
}
export const buildGatsbyNodeObjectResolver = ({ field, fieldName }) => async (
source,
_,
context
) => {
const typeName = buildTypeName(field.type.name)
const nodeField = source[fieldName]
if (!nodeField || (nodeField && !nodeField.id)) {
return null
}
const existingNode = context.nodeModel.getNodeById({
id: nodeField.id,
type: typeName,
})
const {
schema: { typePrefix: prefix },
} = getPluginOptions()
if (existingNode?.__typename && !existingNode.__typename.startsWith(prefix)) {
existingNode.__typename = buildTypeName(existingNode.__typename)
}
if (existingNode) {
return existingNode
}
const queryInfo = getQueryInfoByTypeName(field.type.name)
if (!queryInfo) {
// if we don't have query info for a type
// it probably means this type is excluded in plugin options
return null
}
const isLazyMediaItem =
queryInfo.typeInfo.nodesTypeName === `MediaItem` &&
queryInfo.settings.lazyNodes
if (
// only fetch/create nodes in resolvers for media items when they have lazyNodes enabled
!isLazyMediaItem &&
// but if we're in preview mode we want to lazy fetch nodes
// because if nodes are limited we still want to lazy fetch connections
!inPreviewMode()
) {
return null
}
// if this node doesn't exist, fetch it and create a node
const { node } = await fetchAndCreateSingleNode({
id: nodeField.id,
actionType: `CREATE`,
singleName: queryInfo.typeInfo.singularName,
})
if (source.id && node) {
const { helpers } = getGatsbyApi()
await helpers.actions.createParentChildLink({
parent: source,
child: node,
})
}
return node || null
}
export const transformGatsbyNodeObject = transformerApi => {
const { field } = transformerApi
const typeName = buildTypeName(field.type.name)
return {
type: typeName,
resolve: buildGatsbyNodeObjectResolver(transformerApi),
}
}