@constructive-io/graphql-codegen 2.18.0 → 2.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/README.md +1818 -113
  2. package/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  3. package/__tests__/codegen/input-types-generator.test.js +635 -0
  4. package/cli/codegen/barrel.d.ts +27 -0
  5. package/cli/codegen/barrel.js +163 -0
  6. package/cli/codegen/client.d.ts +4 -0
  7. package/cli/codegen/client.js +170 -0
  8. package/cli/codegen/custom-mutations.d.ts +38 -0
  9. package/cli/codegen/custom-mutations.js +149 -0
  10. package/cli/codegen/custom-queries.d.ts +38 -0
  11. package/cli/codegen/custom-queries.js +358 -0
  12. package/cli/codegen/filters.d.ts +27 -0
  13. package/cli/codegen/filters.js +357 -0
  14. package/cli/codegen/gql-ast.d.ts +41 -0
  15. package/cli/codegen/gql-ast.js +329 -0
  16. package/cli/codegen/index.d.ts +71 -0
  17. package/cli/codegen/index.js +147 -0
  18. package/cli/codegen/mutations.d.ts +30 -0
  19. package/cli/codegen/mutations.js +410 -0
  20. package/cli/codegen/orm/barrel.d.ts +18 -0
  21. package/cli/codegen/orm/barrel.js +48 -0
  22. package/cli/codegen/orm/client-generator.d.ts +45 -0
  23. package/cli/codegen/orm/client-generator.js +646 -0
  24. package/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  25. package/cli/codegen/orm/custom-ops-generator.js +350 -0
  26. package/cli/codegen/orm/index.d.ts +38 -0
  27. package/cli/codegen/orm/index.js +88 -0
  28. package/cli/codegen/orm/input-types-generator.d.ts +21 -0
  29. package/cli/codegen/orm/input-types-generator.js +705 -0
  30. package/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  31. package/cli/codegen/orm/input-types-generator.test.js +75 -0
  32. package/cli/codegen/orm/model-generator.d.ts +32 -0
  33. package/cli/codegen/orm/model-generator.js +264 -0
  34. package/cli/codegen/orm/query-builder.d.ts +161 -0
  35. package/cli/codegen/orm/query-builder.js +366 -0
  36. package/cli/codegen/orm/select-types.d.ts +169 -0
  37. package/cli/codegen/orm/select-types.js +16 -0
  38. package/cli/codegen/orm/select-types.test.d.ts +11 -0
  39. package/cli/codegen/orm/select-types.test.js +22 -0
  40. package/cli/codegen/queries.d.ts +25 -0
  41. package/cli/codegen/queries.js +438 -0
  42. package/cli/codegen/scalars.d.ts +12 -0
  43. package/cli/codegen/scalars.js +71 -0
  44. package/cli/codegen/schema-gql-ast.d.ts +51 -0
  45. package/cli/codegen/schema-gql-ast.js +385 -0
  46. package/cli/codegen/ts-ast.d.ts +122 -0
  47. package/cli/codegen/ts-ast.js +280 -0
  48. package/cli/codegen/type-resolver.d.ts +96 -0
  49. package/cli/codegen/type-resolver.js +246 -0
  50. package/cli/codegen/types.d.ts +12 -0
  51. package/cli/codegen/types.js +69 -0
  52. package/cli/codegen/utils.d.ts +163 -0
  53. package/cli/codegen/utils.js +326 -0
  54. package/cli/commands/generate-orm.d.ts +37 -0
  55. package/cli/commands/generate-orm.js +195 -0
  56. package/cli/commands/generate.d.ts +39 -0
  57. package/cli/commands/generate.js +299 -0
  58. package/cli/commands/index.d.ts +7 -0
  59. package/cli/commands/index.js +12 -0
  60. package/cli/commands/init.d.ts +35 -0
  61. package/cli/commands/init.js +176 -0
  62. package/cli/index.d.ts +4 -0
  63. package/cli/index.js +291 -0
  64. package/cli/introspect/fetch-meta.d.ts +31 -0
  65. package/cli/introspect/fetch-meta.js +108 -0
  66. package/cli/introspect/fetch-schema.d.ts +21 -0
  67. package/cli/introspect/fetch-schema.js +86 -0
  68. package/cli/introspect/index.d.ts +8 -0
  69. package/cli/introspect/index.js +16 -0
  70. package/cli/introspect/meta-query.d.ts +111 -0
  71. package/cli/introspect/meta-query.js +191 -0
  72. package/cli/introspect/schema-query.d.ts +20 -0
  73. package/cli/introspect/schema-query.js +123 -0
  74. package/cli/introspect/transform-schema.d.ts +74 -0
  75. package/cli/introspect/transform-schema.js +269 -0
  76. package/cli/introspect/transform-schema.test.d.ts +1 -0
  77. package/cli/introspect/transform-schema.test.js +67 -0
  78. package/cli/introspect/transform.d.ts +21 -0
  79. package/cli/introspect/transform.js +216 -0
  80. package/cli/watch/cache.d.ts +45 -0
  81. package/cli/watch/cache.js +111 -0
  82. package/cli/watch/debounce.d.ts +19 -0
  83. package/cli/watch/debounce.js +89 -0
  84. package/cli/watch/hash.d.ts +17 -0
  85. package/cli/watch/hash.js +48 -0
  86. package/cli/watch/index.d.ts +10 -0
  87. package/cli/watch/index.js +22 -0
  88. package/cli/watch/orchestrator.d.ts +63 -0
  89. package/cli/watch/orchestrator.js +228 -0
  90. package/cli/watch/poller.d.ts +65 -0
  91. package/cli/watch/poller.js +203 -0
  92. package/cli/watch/types.d.ts +67 -0
  93. package/cli/watch/types.js +5 -0
  94. package/client/error.d.ts +95 -0
  95. package/client/error.js +255 -0
  96. package/client/execute.d.ts +57 -0
  97. package/client/execute.js +124 -0
  98. package/client/index.d.ts +6 -0
  99. package/client/index.js +18 -0
  100. package/client/typed-document.d.ts +31 -0
  101. package/client/typed-document.js +44 -0
  102. package/core/ast.d.ts +10 -0
  103. package/core/ast.js +593 -0
  104. package/core/custom-ast.d.ts +35 -0
  105. package/core/custom-ast.js +204 -0
  106. package/core/index.d.ts +8 -0
  107. package/core/index.js +33 -0
  108. package/core/meta-object/convert.d.ts +65 -0
  109. package/core/meta-object/convert.js +63 -0
  110. package/core/meta-object/format.json +93 -0
  111. package/core/meta-object/index.d.ts +2 -0
  112. package/core/meta-object/index.js +18 -0
  113. package/core/meta-object/validate.d.ts +9 -0
  114. package/core/meta-object/validate.js +34 -0
  115. package/core/query-builder.d.ts +46 -0
  116. package/core/query-builder.js +412 -0
  117. package/core/types.d.ts +139 -0
  118. package/core/types.js +28 -0
  119. package/esm/__tests__/codegen/input-types-generator.test.d.ts +1 -0
  120. package/esm/__tests__/codegen/input-types-generator.test.js +633 -0
  121. package/esm/cli/codegen/barrel.d.ts +27 -0
  122. package/esm/cli/codegen/barrel.js +156 -0
  123. package/esm/cli/codegen/client.d.ts +4 -0
  124. package/esm/cli/codegen/client.js +167 -0
  125. package/esm/cli/codegen/custom-mutations.d.ts +38 -0
  126. package/esm/cli/codegen/custom-mutations.js +145 -0
  127. package/esm/cli/codegen/custom-queries.d.ts +38 -0
  128. package/esm/cli/codegen/custom-queries.js +354 -0
  129. package/esm/cli/codegen/filters.d.ts +27 -0
  130. package/esm/cli/codegen/filters.js +351 -0
  131. package/esm/cli/codegen/gql-ast.d.ts +41 -0
  132. package/esm/cli/codegen/gql-ast.js +288 -0
  133. package/esm/cli/codegen/index.d.ts +71 -0
  134. package/esm/cli/codegen/index.js +124 -0
  135. package/esm/cli/codegen/mutations.d.ts +30 -0
  136. package/esm/cli/codegen/mutations.js +404 -0
  137. package/esm/cli/codegen/orm/barrel.d.ts +18 -0
  138. package/esm/cli/codegen/orm/barrel.js +44 -0
  139. package/esm/cli/codegen/orm/client-generator.d.ts +45 -0
  140. package/esm/cli/codegen/orm/client-generator.js +640 -0
  141. package/esm/cli/codegen/orm/custom-ops-generator.d.ts +30 -0
  142. package/esm/cli/codegen/orm/custom-ops-generator.js +346 -0
  143. package/esm/cli/codegen/orm/index.d.ts +38 -0
  144. package/esm/cli/codegen/orm/index.js +75 -0
  145. package/esm/cli/codegen/orm/input-types-generator.d.ts +21 -0
  146. package/esm/cli/codegen/orm/input-types-generator.js +700 -0
  147. package/esm/cli/codegen/orm/input-types-generator.test.d.ts +1 -0
  148. package/esm/cli/codegen/orm/input-types-generator.test.js +73 -0
  149. package/esm/cli/codegen/orm/model-generator.d.ts +32 -0
  150. package/esm/cli/codegen/orm/model-generator.js +260 -0
  151. package/esm/cli/codegen/orm/query-builder.d.ts +161 -0
  152. package/esm/cli/codegen/orm/query-builder.js +353 -0
  153. package/esm/cli/codegen/orm/select-types.d.ts +169 -0
  154. package/esm/cli/codegen/orm/select-types.js +15 -0
  155. package/esm/cli/codegen/orm/select-types.test.d.ts +11 -0
  156. package/esm/cli/codegen/orm/select-types.test.js +21 -0
  157. package/esm/cli/codegen/queries.d.ts +25 -0
  158. package/esm/cli/codegen/queries.js +433 -0
  159. package/esm/cli/codegen/scalars.d.ts +12 -0
  160. package/esm/cli/codegen/scalars.js +66 -0
  161. package/esm/cli/codegen/schema-gql-ast.d.ts +51 -0
  162. package/esm/cli/codegen/schema-gql-ast.js +343 -0
  163. package/esm/cli/codegen/ts-ast.d.ts +122 -0
  164. package/esm/cli/codegen/ts-ast.js +260 -0
  165. package/esm/cli/codegen/type-resolver.d.ts +96 -0
  166. package/esm/cli/codegen/type-resolver.js +224 -0
  167. package/esm/cli/codegen/types.d.ts +12 -0
  168. package/esm/cli/codegen/types.js +65 -0
  169. package/esm/cli/codegen/utils.d.ts +163 -0
  170. package/esm/cli/codegen/utils.js +288 -0
  171. package/esm/cli/commands/generate-orm.d.ts +37 -0
  172. package/esm/cli/commands/generate-orm.js +192 -0
  173. package/esm/cli/commands/generate.d.ts +39 -0
  174. package/esm/cli/commands/generate.js +262 -0
  175. package/esm/cli/commands/index.d.ts +7 -0
  176. package/esm/cli/commands/index.js +5 -0
  177. package/esm/cli/commands/init.d.ts +35 -0
  178. package/esm/cli/commands/init.js +138 -0
  179. package/esm/cli/index.d.ts +4 -0
  180. package/esm/cli/index.js +256 -0
  181. package/esm/cli/introspect/fetch-meta.d.ts +31 -0
  182. package/esm/cli/introspect/fetch-meta.js +104 -0
  183. package/esm/cli/introspect/fetch-schema.d.ts +21 -0
  184. package/esm/cli/introspect/fetch-schema.js +83 -0
  185. package/esm/cli/introspect/index.d.ts +8 -0
  186. package/esm/cli/introspect/index.js +6 -0
  187. package/esm/cli/introspect/meta-query.d.ts +111 -0
  188. package/esm/cli/introspect/meta-query.js +188 -0
  189. package/esm/cli/introspect/schema-query.d.ts +20 -0
  190. package/esm/cli/introspect/schema-query.js +120 -0
  191. package/esm/cli/introspect/transform-schema.d.ts +74 -0
  192. package/esm/cli/introspect/transform-schema.js +259 -0
  193. package/esm/cli/introspect/transform-schema.test.d.ts +1 -0
  194. package/esm/cli/introspect/transform-schema.test.js +65 -0
  195. package/esm/cli/introspect/transform.d.ts +21 -0
  196. package/esm/cli/introspect/transform.js +210 -0
  197. package/esm/cli/watch/cache.d.ts +45 -0
  198. package/esm/cli/watch/cache.js +73 -0
  199. package/esm/cli/watch/debounce.d.ts +19 -0
  200. package/esm/cli/watch/debounce.js +85 -0
  201. package/esm/cli/watch/hash.d.ts +17 -0
  202. package/esm/cli/watch/hash.js +43 -0
  203. package/esm/cli/watch/index.d.ts +10 -0
  204. package/esm/cli/watch/index.js +8 -0
  205. package/esm/cli/watch/orchestrator.d.ts +63 -0
  206. package/esm/cli/watch/orchestrator.js +223 -0
  207. package/esm/cli/watch/poller.d.ts +65 -0
  208. package/esm/cli/watch/poller.js +198 -0
  209. package/esm/cli/watch/types.d.ts +67 -0
  210. package/esm/cli/watch/types.js +4 -0
  211. package/esm/client/error.d.ts +95 -0
  212. package/esm/client/error.js +249 -0
  213. package/esm/client/execute.d.ts +57 -0
  214. package/esm/client/execute.js +120 -0
  215. package/esm/client/index.d.ts +6 -0
  216. package/esm/client/index.js +6 -0
  217. package/esm/client/typed-document.d.ts +31 -0
  218. package/esm/client/typed-document.js +40 -0
  219. package/esm/core/ast.d.ts +10 -0
  220. package/esm/core/ast.js +549 -0
  221. package/esm/core/custom-ast.d.ts +35 -0
  222. package/esm/core/custom-ast.js +161 -0
  223. package/esm/core/index.d.ts +8 -0
  224. package/esm/core/index.js +12 -0
  225. package/esm/core/meta-object/convert.d.ts +65 -0
  226. package/esm/core/meta-object/convert.js +60 -0
  227. package/esm/core/meta-object/format.json +93 -0
  228. package/esm/core/meta-object/index.d.ts +2 -0
  229. package/esm/core/meta-object/index.js +2 -0
  230. package/esm/core/meta-object/validate.d.ts +9 -0
  231. package/esm/core/meta-object/validate.js +28 -0
  232. package/esm/core/query-builder.d.ts +46 -0
  233. package/esm/core/query-builder.js +375 -0
  234. package/esm/core/types.d.ts +139 -0
  235. package/esm/core/types.js +24 -0
  236. package/esm/generators/field-selector.d.ts +30 -0
  237. package/esm/generators/field-selector.js +355 -0
  238. package/esm/generators/index.d.ts +6 -0
  239. package/esm/generators/index.js +9 -0
  240. package/esm/generators/mutations.d.ts +31 -0
  241. package/esm/generators/mutations.js +197 -0
  242. package/esm/generators/select.d.ts +50 -0
  243. package/esm/generators/select.js +636 -0
  244. package/esm/index.d.ts +12 -0
  245. package/esm/index.js +17 -3
  246. package/esm/react/index.d.ts +5 -0
  247. package/esm/react/index.js +6 -0
  248. package/esm/types/config.d.ts +199 -0
  249. package/esm/types/config.js +106 -0
  250. package/esm/types/index.d.ts +9 -0
  251. package/esm/types/index.js +4 -0
  252. package/esm/types/introspection.d.ts +121 -0
  253. package/esm/types/introspection.js +54 -0
  254. package/esm/types/mutation.d.ts +45 -0
  255. package/esm/types/mutation.js +4 -0
  256. package/esm/types/query.d.ts +82 -0
  257. package/esm/types/query.js +4 -0
  258. package/esm/types/schema.d.ts +253 -0
  259. package/esm/types/schema.js +5 -0
  260. package/esm/types/selection.d.ts +43 -0
  261. package/esm/types/selection.js +4 -0
  262. package/esm/utils/index.d.ts +4 -0
  263. package/esm/utils/index.js +4 -0
  264. package/generators/field-selector.d.ts +30 -0
  265. package/generators/field-selector.js +361 -0
  266. package/generators/index.d.ts +6 -0
  267. package/generators/index.js +27 -0
  268. package/generators/mutations.d.ts +31 -0
  269. package/generators/mutations.js +235 -0
  270. package/generators/select.d.ts +50 -0
  271. package/generators/select.js +679 -0
  272. package/index.d.ts +12 -3
  273. package/index.js +19 -3
  274. package/package.json +59 -38
  275. package/react/index.d.ts +5 -0
  276. package/react/index.js +9 -0
  277. package/types/config.d.ts +199 -0
  278. package/types/config.js +111 -0
  279. package/types/index.d.ts +9 -0
  280. package/types/index.js +10 -0
  281. package/types/introspection.d.ts +121 -0
  282. package/types/introspection.js +62 -0
  283. package/types/mutation.d.ts +45 -0
  284. package/types/mutation.js +5 -0
  285. package/types/query.d.ts +82 -0
  286. package/types/query.js +5 -0
  287. package/types/schema.d.ts +253 -0
  288. package/types/schema.js +6 -0
  289. package/types/selection.d.ts +43 -0
  290. package/types/selection.js +5 -0
  291. package/utils/index.d.ts +4 -0
  292. package/utils/index.js +7 -0
  293. package/codegen.d.ts +0 -13
  294. package/codegen.js +0 -293
  295. package/esm/codegen.js +0 -253
  296. package/esm/gql.js +0 -939
  297. package/esm/options.js +0 -27
  298. package/gql.d.ts +0 -188
  299. package/gql.js +0 -992
  300. package/options.d.ts +0 -45
  301. package/options.js +0 -31
