-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
helpers.js
154 lines (123 loc) · 3.71 KB
/
helpers.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
import store from "~/store"
import { typeDefinitionFilters } from "./type-filters"
import { getPluginOptions } from "~/utils/get-gatsby-api"
import { cloneDeep, merge } from "lodash"
/**
* This function namespaces typenames with a prefix
*/
export const buildTypeName = name => {
if (!name || typeof name !== `string`) {
return null
}
const {
schema: { typePrefix: prefix },
} = getPluginOptions()
// this is for our namespace type on the root { wp { ...fields } }
if (name === prefix) {
return name
}
if (name === `Filter`) {
name = `FilterType`
}
if (name.startsWith(prefix)) {
return name
}
return prefix + name
}
/**
* Find the first type name of a Type definition pulled via introspection
* @param {object} type
*/
export const findTypeName = type =>
type?.name ||
type?.ofType?.name ||
type?.ofType?.ofType?.name ||
type?.ofType?.ofType?.ofType?.name
/**
* Find the first type kind of a Type definition pulled via introspection
* @param {object} type
*/
export const findTypeKind = type =>
type?.kind ||
type?.ofType?.kind ||
type?.ofType?.ofType?.kind ||
type?.ofType?.ofType?.ofType?.kind
export const fieldOfTypeWasFetched = type => {
const { fetchedTypes } = store.getState().remoteSchema
const typeName = findTypeName(type)
const typeWasFetched = !!fetchedTypes.get(typeName)
return typeWasFetched
}
const supportedScalars = [
`Int`,
`Float`,
`String`,
`Boolean`,
`ID`,
`Date`,
`JSON`,
]
export const typeIsABuiltInScalar = type =>
// @todo the next function and this one are redundant.
// see the next todo on how to fix the issue. If that todo is resolved, these functions will be identical. :(
supportedScalars.includes(findTypeName(type))
export const typeIsASupportedScalar = type => {
if (findTypeKind(type) !== `SCALAR`) {
// @todo returning true here seems wrong since a type that is not a scalar can't be a supported scalar... so there is some other logic elsewhere that is wrong
// making this return false causes errors in the schema
return true
}
return supportedScalars.includes(findTypeName(type))
}
const typeSettingCache = {}
// retrieves plugin settings for the provided type
export const getTypeSettingsByType = type => {
if (!type) {
return {}
}
const typeName = findTypeName(type)
const cachedTypeSettings = typeSettingCache[typeName]
if (cachedTypeSettings) {
return cachedTypeSettings
}
// the plugin options object containing every type setting
const allTypeSettings = store.getState().gatsbyApi.pluginOptions.type
const typeSettings = cloneDeep(allTypeSettings[typeName] || {})
// the type.__all plugin option which is applied to every type setting
const __allTypeSetting = cloneDeep(allTypeSettings.__all || {})
if (typeName === `MediaItem`) {
delete __allTypeSetting.limit
delete typeSettings.limit
}
if (typeSettings) {
const mergedSettings = merge(__allTypeSetting, typeSettings)
typeSettingCache[typeName] = mergedSettings
return mergedSettings
}
typeSettingCache[typeName] = __allTypeSetting
return __allTypeSetting
}
/**
* This is used to filter the automatically generated type definitions before they're added to the schema customization api.
*/
export const filterTypeDefinition = (
typeDefinition,
typeBuilderApi,
typeKind
) => {
const filters = typeDefinitionFilters.filter(filter =>
[typeBuilderApi.type.name, `__all`].includes(filter.typeName)
)
if (filters?.length) {
filters.forEach(filter => {
if (filter && typeof filter.typeDef === `function`) {
typeDefinition = filter.typeDef(
typeDefinition,
typeBuilderApi,
typeKind
)
}
})
}
return typeDefinition
}