forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema-helpers.js
196 lines (159 loc) · 5.9 KB
/
schema-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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
import renderContent from '../../../lib/render-content/index.js'
import fs from 'fs'
import xGraphql from 'graphql'
import path from 'path'
const graphqlTypes = JSON.parse(
fs.readFileSync(path.join(process.cwd(), './lib/graphql/types.json'))
)
const { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } =
xGraphql
const singleQuotesInsteadOfBackticks = / '(\S+?)' /
function addPeriod(string) {
return string.endsWith('.') ? string : string + '.'
}
async function getArguments(args, schema) {
if (!args.length) return
const newArgs = []
for (const arg of args) {
const newArg = {}
const type = {}
newArg.name = arg.name.value
newArg.defaultValue = arg.defaultValue ? arg.defaultValue.value : undefined
newArg.description = await getDescription(arg.description.value)
type.name = getType(arg)
type.id = getId(type.name)
type.kind = getTypeKind(type.name, schema)
type.href = getFullLink(type.kind, type.id)
newArg.type = type
newArgs.push(newArg)
}
return newArgs
}
async function getDeprecationReason(directives, schemaMember) {
if (!schemaMember.isDeprecated) return
// it's possible for a schema member to be deprecated and under preview
const deprecationDirective = directives.filter((dir) => dir.name.value === 'deprecated')
// catch any schema members that have more than one deprecation (none currently)
if (deprecationDirective.length > 1)
console.log(`more than one deprecation found for ${schemaMember.name}`)
return renderContent(deprecationDirective[0].arguments[0].value.value)
}
function getDeprecationStatus(directives) {
if (!directives.length) return
return directives[0].name.value === 'deprecated'
}
async function getDescription(rawDescription) {
rawDescription = rawDescription.replace(singleQuotesInsteadOfBackticks, '`$1`')
return renderContent(addPeriod(rawDescription))
}
function getFullLink(baseType, id) {
return `/graphql/reference/${baseType}#${id}`
}
function getId(path) {
return removeMarkers(path).toLowerCase()
}
// e.g., given `ObjectTypeDefinition`, get `objects`
function getKind(type) {
return graphqlTypes.find((graphqlType) => graphqlType.type === type).kind
}
async function getPreview(directives, schemaMember, previewsPerVersion) {
if (!directives.length) return
// it's possible for a schema member to be deprecated and under preview
const previewDirective = directives.filter((dir) => dir.name.value === 'preview')
if (!previewDirective.length) return
// catch any schema members that are under more than one preview (none currently)
if (previewDirective.length > 1)
console.log(`more than one preview found for ${schemaMember.name}`)
// an input object's input field may have a ListValue directive that is not relevant to previews
if (previewDirective[0].arguments[0].value.kind !== 'StringValue') return
const previewName = previewDirective[0].arguments[0].value.value
const preview = previewsPerVersion.find((p) => p.toggled_by.includes(previewName))
if (!preview) console.error(`cannot find "${previewName}" in graphql_previews.yml`)
return preview
}
// the docs use brackets to denote list types: `[foo]`
// and an exclamation mark to denote non-nullable types: `foo!`
// both single items and lists can be non-nullable
// so the permutations are:
// 1. single items: `foo`, `foo!`
// 2. nullable lists: `[foo]`, `[foo!]`
// 3. non-null lists: `[foo]!`, `[foo!]!`
// see https://github.com/rmosolgo/graphql-ruby/blob/master/guides/type_definitions/lists.md#lists-nullable-lists-and-lists-of-nulls
function getType(field) {
// 1. single items
if (field.type.kind !== 'ListType') {
// nullable item, e.g. `license` query has `License` type
if (field.type.kind === 'NamedType') {
return field.type.name.value
}
// non-null item, e.g. `meta` query has `GitHubMetadata!` type
if (field.type.kind === 'NonNullType' && field.type.type.kind === 'NamedType') {
return `${field.type.type.name.value}!`
}
}
// 2. nullable lists
if (field.type.kind === 'ListType') {
// nullable items, e.g. `codesOfConduct` query has `[CodeOfConduct]` type
if (field.type.type.kind === 'NamedType') {
return `[${field.type.type.name.value}]`
}
// non-null items, e.g. `severities` arg has `[SecurityAdvisorySeverity!]` type
if (field.type.type.kind === 'NonNullType' && field.type.type.type.kind === 'NamedType') {
return `[${field.type.type.type.name.value}!]`
}
}
// 3. non-null lists
if (field.type.kind === 'NonNullType' && field.type.type.kind === 'ListType') {
// nullable items, e.g. `licenses` query has `[License]!` type
if (field.type.type.type.kind === 'NamedType') {
return `[${field.type.type.type.name.value}]!`
}
// non-null items, e.g. `marketplaceCategories` query has `[MarketplaceCategory!]!` type
if (
field.type.type.type.kind === 'NonNullType' &&
field.type.type.type.type.kind === 'NamedType'
) {
return `[${field.type.type.type.type.name.value}!]!`
}
}
console.error(`cannot get type of ${field.name.value}`)
}
function getTypeKind(type, schema) {
type = removeMarkers(type)
const typeFromSchema = schema.getType(type)
if (isScalarType(typeFromSchema)) {
return 'scalars'
}
if (isObjectType(typeFromSchema)) {
return 'objects'
}
if (isInterfaceType(typeFromSchema)) {
return 'interfaces'
}
if (isUnionType(typeFromSchema)) {
return 'unions'
}
if (isEnumType(typeFromSchema)) {
return 'enums'
}
if (isInputObjectType(typeFromSchema)) {
return 'input-objects'
}
console.error(`cannot find type kind of ${type}`)
}
function removeMarkers(str) {
return str.replace('[', '').replace(']', '').replace(/!/g, '')
}
export default {
getArguments,
getDeprecationReason,
getDeprecationStatus,
getDescription,
getFullLink,
getId,
getKind,
getPreview,
getType,
getTypeKind,
}