package/esm/gql.js DELETED
@@ -1,939 +0,0 @@
1
- // TODO: use inflection for all the things
2
- // const { singularize } = require('inflection');
3
- import * as t from 'gql-ast';
4
- import inflection from 'inflection';
5
- const NON_MUTABLE_PROPS = [
6
- 'id',
7
- 'createdAt',
8
- 'createdBy',
9
- 'updatedAt',
10
- 'updatedBy',
11
- ];
12
- const objectToArray = (obj) => Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));
13
- function refToTypeNode(ref, overrides) {
14
- if (!ref)
15
- return null;
16
- if (ref.kind === 'NON_NULL') {
17
- const inner = refToTypeNode(ref.ofType, overrides);
18
- return t.nonNullType({ type: inner });
19
- }
20
- if (ref.kind === 'LIST') {
21
- const inner = refToTypeNode(ref.ofType, overrides);
22
- return t.listType({ type: inner });
23
- }
24
- const name = (overrides && overrides[ref.name]) || ref.name;
25
- return t.namedType({ type: name });
26
- }
27
- function resolveTypeName(name, type, overrides) {
28
- if (typeof type === 'string') {
29
- const base = type;
30
- const mapped = overrides && overrides[base];
31
- return mapped || base;
32
- }
33
- if (type && typeof type === 'object') {
34
- if (typeof type.name === 'string' && type.name.length > 0)
35
- return type.name;
36
- let t = type;
37
- while (t && typeof t === 'object' && t.ofType)
38
- t = t.ofType;
39
- if (t && typeof t.name === 'string' && t.name.length > 0) {
40
- const base = t.name;
41
- const mapped = overrides && overrides[base];
42
- return mapped || base;
43
- }
44
- }
45
- return 'JSON';
46
- }
47
- function refToNamedTypeName(ref) {
48
- let r = ref;
49
- while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
50
- r = r.ofType;
51
- return r && r.name ? r.name : null;
52
- }
53
- function extractNamedTypeName(node) {
54
- let n = node;
55
- while (n && !n.name && n.type)
56
- n = n.type;
57
- return n && n.name ? n.name : null;
58
- }
59
- function singularModel(name) {
60
- return inflection.singularize(name);
61
- }
62
- export const createGqlMutation = ({ operationName, mutationName, selectArgs, variableDefinitions, modelName, selections, useModel = true, }) => {
63
- const opSel = !modelName
64
- ? [
65
- t.field({
66
- name: operationName,
67
- args: selectArgs,
68
- selectionSet: t.selectionSet({ selections }),
69
- }),
70
- ]
71
- : [
72
- t.field({
73
- name: operationName,
74
- args: selectArgs,
75
- selectionSet: t.selectionSet({
76
- selections: useModel
77
- ? [
78
- t.field({
79
- name: modelName,
80
- selectionSet: t.selectionSet({ selections }),
81
- }),
82
- ]
83
- : selections,
84
- }),
85
- }),
86
- ];
87
- return t.document({
88
- definitions: [
89
- t.operationDefinition({
90
- operation: 'mutation',
91
- name: mutationName,
92
- variableDefinitions,
93
- selectionSet: t.selectionSet({ selections: opSel }),
94
- }),
95
- ],
96
- });
97
- };
98
- export const getMany = ({ operationName, query, fields, }) => {
99
- const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query', 'all'].join('_'), true);
100
- const selections = getSelections(query, fields);
101
- const opSel = [
102
- t.field({
103
- name: operationName,
104
- selectionSet: t.selectionSet({
105
- selections: [
106
- t.field({ name: 'totalCount' }),
107
- t.field({
108
- name: 'pageInfo',
109
- selectionSet: t.selectionSet({
110
- selections: [
111
- t.field({ name: 'hasNextPage' }),
112
- t.field({ name: 'hasPreviousPage' }),
113
- t.field({ name: 'endCursor' }),
114
- t.field({ name: 'startCursor' }),
115
- ],
116
- }),
117
- }),
118
- t.field({
119
- name: 'edges',
120
- selectionSet: t.selectionSet({
121
- selections: [
122
- t.field({ name: 'cursor' }),
123
- t.field({
124
- name: 'node',
125
- selectionSet: t.selectionSet({ selections }),
126
- }),
127
- ],
128
- }),
129
- }),
130
- ],
131
- }),
132
- }),
133
- ];
134
- const ast = t.document({
135
- definitions: [
136
- t.operationDefinition({
137
- operation: 'query',
138
- name: queryName,
139
- selectionSet: t.selectionSet({ selections: opSel }),
140
- }),
141
- ],
142
- });
143
- return { name: queryName, ast };
144
- };
145
- export const getManyPaginatedEdges = ({ operationName, query, fields, }) => {
146
- const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'paginated'].join('_'), true);
147
- const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
148
- const Singular = query.model;
149
- const Condition = `${Singular}Condition`;
150
- const Filter = `${Singular}Filter`;
151
- const OrderBy = `${Plural}OrderBy`;
152
- const selections = getSelections(query, fields);
153
- const variableDefinitions = [
154
- 'first',
155
- 'last',
156
- 'offset',
157
- 'after',
158
- 'before',
159
- ].map((name) => t.variableDefinition({
160
- variable: t.variable({ name }),
161
- type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
162
- }));
163
- variableDefinitions.push(t.variableDefinition({
164
- variable: t.variable({ name: 'condition' }),
165
- type: t.namedType({ type: Condition }),
166
- }), t.variableDefinition({
167
- variable: t.variable({ name: 'filter' }),
168
- type: t.namedType({ type: Filter }),
169
- }), t.variableDefinition({
170
- variable: t.variable({ name: 'orderBy' }),
171
- type: t.listType({
172
- type: t.nonNullType({
173
- type: t.namedType({ type: OrderBy }),
174
- }),
175
- }),
176
- }));
177
- const args = [
178
- 'first',
179
- 'last',
180
- 'offset',
181
- 'after',
182
- 'before',
183
- 'condition',
184
- 'filter',
185
- 'orderBy',
186
- ].map((name) => t.argument({
187
- name,
188
- value: t.variable({ name }),
189
- }));
190
- const ast = t.document({
191
- definitions: [
192
- t.operationDefinition({
193
- operation: 'query',
194
- name: queryName,
195
- variableDefinitions,
196
- selectionSet: t.selectionSet({
197
- selections: [
198
- t.field({
199
- name: operationName,
200
- args,
201
- selectionSet: t.selectionSet({
202
- selections: [
203
- t.field({ name: 'totalCount' }),
204
- t.field({
205
- name: 'pageInfo',
206
- selectionSet: t.selectionSet({
207
- selections: [
208
- t.field({ name: 'hasNextPage' }),
209
- t.field({ name: 'hasPreviousPage' }),
210
- t.field({ name: 'endCursor' }),
211
- t.field({ name: 'startCursor' }),
212
- ],
213
- }),
214
- }),
215
- t.field({
216
- name: 'edges',
217
- selectionSet: t.selectionSet({
218
- selections: [
219
- t.field({ name: 'cursor' }),
220
- t.field({
221
- name: 'node',
222
- selectionSet: t.selectionSet({ selections }),
223
- }),
224
- ],
225
- }),
226
- }),
227
- ],
228
- }),
229
- }),
230
- ],
231
- }),
232
- }),
233
- ],
234
- });
235
- return { name: queryName, ast };
236
- };
237
- export const getManyPaginatedNodes = ({ operationName, query, fields, }) => {
238
- const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query'].join('_'), true);
239
- const Singular = query.model;
240
- const Plural = operationName.charAt(0).toUpperCase() + operationName.slice(1);
241
- const Condition = `${Singular}Condition`;
242
- const Filter = `${Singular}Filter`;
243
- const OrderBy = `${Plural}OrderBy`;
244
- const selections = getSelections(query, fields);
245
- const variableDefinitions = [
246
- 'first',
247
- 'last',
248
- 'after',
249
- 'before',
250
- 'offset',
251
- ].map((name) => t.variableDefinition({
252
- variable: t.variable({ name }),
253
- type: t.namedType({ type: name === 'after' || name === 'before' ? 'Cursor' : 'Int' }),
254
- }));
255
- variableDefinitions.push(t.variableDefinition({
256
- variable: t.variable({ name: 'condition' }),
257
- type: t.namedType({ type: Condition }),
258
- }), t.variableDefinition({
259
- variable: t.variable({ name: 'filter' }),
260
- type: t.namedType({ type: Filter }),
261
- }), t.variableDefinition({
262
- variable: t.variable({ name: 'orderBy' }),
263
- type: t.listType({
264
- type: t.nonNullType({
265
- type: t.namedType({ type: OrderBy }),
266
- }),
267
- }),
268
- }));
269
- const args = [
270
- 'first',
271
- 'last',
272
- 'offset',
273
- 'after',
274
- 'before',
275
- 'condition',
276
- 'filter',
277
- 'orderBy',
278
- ].map((name) => t.argument({
279
- name,
280
- value: t.variable({ name }),
281
- }));
282
- const ast = t.document({
283
- definitions: [
284
- t.operationDefinition({
285
- operation: 'query',
286
- name: queryName,
287
- variableDefinitions,
288
- selectionSet: t.selectionSet({
289
- selections: [
290
- t.field({
291
- name: operationName,
292
- args,
293
- selectionSet: t.selectionSet({
294
- selections: [
295
- t.field({ name: 'totalCount' }),
296
- t.field({
297
- name: 'pageInfo',
298
- selectionSet: t.selectionSet({
299
- selections: [
300
- t.field({ name: 'hasNextPage' }),
301
- t.field({ name: 'hasPreviousPage' }),
302
- t.field({ name: 'endCursor' }),
303
- t.field({ name: 'startCursor' }),
304
- ],
305
- }),
306
- }),
307
- t.field({
308
- name: 'nodes',
309
- selectionSet: t.selectionSet({ selections }),
310
- }),
311
- ],
312
- }),
313
- }),
314
- ],
315
- }),
316
- }),
317
- ],
318
- });
319
- return { name: queryName, ast };
320
- };
321
- export const getOrderByEnums = ({ operationName, query, }) => {
322
- const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'Order', 'By', 'Enums'].join('_'), true);
323
- const Model = operationName.charAt(0).toUpperCase() + operationName.slice(1);
324
- const OrderBy = `${Model}OrderBy`;
325
- const ast = t.document({
326
- definitions: [
327
- t.operationDefinition({
328
- operation: 'query',
329
- name: queryName,
330
- selectionSet: t.selectionSet({
331
- selections: [
332
- t.field({
333
- name: '__type',
334
- args: [
335
- t.argument({
336
- name: 'name',
337
- value: t.stringValue({ value: OrderBy }),
338
- }),
339
- ],
340
- selectionSet: t.selectionSet({
341
- selections: [
342
- t.field({
343
- name: 'enumValues',
344
- selectionSet: t.selectionSet({
345
- selections: [t.field({ name: 'name' })],
346
- }),
347
- }),
348
- ],
349
- }),
350
- }),
351
- ],
352
- }),
353
- }),
354
- ],
355
- });
356
- return { name: queryName, ast };
357
- };
358
- export const getFragment = ({ operationName, query, }) => {
359
- const queryName = inflection.camelize([inflection.underscore(query.model), 'Fragment'].join('_'), true);
360
- const selections = getSelections(query);
361
- const ast = t.document({
362
- definitions: [
363
- t.fragmentDefinition({
364
- name: queryName,
365
- typeCondition: t.namedType({
366
- type: query.model,
367
- }),
368
- selectionSet: t.selectionSet({
369
- selections,
370
- }),
371
- }),
372
- ],
373
- });
374
- return { name: queryName, ast };
375
- };
376
- export const getOne = ({ operationName, query, fields, }, typeNameOverrides) => {
377
- const queryName = inflection.camelize(['get', inflection.underscore(operationName), 'query'].join('_'), true);
378
- const variableDefinitions = objectToArray(query.properties)
379
- .filter((field) => field.isNotNull)
380
- .map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
381
- const typeName = resolveTypeName(name, type, typeNameOverrides);
382
- let gqlType = t.namedType({ type: typeName });
383
- if (isNotNull) {
384
- gqlType = t.nonNullType({ type: gqlType });
385
- }
386
- if (isArray) {
387
- gqlType = t.listType({ type: gqlType });
388
- if (isArrayNotNull) {
389
- gqlType = t.nonNullType({ type: gqlType });
390
- }
391
- }
392
- return t.variableDefinition({
393
- variable: t.variable({ name }),
394
- type: gqlType,
395
- });
396
- });
397
- const selectArgs = objectToArray(query.properties)
398
- .filter((field) => field.isNotNull)
399
- .map((field) => t.argument({
400
- name: field.name,
401
- value: t.variable({ name: field.name }),
402
- }));
403
- const selections = getSelections(query, fields);
404
- const opSel = [
405
- t.field({
406
- name: operationName,
407
- args: selectArgs,
408
- selectionSet: t.selectionSet({ selections }),
409
- }),
410
- ];
411
- const ast = t.document({
412
- definitions: [
413
- t.operationDefinition({
414
- operation: 'query',
415
- name: queryName,
416
- variableDefinitions,
417
- selectionSet: t.selectionSet({ selections: opSel }),
418
- }),
419
- ],
420
- });
421
- return { name: queryName, ast };
422
- };
423
- export const createOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
424
- const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
425
- if (!mutation.properties?.input?.properties) {
426
- console.log('no input field for mutation for ' + mutationName);
427
- return;
428
- }
429
- const modelName = inflection.camelize([singularModel(mutation.model)].join('_'), true);
430
- const allAttrs = objectToArray(mutation.properties.input.properties[modelName].properties);
431
- const attrs = allAttrs.filter((field) => field.name === 'id' ? Boolean(field.isNotNull) : !NON_MUTABLE_PROPS.includes(field.name));
432
- const useRaw = selection?.mutationInputMode === 'raw';
433
- const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
434
- let unresolved = 0;
435
- let modelInputName = null;
436
- if (typeIndex && inputTypeName) {
437
- const modelRef = typeIndex.getInputFieldType(inputTypeName, modelName);
438
- modelInputName = refToNamedTypeName(modelRef);
439
- }
440
- const variableDefinitions = attrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
441
- let gqlType = null;
442
- if (typeIndex && modelInputName) {
443
- const fieldTypeRef = typeIndex.getInputFieldType(modelInputName, name);
444
- const tn = refToTypeNode(fieldTypeRef, typeNameOverrides);
445
- if (tn)
446
- gqlType = tn;
447
- }
448
- if (!gqlType) {
449
- const typeName = resolveTypeName(name, type, typeNameOverrides);
450
- gqlType = t.namedType({ type: typeName });
451
- if (isNotNull) {
452
- gqlType = t.nonNullType({ type: gqlType });
453
- }
454
- if (isArray) {
455
- gqlType = t.listType({ type: gqlType });
456
- if (isArrayNotNull) {
457
- gqlType = t.nonNullType({ type: gqlType });
458
- }
459
- }
460
- }
461
- const nn = extractNamedTypeName(gqlType);
462
- if (nn === 'JSON')
463
- unresolved++;
464
- return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
465
- });
466
- const mustUseRaw = useRaw || unresolved > 0;
467
- const selectArgs = mustUseRaw
468
- ? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
469
- : [
470
- t.argument({
471
- name: 'input',
472
- value: t.objectValue({
473
- fields: [
474
- t.objectField({
475
- name: modelName,
476
- value: t.objectValue({
477
- fields: attrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
478
- }),
479
- }),
480
- ],
481
- }),
482
- }),
483
- ];
484
- let idExists = true;
485
- let availableFieldNames = [];
486
- if (typeIndex) {
487
- const typ = typeIndex.byName?.[mutation.model];
488
- const fields = (typ && Array.isArray(typ.fields)) ? typ.fields : [];
489
- idExists = fields.some((f) => f && f.name === 'id');
490
- availableFieldNames = fields
491
- .filter((f) => {
492
- let r = f.type;
493
- while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
494
- r = r.ofType;
495
- const kind = r?.kind;
496
- return kind === 'SCALAR' || kind === 'ENUM';
497
- })
498
- .map((f) => f.name);
499
- }
500
- const finalFields = Array.from(new Set([...(idExists ? ['id'] : []), ...availableFieldNames]));
501
- const nested = (finalFields.length > 0)
502
- ? [t.field({
503
- name: modelName,
504
- selectionSet: t.selectionSet({ selections: finalFields.map((f) => t.field({ name: f })) }),
505
- })]
506
- : [];
507
- const ast = createGqlMutation({
508
- operationName,
509
- mutationName,
510
- selectArgs,
511
- selections: [...nested, t.field({ name: 'clientMutationId' })],
512
- variableDefinitions: mustUseRaw
513
- ? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
514
- : variableDefinitions,
515
- useModel: false,
516
- });
517
- return { name: mutationName, ast };
518
- };
519
- export const patchOne = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
520
- const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
521
- if (!mutation.properties?.input?.properties) {
522
- console.log('no input field for mutation for ' + mutationName);
523
- return;
524
- }
525
- const modelName = inflection.camelize([singularModel(mutation.model)].join('_'), true);
526
- // @ts-ignore
527
- const allAttrs = objectToArray(mutation.properties.input.properties['patch']?.properties || {});
528
- const patchAttrs = allAttrs.filter(
529
- // @ts-ignore
530
- (prop) => !NON_MUTABLE_PROPS.includes(prop.name));
531
- const patchByAttrs = objectToArray(mutation.properties.input.properties).filter((n) => n.name !== 'patch');
532
- const patchers = patchByAttrs.map((p) => p.name);
533
- const useCollapsedOpt = selection?.mutationInputMode === 'patchCollapsed';
534
- const ModelPascal = inflection.camelize(singularModel(mutation.model), false);
535
- const patchTypeName = `${ModelPascal}Patch`;
536
- const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
537
- let unresolved = 0;
538
- const patchAttrVarDefs = useCollapsedOpt
539
- ? [
540
- t.variableDefinition({
541
- variable: t.variable({ name: 'patch' }),
542
- type: t.nonNullType({ type: t.namedType({ type: patchTypeName }) }),
543
- }),
544
- ]
545
- : patchAttrs
546
- .filter((field) => !patchers.includes(field.name))
547
- .map(({ name, type, isArray }) => {
548
- let gqlType = null;
549
- if (typeIndex) {
550
- const pType = typeIndex.byName[patchTypeName];
551
- const f = pType && pType.inputFields && pType.inputFields.find((x) => x.name === name);
552
- if (f && f.type)
553
- gqlType = refToTypeNode(f.type, typeNameOverrides);
554
- }
555
- if (!gqlType) {
556
- const typeName = resolveTypeName(name, type, typeNameOverrides);
557
- gqlType = t.namedType({ type: typeName });
558
- if (isArray) {
559
- gqlType = t.listType({ type: gqlType });
560
- }
561
- if (patchers.includes(name)) {
562
- gqlType = t.nonNullType({ type: gqlType });
563
- }
564
- }
565
- const nn = extractNamedTypeName(gqlType);
566
- if (nn === 'JSON')
567
- unresolved++;
568
- return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
569
- });
570
- const patchByVarDefs = patchByAttrs.map(({ name, type, isNotNull, isArray, isArrayNotNull }) => {
571
- let gqlType = null;
572
- if (typeIndex && inputTypeName) {
573
- const ref = typeIndex.getInputFieldType(inputTypeName, name);
574
- const tn = refToTypeNode(ref, typeNameOverrides);
575
- if (tn)
576
- gqlType = tn;
577
- }
578
- if (!gqlType) {
579
- const typeName = resolveTypeName(name, type, typeNameOverrides);
580
- gqlType = t.namedType({ type: typeName });
581
- if (isNotNull) {
582
- gqlType = t.nonNullType({ type: gqlType });
583
- }
584
- if (isArray) {
585
- gqlType = t.listType({ type: gqlType });
586
- if (isArrayNotNull) {
587
- gqlType = t.nonNullType({ type: gqlType });
588
- }
589
- }
590
- }
591
- const nn = extractNamedTypeName(gqlType);
592
- if (nn === 'JSON')
593
- unresolved++;
594
- return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
595
- });
596
- const mustUseRaw = unresolved > 0;
597
- const selectArgs = mustUseRaw
598
- ? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
599
- : [
600
- t.argument({
601
- name: 'input',
602
- value: t.objectValue({
603
- fields: [
604
- ...patchByAttrs.map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
605
- t.objectField({
606
- name: 'patch',
607
- value: useCollapsedOpt ? t.variable({ name: 'patch' }) : t.objectValue({
608
- fields: patchAttrs
609
- .filter((field) => !patchers.includes(field.name))
610
- .map((field) => t.objectField({ name: field.name, value: t.variable({ name: field.name }) })),
611
- }),
612
- }),
613
- ],
614
- }),
615
- }),
616
- ];
617
- let idExistsPatch = true;
618
- if (typeIndex) {
619
- const typ = typeIndex.byName?.[mutation.model];
620
- const fields = (typ && Array.isArray(typ.fields)) ? typ.fields : [];
621
- idExistsPatch = fields.some((f) => f && f.name === 'id');
622
- }
623
- const shouldDropIdPatch = /Extension$/i.test(modelName) || !idExistsPatch;
624
- const idSelection = shouldDropIdPatch ? [] : ['id'];
625
- const nestedPatch = (idSelection.length > 0)
626
- ? [t.field({
627
- name: modelName,
628
- selectionSet: t.selectionSet({ selections: idSelection.map((f) => t.field({ name: f })) }),
629
- })]
630
- : [];
631
- const ast = createGqlMutation({
632
- operationName,
633
- mutationName,
634
- selectArgs,
635
- selections: [...nestedPatch, t.field({ name: 'clientMutationId' })],
636
- variableDefinitions: mustUseRaw
637
- ? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
638
- : [...patchByVarDefs, ...patchAttrVarDefs],
639
- useModel: false,
640
- });
641
- return { name: mutationName, ast };
642
- };
643
- export const deleteOne = ({ operationName, mutation, }, typeNameOverrides, typeIndex) => {
644
- const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
645
- if (!mutation.properties?.input?.properties) {
646
- console.log('no input field for mutation for ' + mutationName);
647
- return;
648
- }
649
- const modelName = inflection.camelize([singularModel(mutation.model)].join('_'), true);
650
- // @ts-ignore
651
- const deleteAttrs = objectToArray(mutation.properties.input.properties);
652
- const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
653
- let unresolved = 0;
654
- const variableDefinitions = deleteAttrs.map(({ name, type, isNotNull, isArray }) => {
655
- let gqlType = null;
656
- if (typeIndex && inputTypeName) {
657
- const ref = typeIndex.getInputFieldType(inputTypeName, name);
658
- const tn = refToTypeNode(ref, typeNameOverrides);
659
- if (tn)
660
- gqlType = tn;
661
- }
662
- if (!gqlType) {
663
- const typeName = resolveTypeName(name, type, typeNameOverrides);
664
- gqlType = t.namedType({ type: typeName });
665
- if (isNotNull) {
666
- gqlType = t.nonNullType({ type: gqlType });
667
- }
668
- if (isArray) {
669
- gqlType = t.listType({ type: gqlType });
670
- gqlType = t.nonNullType({ type: gqlType });
671
- }
672
- }
673
- const nn = extractNamedTypeName(gqlType);
674
- if (nn === 'JSON')
675
- unresolved++;
676
- return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
677
- });
678
- const mustUseRaw = unresolved > 0;
679
- const selectArgs = mustUseRaw
680
- ? [t.argument({ name: 'input', value: t.variable({ name: 'input' }) })]
681
- : [
682
- t.argument({
683
- name: 'input',
684
- value: t.objectValue({
685
- fields: deleteAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
686
- }),
687
- }),
688
- ];
689
- const selections = [t.field({ name: 'clientMutationId' })];
690
- const ast = createGqlMutation({
691
- operationName,
692
- mutationName,
693
- selectArgs,
694
- selections,
695
- variableDefinitions: mustUseRaw
696
- ? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
697
- : variableDefinitions,
698
- modelName,
699
- useModel: false,
700
- });
701
- return { name: mutationName, ast };
702
- };
703
- export const createMutation = ({ operationName, mutation, selection, }, typeNameOverrides, typeIndex) => {
704
- const mutationName = inflection.camelize([inflection.underscore(operationName), 'mutation'].join('_'), true);
705
- if (!mutation.properties?.input?.properties) {
706
- console.log('no input field for mutation for ' + mutationName);
707
- return;
708
- }
709
- // @ts-ignore
710
- const otherAttrs = objectToArray(mutation.properties.input.properties);
711
- const useRaw = selection?.mutationInputMode === 'raw';
712
- const inputTypeName = resolveTypeName('input', mutation.properties?.input?.type || mutation.properties?.input, typeNameOverrides);
713
- let unresolved = 0;
714
- const builtVarDefs = otherAttrs.map(({ name, type, isArray, isArrayNotNull }) => {
715
- let gqlType = null;
716
- if (typeIndex && inputTypeName) {
717
- const ref = typeIndex.getInputFieldType(inputTypeName, name);
718
- const tn = refToTypeNode(ref, typeNameOverrides);
719
- if (tn)
720
- gqlType = tn;
721
- }
722
- if (!gqlType) {
723
- const typeName = resolveTypeName(name, type, typeNameOverrides);
724
- gqlType = t.namedType({ type: typeName });
725
- gqlType = t.nonNullType({ type: gqlType });
726
- if (isArray) {
727
- gqlType = t.listType({ type: gqlType });
728
- if (isArrayNotNull) {
729
- gqlType = t.nonNullType({ type: gqlType });
730
- }
731
- }
732
- if (gqlType.type && gqlType.type.type && gqlType.type.type.name === 'JSON') {
733
- unresolved++;
734
- }
735
- }
736
- return t.variableDefinition({ variable: t.variable({ name }), type: gqlType });
737
- });
738
- const mustUseRaw = useRaw || otherAttrs.length === 0 || unresolved > 0;
739
- const variableDefinitions = mustUseRaw
740
- ? [t.variableDefinition({ variable: t.variable({ name: 'input' }), type: t.nonNullType({ type: t.namedType({ type: inputTypeName }) }) })]
741
- : builtVarDefs;
742
- const selectArgs = [
743
- t.argument({
744
- name: 'input',
745
- value: mustUseRaw
746
- ? t.variable({ name: 'input' })
747
- : t.objectValue({
748
- fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
749
- }),
750
- }),
751
- ];
752
- const scalarOutputs = (mutation.outputs || [])
753
- .filter((field) => field.type.kind === 'SCALAR')
754
- .map((f) => f.name);
755
- let objectOutputName = (mutation.outputs || [])
756
- .find((field) => field.type.kind === 'OBJECT')?.name;
757
- if (!objectOutputName) {
758
- const payloadTypeName = mutation?.output?.name;
759
- if (typeIndex && payloadTypeName) {
760
- const payloadType = typeIndex.byName?.[payloadTypeName];
761
- const fields = (payloadType && Array.isArray(payloadType.fields)) ? payloadType.fields : [];
762
- const match = fields
763
- .filter((f) => f && f.name !== 'clientMutationId')
764
- .filter((f) => (refToNamedTypeName(f.type) || f.type?.name) !== 'Query')
765
- .find((f) => (refToNamedTypeName(f.type) || f.type?.name) === mutation?.model);
766
- if (match)
767
- objectOutputName = match.name;
768
- }
769
- }
770
- const selections = [];
771
- if (objectOutputName) {
772
- const modelTypeName = mutation?.model;
773
- const modelType = typeIndex && modelTypeName ? typeIndex.byName?.[modelTypeName] : null;
774
- const fieldNames = (modelType && Array.isArray(modelType.fields))
775
- ? modelType.fields
776
- .filter((f) => {
777
- let r = f.type;
778
- while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST'))
779
- r = r.ofType;
780
- const kind = r?.kind;
781
- return kind === 'SCALAR' || kind === 'ENUM';
782
- })
783
- .map((f) => f.name)
784
- : [];
785
- selections.push(t.field({
786
- name: objectOutputName,
787
- selectionSet: t.selectionSet({ selections: fieldNames.map((n) => t.field({ name: n })) }),
788
- }));
789
- }
790
- if (scalarOutputs.length > 0) {
791
- selections.push(...scalarOutputs.map((o) => t.field({ name: o })));
792
- }
793
- else {
794
- selections.push(t.field({ name: 'clientMutationId' }));
795
- }
796
- const ast = createGqlMutation({
797
- operationName,
798
- mutationName,
799
- selectArgs,
800
- selections,
801
- variableDefinitions,
802
- });
803
- return { name: mutationName, ast };
804
- };
805
- export const generate = (gql, selection, typeNameOverrides, typeIndex) => {
806
- return Object.keys(gql).reduce((m, operationName) => {
807
- const defn = gql[operationName];
808
- let name;
809
- let ast;
810
- if (defn.qtype === 'mutation') {
811
- if (defn.mutationType === 'create') {
812
- ({ name, ast } = createOne({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
813
- }
814
- else if (defn.mutationType === 'patch') {
815
- ({ name, ast } = patchOne({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
816
- }
817
- else if (defn.mutationType === 'delete') {
818
- ({ name, ast } = deleteOne({ operationName, mutation: defn }, typeNameOverrides, typeIndex) ?? {});
819
- }
820
- else {
821
- ({ name, ast } = createMutation({ operationName, mutation: defn, selection }, typeNameOverrides, typeIndex) ?? {});
822
- }
823
- }
824
- else if (defn.qtype === 'getMany') {
825
- [
826
- getMany,
827
- getManyPaginatedEdges,
828
- getOrderByEnums,
829
- getFragment
830
- ].forEach(fn => {
831
- const result = fn({ operationName, query: defn });
832
- if (result?.name && result?.ast) {
833
- m[result.name] = result;
834
- }
835
- });
836
- }
837
- else if (defn.qtype === 'getOne') {
838
- // @ts-ignore
839
- ({ name, ast } = getOne({ operationName, query: defn }, typeNameOverrides) ?? {});
840
- }
841
- else {
842
- console.warn('Unknown qtype for key: ' + operationName);
843
- }
844
- if (name && ast) {
845
- m[name] = { name, ast };
846
- }
847
- return m;
848
- }, {});
849
- };
850
- export const generateGranular = (gql, model, fields) => {
851
- return Object.keys(gql).reduce((m, operationName) => {
852
- const defn = gql[operationName];
853
- const matchModel = defn.model;
854
- let name;
855
- let ast;
856
- if (defn.qtype === 'getMany') {
857
- const many = getMany({ operationName, query: defn, fields });
858
- if (many?.name && many?.ast && model === matchModel) {
859
- m[many.name] = many;
860
- }
861
- const paginatedEdges = getManyPaginatedEdges({
862
- operationName,
863
- query: defn,
864
- fields,
865
- });
866
- if (paginatedEdges?.name && paginatedEdges?.ast && model === matchModel) {
867
- m[paginatedEdges.name] = paginatedEdges;
868
- }
869
- const paginatedNodes = getManyPaginatedNodes({
870
- operationName,
871
- query: defn,
872
- fields,
873
- });
874
- if (paginatedNodes?.name && paginatedNodes?.ast && model === matchModel) {
875
- m[paginatedNodes.name] = paginatedNodes;
876
- }
877
- }
878
- else if (defn.qtype === 'getOne') {
879
- const one = getOne({ operationName, query: defn, fields });
880
- if (one?.name && one?.ast && model === matchModel) {
881
- m[one.name] = one;
882
- }
883
- }
884
- return m;
885
- }, {});
886
- };
887
- export function getSelections(query, fields = []) {
888
- const useAll = fields.length === 0;
889
- const shouldDropId = typeof query.model === 'string' && /Extension$/i.test(query.model);
890
- const mapItem = (item) => {
891
- if (typeof item === 'string') {
892
- if (shouldDropId && item === 'id')
893
- return null;
894
- if (!useAll && !fields.includes(item))
895
- return null;
896
- return t.field({ name: item });
897
- }
898
- if (typeof item === 'object' &&
899
- item !== null &&
900
- 'name' in item &&
901
- 'selection' in item &&
902
- Array.isArray(item.selection)) {
903
- if (!useAll && !fields.includes(item.name))
904
- return null;
905
- const isMany = item.qtype === 'getMany';
906
- if (isMany) {
907
- return t.field({
908
- name: item.name,
909
- args: [t.argument({ name: 'first', value: t.intValue({ value: '3' }) })],
910
- selectionSet: t.selectionSet({
911
- selections: [
912
- t.field({
913
- name: 'edges',
914
- selectionSet: t.selectionSet({
915
- selections: [
916
- t.field({ name: 'cursor' }),
917
- t.field({
918
- name: 'node',
919
- selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
920
- }),
921
- ],
922
- }),
923
- }),
924
- ],
925
- }),
926
- });
927
- }
928
- return t.field({
929
- name: item.name,
930
- selectionSet: t.selectionSet({ selections: item.selection.map((s) => mapItem(s)).filter(Boolean) }),
931
- });
932
- }
933
- return null;
934
- };
935
- return query.selection
936
- .filter((s) => !(shouldDropId && s === 'id'))
937
- .map((field) => mapItem(field))
938
- .filter((i) => Boolean(i));
939
- }