Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

schema-helpers.js 5.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. const renderContent = require('../../../lib/render-content')
  2. const graphqlTypes = require('../../../lib/graphql/types')
  3. const {
  4. isScalarType,
  5. isObjectType,
  6. isInterfaceType,
  7. isUnionType,
  8. isEnumType,
  9. isInputObjectType
  10. } = require('graphql')
  11. const singleQuotesInsteadOfBackticks = / '(\S+?)' /
  12. function addPeriod (string) {
  13. return string.endsWith('.') ? string : string + '.'
  14. }
  15. async function getArguments (args, schema) {
  16. if (!args.length) return
  17. const newArgs = []
  18. for (const arg of args) {
  19. const newArg = {}
  20. const type = {}
  21. newArg.name = arg.name.value
  22. newArg.defaultValue = arg.defaultValue ? arg.defaultValue.value : undefined
  23. newArg.description = await getDescription(arg.description.value)
  24. type.name = getType(arg)
  25. type.id = getId(type.name)
  26. type.kind = getTypeKind(type.name, schema)
  27. type.href = getFullLink(type.kind, type.id)
  28. newArg.type = type
  29. newArgs.push(newArg)
  30. }
  31. return newArgs
  32. }
  33. async function getDeprecationReason (directives, schemaMember) {
  34. if (!schemaMember.isDeprecated) return
  35. // it's possible for a schema member to be deprecated and under preview
  36. const deprecationDirective = directives.filter(dir => dir.name.value === 'deprecated')
  37. // catch any schema members that have more than one deprecation (none currently)
  38. if (deprecationDirective.length > 1) console.log(`more than one deprecation found for ${schemaMember.name}`)
  39. return renderContent(deprecationDirective[0].arguments[0].value.value)
  40. }
  41. function getDeprecationStatus (directives) {
  42. if (!directives.length) return
  43. return directives[0].name.value === 'deprecated'
  44. }
  45. async function getDescription (rawDescription) {
  46. rawDescription = rawDescription.replace(singleQuotesInsteadOfBackticks, '`$1`')
  47. return renderContent(addPeriod(rawDescription))
  48. }
  49. function getFullLink (baseType, id) {
  50. return `/graphql/reference/${baseType}#${id}`
  51. }
  52. function getId (path) {
  53. return removeMarkers(path).toLowerCase()
  54. }
  55. // e.g., given `ObjectTypeDefinition`, get `objects`
  56. function getKind (type) {
  57. return graphqlTypes.find(graphqlType => graphqlType.type === type).kind
  58. }
  59. async function getPreview (directives, schemaMember, previewsPerVersion) {
  60. if (!directives.length) return
  61. // it's possible for a schema member to be deprecated and under preview
  62. const previewDirective = directives.filter(dir => dir.name.value === 'preview')
  63. if (!previewDirective.length) return
  64. // catch any schema members that are under more than one preview (none currently)
  65. if (previewDirective.length > 1) console.log(`more than one preview found for ${schemaMember.name}`)
  66. // an input object's input field may have a ListValue directive that is not relevant to previews
  67. if (previewDirective[0].arguments[0].value.kind !== 'StringValue') return
  68. const previewName = previewDirective[0].arguments[0].value.value
  69. const preview = previewsPerVersion.find(p => p.toggled_by.includes(previewName))
  70. if (!preview) console.error(`cannot find "${previewName}" in graphql_previews.yml`)
  71. return preview
  72. }
  73. // the docs use brackets to denote list types: `[foo]`
  74. // and an exclamation mark to denote non-nullable types: `foo!`
  75. // both single items and lists can be non-nullable
  76. // so the permutations are:
  77. // 1. single items: `foo`, `foo!`
  78. // 2. nullable lists: `[foo]`, `[foo!]`
  79. // 3. non-null lists: `[foo]!`, `[foo!]!`
  80. // see https://github.com/rmosolgo/graphql-ruby/blob/master/guides/type_definitions/lists.md#lists-nullable-lists-and-lists-of-nulls
  81. function getType (field) {
  82. // 1. single items
  83. if (field.type.kind !== 'ListType') {
  84. // nullable item, e.g. `license` query has `License` type
  85. if (field.type.kind === 'NamedType') {
  86. return field.type.name.value
  87. }
  88. // non-null item, e.g. `meta` query has `GitHubMetadata!` type
  89. if (field.type.kind === 'NonNullType' && field.type.type.kind === 'NamedType') {
  90. return `${field.type.type.name.value}!`
  91. }
  92. }
  93. // 2. nullable lists
  94. if (field.type.kind === 'ListType') {
  95. // nullable items, e.g. `codesOfConduct` query has `[CodeOfConduct]` type
  96. if (field.type.type.kind === 'NamedType') {
  97. return `[${field.type.type.name.value}]`
  98. }
  99. // non-null items, e.g. `severities` arg has `[SecurityAdvisorySeverity!]` type
  100. if (field.type.type.kind === 'NonNullType' && field.type.type.type.kind === 'NamedType') {
  101. return `[${field.type.type.type.name.value}!]`
  102. }
  103. }
  104. // 3. non-null lists
  105. if (field.type.kind === 'NonNullType' && field.type.type.kind === 'ListType') {
  106. // nullable items, e.g. `licenses` query has `[License]!` type
  107. if (field.type.type.type.kind === 'NamedType') {
  108. return `[${field.type.type.type.name.value}]!`
  109. }
  110. // non-null items, e.g. `marketplaceCategories` query has `[MarketplaceCategory!]!` type
  111. if (field.type.type.type.kind === 'NonNullType' && field.type.type.type.type.kind === 'NamedType') {
  112. return `[${field.type.type.type.type.name.value}!]!`
  113. }
  114. }
  115. console.error(`cannot get type of ${field.name.value}`)
  116. }
  117. function getTypeKind (type, schema) {
  118. type = removeMarkers(type)
  119. const typeFromSchema = schema.getType(type)
  120. if (isScalarType(typeFromSchema)) {
  121. return 'scalars'
  122. }
  123. if (isObjectType(typeFromSchema)) {
  124. return 'objects'
  125. }
  126. if (isInterfaceType(typeFromSchema)) {
  127. return 'interfaces'
  128. }
  129. if (isUnionType(typeFromSchema)) {
  130. return 'unions'
  131. }
  132. if (isEnumType(typeFromSchema)) {
  133. return 'enums'
  134. }
  135. if (isInputObjectType(typeFromSchema)) {
  136. return 'input-objects'
  137. }
  138. console.error(`cannot find type kind of ${type}`)
  139. }
  140. function removeMarkers (str) {
  141. return str.replace('[', '')
  142. .replace(']', '')
  143. .replace(/!/g, '')
  144. }
  145. module.exports = {
  146. getArguments,
  147. getDeprecationReason,
  148. getDeprecationStatus,
  149. getDescription,
  150. getFullLink,
  151. getId,
  152. getKind,
  153. getPreview,
  154. getType,
  155. getTypeKind
  156. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...