@xyd-js/gql 0.1.0-build.170

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 (76) hide show
  1. package/CHANGELOG.md +1551 -0
  2. package/LICENSE +21 -0
  3. package/README.md +3 -0
  4. package/TODO.md +8 -0
  5. package/__fixtures__/-1.opendocs.docs-nested/input.graphql +66 -0
  6. package/__fixtures__/-1.opendocs.docs-nested/output.json +554 -0
  7. package/__fixtures__/-1.opendocs.flat/input.graphql +19 -0
  8. package/__fixtures__/-1.opendocs.flat/output.json +243 -0
  9. package/__fixtures__/-1.opendocs.scopes/input.graphql +33 -0
  10. package/__fixtures__/-1.opendocs.scopes/output.json +378 -0
  11. package/__fixtures__/-1.opendocs.sidebar/input.graphql +44 -0
  12. package/__fixtures__/-1.opendocs.sort/input.graphql +92 -0
  13. package/__fixtures__/-1.opendocs.sort/output.json +1078 -0
  14. package/__fixtures__/-1.opendocs.sort+group/input.graphql +111 -0
  15. package/__fixtures__/-1.opendocs.sort+group/output.json +1114 -0
  16. package/__fixtures__/-1.opendocs.sort+group+path/input.graphql +118 -0
  17. package/__fixtures__/-1.opendocs.sort+group+path/output.json +1114 -0
  18. package/__fixtures__/-2.complex.github/input.graphql +69424 -0
  19. package/__fixtures__/-2.complex.github/output.json +269874 -0
  20. package/__fixtures__/-2.complex.livesession/input.graphql +23 -0
  21. package/__fixtures__/-2.complex.livesession/output.json +302 -0
  22. package/__fixtures__/-2.complex.monday/input.graphql +6089 -0
  23. package/__fixtures__/-2.complex.monday/output.json +1 -0
  24. package/__fixtures__/-3.array-non-null-return/input.graphql +9 -0
  25. package/__fixtures__/-3.array-non-null-return/output.json +151 -0
  26. package/__fixtures__/1.basic/input.graphql +118 -0
  27. package/__fixtures__/1.basic/output.json +630 -0
  28. package/__fixtures__/2.circular/input.graphql +17 -0
  29. package/__fixtures__/2.circular/output.json +248 -0
  30. package/__fixtures__/3.opendocs/input.graphql +27 -0
  31. package/__fixtures__/3.opendocs/output.json +338 -0
  32. package/__fixtures__/4.union/input.graphql +19 -0
  33. package/__fixtures__/4.union/output.json +344 -0
  34. package/__fixtures__/5.flat/input.graphql +27 -0
  35. package/__fixtures__/5.flat/output.json +383 -0
  36. package/__fixtures__/6.default-values/input.graphql +47 -0
  37. package/__fixtures__/6.default-values/output.json +655 -0
  38. package/__fixtures__/7.type-args/input.graphql +19 -0
  39. package/__fixtures__/7.type-args/output.json +301 -0
  40. package/__fixtures__/8.default-sort/input.graphql +60 -0
  41. package/__fixtures__/8.default-sort/output.json +1078 -0
  42. package/__tests__/gqlSchemaToReferences.test.ts +109 -0
  43. package/__tests__/utils.ts +45 -0
  44. package/declarations.d.ts +4 -0
  45. package/dist/index.d.ts +21 -0
  46. package/dist/index.js +1503 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/opendocs.graphql +56 -0
  49. package/index.ts +3 -0
  50. package/package.json +29 -0
  51. package/src/context.ts +17 -0
  52. package/src/converters/gql-arg.ts +51 -0
  53. package/src/converters/gql-enum.ts +27 -0
  54. package/src/converters/gql-field.ts +164 -0
  55. package/src/converters/gql-input.ts +34 -0
  56. package/src/converters/gql-interface.ts +35 -0
  57. package/src/converters/gql-mutation.ts +36 -0
  58. package/src/converters/gql-object.ts +83 -0
  59. package/src/converters/gql-operation.ts +128 -0
  60. package/src/converters/gql-query.ts +36 -0
  61. package/src/converters/gql-sample.ts +159 -0
  62. package/src/converters/gql-scalar.ts +16 -0
  63. package/src/converters/gql-subscription.ts +36 -0
  64. package/src/converters/gql-types.ts +195 -0
  65. package/src/converters/gql-union.ts +40 -0
  66. package/src/gql-core.ts +362 -0
  67. package/src/index.ts +3 -0
  68. package/src/opendocs.graphql +56 -0
  69. package/src/opendocs.ts +253 -0
  70. package/src/schema.ts +293 -0
  71. package/src/types.ts +103 -0
  72. package/src/utils.ts +25 -0
  73. package/tsconfig.json +18 -0
  74. package/tsup.config.ts +33 -0
  75. package/tsup.examples-config.ts +30 -0
  76. package/vitest.config.ts +21 -0
package/dist/index.js ADDED
@@ -0,0 +1,1503 @@
1
+ // src/schema.ts
2
+ import fs from "fs";
3
+ import {
4
+ buildSchema,
5
+ print as print2,
6
+ visit,
7
+ parse
8
+ } from "graphql";
9
+ import { mergeTypeDefs } from "@graphql-tools/merge";
10
+
11
+ // src/types.ts
12
+ var DEFAULT_SORT_ORDER = [
13
+ { node: "query" },
14
+ { node: "mutation" },
15
+ { node: "subscription" },
16
+ { node: "object" },
17
+ { node: "interface" },
18
+ { node: "union" },
19
+ { node: "input" },
20
+ { node: "enum" },
21
+ { node: "scalar" }
22
+ ];
23
+ var GQLOperation = class {
24
+ constructor(field) {
25
+ this.field = field;
26
+ }
27
+ get name() {
28
+ return this.field.name;
29
+ }
30
+ get description() {
31
+ return this.field.description;
32
+ }
33
+ get type() {
34
+ return this.field.type;
35
+ }
36
+ get args() {
37
+ return this.field.args;
38
+ }
39
+ get deprecationReason() {
40
+ return this.field.deprecationReason;
41
+ }
42
+ get extensions() {
43
+ return this.field.extensions;
44
+ }
45
+ get astNode() {
46
+ return this.field.astNode;
47
+ }
48
+ set __operationType(operationType) {
49
+ this._operationType = operationType;
50
+ }
51
+ };
52
+
53
+ // src/converters/gql-types.ts
54
+ import {
55
+ isIntrospectionType as isIntrospectionType2,
56
+ isSpecifiedScalarType as isSpecifiedScalarType2
57
+ } from "graphql";
58
+
59
+ // src/converters/gql-field.ts
60
+ import {
61
+ GraphQLInputObjectType as GraphQLInputObjectType3,
62
+ GraphQLUnionType as GraphQLUnionType3,
63
+ GraphQLObjectType as GraphQLObjectType3,
64
+ GraphQLInterfaceType as GraphQLInterfaceType3,
65
+ GraphQLScalarType as GraphQLScalarType3,
66
+ GraphQLNonNull as GraphQLNonNull2
67
+ } from "graphql";
68
+
69
+ // src/gql-core.ts
70
+ import {
71
+ GraphQLEnumType as GraphQLEnumType2,
72
+ GraphQLInputObjectType as GraphQLInputObjectType2,
73
+ GraphQLInterfaceType as GraphQLInterfaceType2,
74
+ GraphQLObjectType as GraphQLObjectType2,
75
+ GraphQLScalarType as GraphQLScalarType2,
76
+ GraphQLUnionType as GraphQLUnionType2,
77
+ isIntrospectionType,
78
+ isSpecifiedScalarType,
79
+ GraphQLNonNull,
80
+ GraphQLList
81
+ } from "graphql";
82
+ import GithubSlugger from "github-slugger";
83
+ import {
84
+ ReferenceCategory,
85
+ ReferenceType
86
+ } from "@xyd-js/uniform";
87
+
88
+ // src/opendocs.ts
89
+ import {
90
+ GraphQLEnumType,
91
+ GraphQLInputObjectType,
92
+ GraphQLInterfaceType,
93
+ GraphQLObjectType,
94
+ GraphQLScalarType,
95
+ GraphQLUnionType
96
+ } from "graphql";
97
+ var OPEN_DOCS_SCHEMA_DIRECTIVE_NAME = "docs";
98
+ var OPEN_DOCS_DIRECTIVE_NAME = "doc";
99
+ function openDocsExtensionsToOptions(schema) {
100
+ const options = {};
101
+ for (const extension of schema.extensionASTNodes || []) {
102
+ if (extension.kind === "SchemaExtension") {
103
+ for (const directive of extension.directives || []) {
104
+ if (directive.name.value === OPEN_DOCS_SCHEMA_DIRECTIVE_NAME) {
105
+ for (const arg of directive.arguments || []) {
106
+ if (arg.name.value === "flattenTypes" && arg.value.kind === "BooleanValue") {
107
+ if (arg.value.value === true) {
108
+ options.flat = true;
109
+ } else if (arg.value.value === false) {
110
+ options.flat = false;
111
+ }
112
+ } else if (arg.name.value === "sort" && arg.value.kind === "ListValue") {
113
+ const sortItems = [];
114
+ for (const item of arg.value.values) {
115
+ if (item.kind === "ObjectValue") {
116
+ const sortItem = {};
117
+ for (const field of item.fields) {
118
+ if (field.name.value === "node" && field.value.kind === "StringValue") {
119
+ sortItem.node = field.value.value;
120
+ } else if (field.name.value === "group" && field.value.kind === "ListValue") {
121
+ sortItem.group = field.value.values.filter((v) => v.kind === "StringValue").map((v) => v.value);
122
+ } else if (field.name.value === "stack" && field.value.kind === "IntValue") {
123
+ sortItem.stack = parseInt(field.value.value);
124
+ }
125
+ }
126
+ sortItems.push(sortItem);
127
+ }
128
+ }
129
+ if (!options.sort) {
130
+ options.sort = {};
131
+ }
132
+ options.sort.sort = sortItems;
133
+ } else if (arg.name.value === "sortStack" && arg.value.kind === "ListValue") {
134
+ const sortStacks = [];
135
+ for (const item of arg.value.values) {
136
+ if (item.kind === "ListValue") {
137
+ const stack = item.values.filter((v) => v.kind === "StringValue").map((v) => v.value);
138
+ sortStacks.push(stack);
139
+ }
140
+ }
141
+ if (!options.sort) {
142
+ options.sort = {};
143
+ }
144
+ options.sort.sortStack = sortStacks;
145
+ } else if (arg.name.value === "route" && arg.value.kind === "StringValue") {
146
+ options.route = arg.value.value;
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
152
+ }
153
+ return options;
154
+ }
155
+ function openDocsToGroup(ctx, odGqlNode) {
156
+ var _a, _b;
157
+ let groups = [];
158
+ const metadata = (ctx == null ? void 0 : ctx.schema).__metadata;
159
+ if (metadata == null ? void 0 : metadata.rootGroups) {
160
+ groups = [...metadata.rootGroups];
161
+ }
162
+ let directiveGroups = false;
163
+ if ((ctx == null ? void 0 : ctx.schema) && "name" in odGqlNode) {
164
+ const metadata2 = ctx.schema.__metadata;
165
+ if (metadata2 == null ? void 0 : metadata2.fields) {
166
+ if ("_operationType" in odGqlNode) {
167
+ let fieldKey = "";
168
+ switch (odGqlNode._operationType) {
169
+ case "query": {
170
+ fieldKey = `Query.${odGqlNode.name}`;
171
+ break;
172
+ }
173
+ case "mutation": {
174
+ fieldKey = `Mutation.${odGqlNode.name}`;
175
+ break;
176
+ }
177
+ case "subscription": {
178
+ fieldKey = `Subscription.${odGqlNode.name}`;
179
+ break;
180
+ }
181
+ }
182
+ const fieldMetadata = metadata2.fields.get(fieldKey);
183
+ if (fieldMetadata == null ? void 0 : fieldMetadata.groups) {
184
+ directiveGroups = true;
185
+ groups.push(...fieldMetadata.groups);
186
+ }
187
+ }
188
+ }
189
+ }
190
+ if (!directiveGroups && ((_a = odGqlNode.astNode) == null ? void 0 : _a.directives)) {
191
+ for (const directive of odGqlNode.astNode.directives) {
192
+ switch (directive.name.value) {
193
+ case OPEN_DOCS_DIRECTIVE_NAME: {
194
+ const groupArg = (_b = directive.arguments) == null ? void 0 : _b.find((arg) => arg.name.value === "group");
195
+ if ((groupArg == null ? void 0 : groupArg.value.kind) === "ListValue") {
196
+ directiveGroups = true;
197
+ groups.push(
198
+ ...groupArg.value.values.filter((v) => v.kind === "StringValue").map((v) => v.value)
199
+ );
200
+ }
201
+ break;
202
+ }
203
+ }
204
+ }
205
+ }
206
+ if (!directiveGroups) {
207
+ if (odGqlNode instanceof GraphQLObjectType) {
208
+ groups.push("Objects");
209
+ } else if (odGqlNode instanceof GraphQLInterfaceType) {
210
+ groups.push("Interfaces");
211
+ } else if (odGqlNode instanceof GraphQLUnionType) {
212
+ groups.push("Unions");
213
+ } else if (odGqlNode instanceof GraphQLEnumType) {
214
+ groups.push("Enums");
215
+ } else if (odGqlNode instanceof GraphQLInputObjectType) {
216
+ groups.push("Inputs");
217
+ } else if (odGqlNode instanceof GraphQLScalarType) {
218
+ groups.push("Scalars");
219
+ } else if (odGqlNode instanceof GQLOperation) {
220
+ switch (odGqlNode._operationType) {
221
+ case "query": {
222
+ groups.push("Queries");
223
+ break;
224
+ }
225
+ case "mutation": {
226
+ groups.push("Mutations");
227
+ break;
228
+ }
229
+ case "subscription": {
230
+ groups.push("Subscriptions");
231
+ break;
232
+ }
233
+ }
234
+ }
235
+ }
236
+ return groups;
237
+ }
238
+ function openDocsCanonical(ctx, gqlType) {
239
+ var _a, _b, _c;
240
+ let path = "";
241
+ if ("astNode" in gqlType && ((_a = gqlType.astNode) == null ? void 0 : _a.kind) === "FieldDefinition" && (ctx == null ? void 0 : ctx.schema)) {
242
+ const metadata = ctx.schema.__metadata;
243
+ if ((metadata == null ? void 0 : metadata.fields) && "name" in gqlType) {
244
+ if ("_operationType" in gqlType) {
245
+ let fieldKey = "";
246
+ switch (gqlType._operationType) {
247
+ case "query": {
248
+ fieldKey = `Query.${gqlType.name}`;
249
+ break;
250
+ }
251
+ case "mutation": {
252
+ fieldKey = `Mutation.${gqlType.name}`;
253
+ break;
254
+ }
255
+ case "subscription": {
256
+ fieldKey = `Subscription.${gqlType.name}`;
257
+ break;
258
+ }
259
+ }
260
+ const fieldMetadata = metadata.fields.get(fieldKey);
261
+ if (fieldMetadata == null ? void 0 : fieldMetadata.path) {
262
+ path = fieldMetadata.path;
263
+ }
264
+ }
265
+ }
266
+ }
267
+ if (!path && ((_b = gqlType.astNode) == null ? void 0 : _b.directives)) {
268
+ for (const directive of gqlType.astNode.directives) {
269
+ if (directive.name.value === OPEN_DOCS_DIRECTIVE_NAME) {
270
+ const pathArg = (_c = directive.arguments) == null ? void 0 : _c.find((arg) => arg.name.value === "path");
271
+ if ((pathArg == null ? void 0 : pathArg.value.kind) === "StringValue") {
272
+ path = pathArg.value.value;
273
+ }
274
+ }
275
+ }
276
+ }
277
+ return path;
278
+ }
279
+
280
+ // src/gql-core.ts
281
+ function extractScopesFromDocDirective(ctx, gqlType) {
282
+ var _a, _b, _c, _d;
283
+ const scopes = [];
284
+ const schema = ctx == null ? void 0 : ctx.schema;
285
+ if ((_a = gqlType.astNode) == null ? void 0 : _a.directives) {
286
+ for (const directive of gqlType.astNode.directives) {
287
+ if (directive.name.value === "doc") {
288
+ const scopesArg = (_b = directive.arguments) == null ? void 0 : _b.find((arg) => arg.name.value === "scopes");
289
+ if ((scopesArg == null ? void 0 : scopesArg.value.kind) === "ListValue") {
290
+ for (const scopeValue of scopesArg.value.values) {
291
+ if (scopeValue.kind === "EnumValue") {
292
+ let enumType;
293
+ if (gqlType instanceof GraphQLEnumType2) {
294
+ enumType = gqlType;
295
+ } else if (schema) {
296
+ const type = schema.getType("OpenDocsScope");
297
+ if (type instanceof GraphQLEnumType2) {
298
+ enumType = type;
299
+ }
300
+ }
301
+ if (enumType) {
302
+ const enumValue = enumType.getValue(scopeValue.value);
303
+ if ((_c = enumValue == null ? void 0 : enumValue.astNode) == null ? void 0 : _c.directives) {
304
+ for (const enumDirective of enumValue.astNode.directives) {
305
+ if (enumDirective.name.value === "scope") {
306
+ const valueArg = (_d = enumDirective.arguments) == null ? void 0 : _d.find((arg) => arg.name.value === "value");
307
+ if ((valueArg == null ? void 0 : valueArg.value.kind) === "StringValue") {
308
+ scopes.push(valueArg.value.value);
309
+ }
310
+ }
311
+ }
312
+ }
313
+ }
314
+ } else if (scopeValue.kind === "StringValue") {
315
+ scopes.push(scopeValue.value);
316
+ }
317
+ }
318
+ }
319
+ }
320
+ }
321
+ }
322
+ return scopes;
323
+ }
324
+ function uniformify(ctx, gqlType, definitions, examples) {
325
+ let canonicalPrefix = "";
326
+ let graphqlTypeShort = "";
327
+ let refType = void 0;
328
+ if (gqlType instanceof GraphQLScalarType2) {
329
+ canonicalPrefix = "scalars";
330
+ graphqlTypeShort = "scalar";
331
+ refType = ReferenceType.GRAPHQL_SCALAR;
332
+ } else if (gqlType instanceof GraphQLObjectType2) {
333
+ canonicalPrefix = "objects";
334
+ graphqlTypeShort = "object";
335
+ refType = ReferenceType.GRAPHQL_OBJECT;
336
+ } else if (gqlType instanceof GraphQLInterfaceType2) {
337
+ canonicalPrefix = "interfaces";
338
+ graphqlTypeShort = "interface";
339
+ refType = ReferenceType.GRAPHQL_INTERFACE;
340
+ } else if (gqlType instanceof GraphQLUnionType2) {
341
+ canonicalPrefix = "unions";
342
+ graphqlTypeShort = "union";
343
+ refType = ReferenceType.GRAPHQL_UNION;
344
+ } else if (gqlType instanceof GraphQLEnumType2) {
345
+ canonicalPrefix = "enums";
346
+ graphqlTypeShort = "enum";
347
+ refType = ReferenceType.GRAPHQL_ENUM;
348
+ } else if (gqlType instanceof GraphQLInputObjectType2) {
349
+ canonicalPrefix = "inputs";
350
+ graphqlTypeShort = "input";
351
+ refType = ReferenceType.GRAPHQL_INPUT;
352
+ } else if (gqlType instanceof GQLOperation) {
353
+ switch (gqlType._operationType) {
354
+ case "query": {
355
+ canonicalPrefix = "queries";
356
+ graphqlTypeShort = "query";
357
+ refType = ReferenceType.GRAPHQL_QUERY;
358
+ break;
359
+ }
360
+ case "mutation": {
361
+ canonicalPrefix = "mutations";
362
+ graphqlTypeShort = "mutation";
363
+ refType = ReferenceType.GRAPHQL_MUTATION;
364
+ break;
365
+ }
366
+ case "subscription": {
367
+ canonicalPrefix = "subscriptions";
368
+ graphqlTypeShort = "subscription";
369
+ refType = ReferenceType.GRAPHQL_SUBSCRIPTION;
370
+ break;
371
+ }
372
+ }
373
+ } else {
374
+ const info = gqlFieldTypeInfo(gqlType);
375
+ if ((info == null ? void 0 : info.typeFlat) && !isBuiltInType(info == null ? void 0 : info.typeFlat)) {
376
+ if (info == null ? void 0 : info.typeFlat) {
377
+ return uniformify(ctx, info.typeFlat, definitions, examples);
378
+ }
379
+ }
380
+ }
381
+ const slugger = new GithubSlugger();
382
+ const slug = gqlType.name;
383
+ const odCanonical = openDocsCanonical(ctx, gqlType);
384
+ let canonical = "";
385
+ if (odCanonical) {
386
+ canonical = odCanonical;
387
+ } else if (canonicalPrefix) {
388
+ canonical = [canonicalPrefix, slug].join("/");
389
+ }
390
+ const scopes = extractScopesFromDocDirective(ctx, gqlType) || [];
391
+ return {
392
+ title: gqlType.name,
393
+ description: gqlType.description || "",
394
+ canonical,
395
+ category: ReferenceCategory.GRAPHQL,
396
+ type: refType,
397
+ context: {
398
+ graphqlTypeShort,
399
+ graphqlName: gqlType.name,
400
+ group: openDocsToGroup(ctx, gqlType),
401
+ scopes
402
+ },
403
+ examples: {
404
+ groups: examples || []
405
+ },
406
+ definitions
407
+ };
408
+ }
409
+ function propsUniformify(ctx, field, properties, meta) {
410
+ var _a;
411
+ const fieldInfo = gqlFieldTypeInfo(field);
412
+ const objRef = uniformify(ctx, field, [], []);
413
+ const builtInType = (fieldInfo == null ? void 0 : fieldInfo.typeFlat) ? isBuiltInType(fieldInfo == null ? void 0 : fieldInfo.typeFlat) : void 0;
414
+ return {
415
+ name: field.name,
416
+ type: field.type.toJSON(),
417
+ description: field.description || "",
418
+ context: {
419
+ graphqlName: field.name,
420
+ graphqlTypeFlat: ((_a = fieldInfo.typeFlat) == null ? void 0 : _a.toJSON()) || "",
421
+ graphqlBuiltInType: builtInType
422
+ },
423
+ properties: properties || [],
424
+ meta: [
425
+ ...gqlFieldToUniformMeta(field),
426
+ ...meta || []
427
+ ],
428
+ symbolDef: {
429
+ canonical: objRef == null ? void 0 : objRef.canonical
430
+ }
431
+ };
432
+ }
433
+ function gqlObjectPropsUniformify(ctx, obj, meta) {
434
+ var _a, _b;
435
+ const objRef = uniformify(ctx, obj, [], []);
436
+ const inputFields = (_a = obj.getFields) == null ? void 0 : _a.call(obj);
437
+ const nestedProps = [];
438
+ const nestedDefinitionProperty = {
439
+ name: obj.name,
440
+ type: obj.toJSON(),
441
+ description: obj.description || "",
442
+ context: objRef.context,
443
+ properties: nestedProps,
444
+ meta: [
445
+ ...meta || []
446
+ ],
447
+ symbolDef: {
448
+ canonical: objRef.canonical
449
+ }
450
+ };
451
+ if (!((_b = ctx == null ? void 0 : ctx.config) == null ? void 0 : _b.flatArg)) {
452
+ for (const [name, inputField] of Object.entries(inputFields)) {
453
+ const prop = gqlFieldToUniformDefinitionProperty(
454
+ ctx,
455
+ inputField
456
+ );
457
+ if (prop) {
458
+ nestedProps.push(prop);
459
+ }
460
+ }
461
+ }
462
+ return nestedDefinitionProperty;
463
+ }
464
+ function gqlFieldToUniformMeta(field) {
465
+ var _a;
466
+ const meta = [];
467
+ if (isNonNullField(field.type) || isListOfNonNullItems(field.type)) {
468
+ meta.push({
469
+ name: "required",
470
+ value: "true"
471
+ });
472
+ }
473
+ if ((_a = field.astNode) == null ? void 0 : _a.directives) {
474
+ for (const directive of field.astNode.directives) {
475
+ if (directive.name.value === "deprecated") {
476
+ let foundDeprecatedReason = false;
477
+ for (const arg of directive.arguments || []) {
478
+ if (arg.name.value === "reason") {
479
+ foundDeprecatedReason = true;
480
+ meta.push({
481
+ name: "deprecated",
482
+ value: arg.value.kind === "StringValue" ? arg.value.value : "true"
483
+ });
484
+ }
485
+ }
486
+ if (!foundDeprecatedReason) {
487
+ meta.push({
488
+ name: "deprecated",
489
+ value: "true"
490
+ });
491
+ }
492
+ }
493
+ }
494
+ }
495
+ if ("defaultValue" in field && field.defaultValue !== void 0) {
496
+ meta.push({
497
+ name: "defaults",
498
+ value: field.defaultValue
499
+ });
500
+ }
501
+ return meta;
502
+ }
503
+ function gqlFieldTypeInfo(field) {
504
+ const getTypeInfo = (type) => {
505
+ if (!type) return void 0;
506
+ if (type instanceof GraphQLNonNull) {
507
+ return getTypeInfo(type.ofType);
508
+ }
509
+ if (type instanceof GraphQLList) {
510
+ return getTypeInfo(type.ofType);
511
+ }
512
+ return type;
513
+ };
514
+ const graphqlTypeFlat = getTypeInfo(field.type);
515
+ return {
516
+ typeFlat: graphqlTypeFlat
517
+ };
518
+ }
519
+ function isBuiltInType(gqlType) {
520
+ return isSpecifiedScalarType(gqlType) || isIntrospectionType(gqlType);
521
+ }
522
+ function isNonNullField(type) {
523
+ return type.constructor.name === "GraphQLNonNull";
524
+ }
525
+ function isListOfNonNullItems(type) {
526
+ return "ofType" in type && type.constructor.name === "GraphQLList" && type.ofType.constructor.name === "GraphQLNonNull";
527
+ }
528
+
529
+ // src/converters/gql-union.ts
530
+ function gqlUnionToUniformRef(ctx, unionType) {
531
+ const properties = gqlUnionToUniformDefinitionProperties(ctx, unionType);
532
+ const definitions = [
533
+ {
534
+ title: "Possible types",
535
+ properties
536
+ }
537
+ ];
538
+ return uniformify(
539
+ ctx,
540
+ unionType,
541
+ definitions,
542
+ []
543
+ );
544
+ }
545
+ function gqlUnionToUniformDefinitionProperties(ctx, unionType) {
546
+ return unionType.getTypes().map((type) => {
547
+ if (type.constructor.name === "GraphQLObjectType") {
548
+ return gqlObjectToUniformDefinitionProperty(
549
+ ctx,
550
+ type
551
+ );
552
+ }
553
+ return [];
554
+ }).flat();
555
+ }
556
+
557
+ // src/converters/gql-field.ts
558
+ function gqlFieldToUniformDefinitionProperty(ctx, field) {
559
+ return GQLFieldConverter.convert(ctx, field);
560
+ }
561
+ var GQLFieldConverter = class _GQLFieldConverter {
562
+ constructor(ctx) {
563
+ this.ctx = ctx;
564
+ }
565
+ static convert(ctx, field) {
566
+ return new _GQLFieldConverter(ctx).convert(field);
567
+ }
568
+ convert(field, parent) {
569
+ var _a, _b, _c, _d, _e;
570
+ if (((_a = this.ctx.globalOptions) == null ? void 0 : _a.flat) && (((_b = this.ctx.config) == null ? void 0 : _b.flatReturn) || ((_c = this.ctx.config) == null ? void 0 : _c.flat))) {
571
+ const info = gqlFieldTypeInfo(field);
572
+ const meta = [];
573
+ const props = propsUniformify(this.ctx, field, [], meta);
574
+ if ((_d = this.ctx.config) == null ? void 0 : _d.flatReturn) {
575
+ return {
576
+ ...props,
577
+ name: ""
578
+ };
579
+ }
580
+ if ((_e = this.ctx.config) == null ? void 0 : _e.flat) {
581
+ return props;
582
+ }
583
+ }
584
+ const fieldInfo = gqlFieldTypeInfo(field);
585
+ const graphqlTypeFlat = fieldInfo == null ? void 0 : fieldInfo.typeFlat;
586
+ let properties;
587
+ switch (graphqlTypeFlat == null ? void 0 : graphqlTypeFlat.constructor) {
588
+ case GraphQLObjectType3:
589
+ case GraphQLInputObjectType3:
590
+ case GraphQLInterfaceType3: {
591
+ properties = this.nestedProperties(graphqlTypeFlat);
592
+ break;
593
+ }
594
+ case GraphQLScalarType3:
595
+ case GraphQLNonNull2: {
596
+ properties = this.definitionPropsFromNestedObj(field) || [];
597
+ break;
598
+ }
599
+ case GraphQLUnionType3: {
600
+ properties = gqlUnionToUniformDefinitionProperties(this.ctx, graphqlTypeFlat);
601
+ break;
602
+ }
603
+ }
604
+ const resp = propsUniformify(
605
+ this.ctx,
606
+ field,
607
+ properties
608
+ );
609
+ if (graphqlTypeFlat) {
610
+ const nestedType = graphqlTypeFlat;
611
+ nestedType.__definitionProperties = resp.properties;
612
+ }
613
+ if (parent) {
614
+ parent.__definitionProperties = resp.properties;
615
+ }
616
+ return resp;
617
+ }
618
+ // TODO: fix any + another more safety solution?
619
+ // definitionPropsFromNestedObj converts graphql nested obj into xyd 'uniform' definition properties
620
+ definitionPropsFromNestedObj(obj) {
621
+ if (!obj) {
622
+ return null;
623
+ }
624
+ if (obj.getFields) {
625
+ return this.nestedProperties(obj);
626
+ }
627
+ if (obj.ofType) {
628
+ return this.definitionPropsFromNestedObj(obj.ofType);
629
+ }
630
+ if (obj.type) {
631
+ return this.definitionPropsFromNestedObj(obj.type);
632
+ }
633
+ return null;
634
+ }
635
+ // deepFieldMap iterates over GraphQL field (field or input fields) maps
636
+ deepFieldMap(fieldsMap, parent) {
637
+ const properties = [];
638
+ for (const [name, field] of Object.entries(fieldsMap)) {
639
+ const prop = this.convert(
640
+ field,
641
+ parent
642
+ );
643
+ if (prop) {
644
+ properties.push(prop);
645
+ }
646
+ }
647
+ return properties;
648
+ }
649
+ // nestedProperties get fields from object or input object types and iterates over them
650
+ nestedProperties(objectType) {
651
+ var _a;
652
+ if (this.ctx.processedTypes.has(objectType)) {
653
+ if (objectType == null ? void 0 : objectType.__definitionProperties) {
654
+ return objectType.__definitionProperties || [];
655
+ }
656
+ return [];
657
+ }
658
+ this.ctx.processedTypes.add(objectType);
659
+ const nestedFields = (_a = objectType == null ? void 0 : objectType.getFields) == null ? void 0 : _a.call(objectType);
660
+ const result = this.deepFieldMap(nestedFields, objectType);
661
+ return result;
662
+ }
663
+ };
664
+
665
+ // src/converters/gql-arg.ts
666
+ import { GraphQLInputObjectType as GraphQLInputObjectType4 } from "graphql";
667
+
668
+ // src/converters/gql-input.ts
669
+ function gqlInputToUniformRef(ctx, gqlType) {
670
+ const prop = gqlInputToUniformDefinitionProperty(
671
+ ctx,
672
+ gqlType
673
+ );
674
+ return uniformify(
675
+ ctx,
676
+ gqlType,
677
+ [
678
+ {
679
+ title: "Fields",
680
+ properties: prop.properties || []
681
+ }
682
+ ],
683
+ []
684
+ );
685
+ }
686
+ function gqlInputToUniformDefinitionProperty(ctx, obj) {
687
+ return gqlObjectPropsUniformify(ctx, obj);
688
+ }
689
+
690
+ // src/converters/gql-arg.ts
691
+ function gqlArgToUniformDefinitionProperty(ctx, args) {
692
+ const resp = [];
693
+ args.forEach((arg) => {
694
+ const fieldInfo = gqlFieldTypeInfo(arg);
695
+ if (!fieldInfo.typeFlat) {
696
+ console.error("gqlArgToUniformDefinitionProperty: no typeFlat for", arg.name);
697
+ return;
698
+ }
699
+ const flatType = fieldInfo.typeFlat;
700
+ if (flatType instanceof GraphQLInputObjectType4) {
701
+ const inputObj = flatType;
702
+ const meta = gqlFieldToUniformMeta(arg);
703
+ const defProperty = gqlInputToUniformDefinitionProperty(
704
+ ctx,
705
+ inputObj
706
+ );
707
+ resp.push({
708
+ ...defProperty,
709
+ type: arg.type.toJSON(),
710
+ name: arg.name,
711
+ description: arg.description || "",
712
+ meta: [
713
+ ...defProperty.meta || [],
714
+ ...meta || []
715
+ ]
716
+ });
717
+ } else {
718
+ const defProperty = propsUniformify(ctx, arg);
719
+ resp.push(defProperty);
720
+ }
721
+ });
722
+ return resp;
723
+ }
724
+
725
+ // src/converters/gql-object.ts
726
+ function gqlObjectToUniformRef(ctx, gqlType) {
727
+ var _a;
728
+ const definitions = [];
729
+ const graphqlFields = [];
730
+ const variants = [];
731
+ const argumentDefinition = {
732
+ title: "Arguments",
733
+ properties: [],
734
+ variants,
735
+ meta: [
736
+ {
737
+ name: "type",
738
+ value: "arguments"
739
+ }
740
+ ]
741
+ };
742
+ for (const [name, field] of Object.entries(gqlType.getFields())) {
743
+ if (!((_a = field == null ? void 0 : field.args) == null ? void 0 : _a.length)) {
744
+ continue;
745
+ }
746
+ const args = gqlArgToUniformDefinitionProperty(ctx, field.args);
747
+ variants.push({
748
+ title: "",
749
+ properties: args,
750
+ meta: [
751
+ {
752
+ name: "symbolName",
753
+ value: name
754
+ }
755
+ ]
756
+ });
757
+ }
758
+ definitions.push(argumentDefinition);
759
+ for (const [name, field] of Object.entries(gqlType.getFields())) {
760
+ const prop = gqlFieldToUniformDefinitionProperty(ctx, field);
761
+ graphqlFields.push(prop);
762
+ }
763
+ definitions.push({
764
+ title: "Fields",
765
+ properties: graphqlFields,
766
+ meta: [
767
+ {
768
+ name: "type",
769
+ value: "fields"
770
+ }
771
+ ]
772
+ });
773
+ return uniformify(
774
+ ctx,
775
+ gqlType,
776
+ definitions,
777
+ []
778
+ );
779
+ }
780
+ function gqlObjectToUniformDefinitionProperty(ctx, obj) {
781
+ return gqlObjectPropsUniformify(ctx, obj);
782
+ }
783
+
784
+ // src/converters/gql-enum.ts
785
+ function gqlEnumToUniformRef(ctx, gqlType) {
786
+ const props = gqlType.getValues().map((value) => ({
787
+ name: value.name,
788
+ type: "string",
789
+ // TODO: other types?
790
+ description: value.description || ""
791
+ }));
792
+ return uniformify(
793
+ ctx,
794
+ gqlType,
795
+ [
796
+ {
797
+ title: "Valid values",
798
+ properties: props
799
+ }
800
+ ],
801
+ []
802
+ );
803
+ }
804
+
805
+ // src/converters/gql-scalar.ts
806
+ function gqlScalarToUniformRef(ctx, gqlType) {
807
+ return uniformify(
808
+ ctx,
809
+ gqlType,
810
+ [],
811
+ []
812
+ );
813
+ }
814
+
815
+ // src/converters/gql-interface.ts
816
+ function gqlInterfaceToUniformRef(ctx, interfaceType) {
817
+ const properties = gqlInterfaceToUniformDefinitionProperties(ctx, interfaceType);
818
+ const definitions = [
819
+ {
820
+ title: "Fields",
821
+ properties
822
+ }
823
+ ];
824
+ return uniformify(
825
+ ctx,
826
+ interfaceType,
827
+ definitions,
828
+ []
829
+ );
830
+ }
831
+ function gqlInterfaceToUniformDefinitionProperties(ctx, interfaceType) {
832
+ return Object.values(interfaceType.getFields()).map((field) => {
833
+ return gqlFieldToUniformDefinitionProperty(
834
+ ctx,
835
+ field
836
+ );
837
+ });
838
+ }
839
+
840
+ // src/context.ts
841
+ var Context = class {
842
+ constructor(processedTypes = /* @__PURE__ */ new Set(), globalOptions, config, schema) {
843
+ this.processedTypes = processedTypes;
844
+ this.globalOptions = globalOptions;
845
+ this.config = config;
846
+ this.schema = schema;
847
+ }
848
+ };
849
+
850
+ // src/converters/gql-types.ts
851
+ function graphqlTypesToUniformReferences(schema, options) {
852
+ var _a, _b, _c, _d, _e, _f;
853
+ const references = [];
854
+ const typeMap = schema.getTypeMap();
855
+ const sharedProcessedTypes = /* @__PURE__ */ new Set();
856
+ for (const gqlType of Object.values(typeMap)) {
857
+ const builtInType = isSpecifiedScalarType2(gqlType) || isIntrospectionType2(gqlType) || gqlType.name === "Mutation" || gqlType.name === "Query" || gqlType.name === "Subscription" || isInternalOpenDocsType(gqlType);
858
+ if (builtInType) {
859
+ continue;
860
+ }
861
+ let ref = null;
862
+ let flat = false;
863
+ if (options == null ? void 0 : options.flat) {
864
+ flat = true;
865
+ }
866
+ switch (gqlType.constructor.name) {
867
+ case "GraphQLObjectType": {
868
+ const type = gqlType;
869
+ const regionKey = `object.${type.name}`;
870
+ if (!(options == null ? void 0 : options.regions) || !((_a = options == null ? void 0 : options.regions) == null ? void 0 : _a.length) || options.regions.includes(regionKey)) {
871
+ ref = gqlObjectToUniformRef(
872
+ new Context(
873
+ sharedProcessedTypes,
874
+ options,
875
+ {
876
+ flat
877
+ },
878
+ schema
879
+ ),
880
+ type
881
+ );
882
+ }
883
+ break;
884
+ }
885
+ case "GraphQLInputObjectType": {
886
+ const type = gqlType;
887
+ const regionKey = `input.${type.name}`;
888
+ if (!(options == null ? void 0 : options.regions) || !((_b = options == null ? void 0 : options.regions) == null ? void 0 : _b.length) || options.regions.includes(regionKey)) {
889
+ ref = gqlInputToUniformRef(
890
+ new Context(
891
+ sharedProcessedTypes,
892
+ options,
893
+ {
894
+ flat
895
+ },
896
+ schema
897
+ ),
898
+ type
899
+ );
900
+ }
901
+ break;
902
+ }
903
+ case "GraphQLEnumType": {
904
+ const type = gqlType;
905
+ const regionKey = `enum.${type.name}`;
906
+ if (!(options == null ? void 0 : options.regions) || !((_c = options == null ? void 0 : options.regions) == null ? void 0 : _c.length) || options.regions.includes(regionKey)) {
907
+ ref = gqlEnumToUniformRef(
908
+ new Context(
909
+ /* @__PURE__ */ new Set(),
910
+ options,
911
+ {},
912
+ schema
913
+ ),
914
+ type
915
+ );
916
+ }
917
+ break;
918
+ }
919
+ case "GraphQLScalarType": {
920
+ const type = gqlType;
921
+ const regionKey = `scalar.${type.name}`;
922
+ if (!(options == null ? void 0 : options.regions) || !((_d = options == null ? void 0 : options.regions) == null ? void 0 : _d.length) || options.regions.includes(regionKey)) {
923
+ ref = gqlScalarToUniformRef(
924
+ new Context(
925
+ /* @__PURE__ */ new Set(),
926
+ options,
927
+ {},
928
+ schema
929
+ ),
930
+ type
931
+ );
932
+ }
933
+ break;
934
+ }
935
+ case "GraphQLUnionType": {
936
+ const type = gqlType;
937
+ const regionKey = `union.${type.name}`;
938
+ if (!(options == null ? void 0 : options.regions) || !((_e = options == null ? void 0 : options.regions) == null ? void 0 : _e.length) || options.regions.includes(regionKey)) {
939
+ ref = gqlUnionToUniformRef(
940
+ new Context(
941
+ sharedProcessedTypes,
942
+ options,
943
+ {
944
+ flat,
945
+ flatArg: flat || false
946
+ },
947
+ schema
948
+ ),
949
+ type
950
+ );
951
+ }
952
+ break;
953
+ }
954
+ case "GraphQLInterfaceType": {
955
+ const type = gqlType;
956
+ const regionKey = `interface.${type.name}`;
957
+ if (!(options == null ? void 0 : options.regions) || !((_f = options == null ? void 0 : options.regions) == null ? void 0 : _f.length) || options.regions.includes(regionKey)) {
958
+ ref = gqlInterfaceToUniformRef(
959
+ new Context(
960
+ /* @__PURE__ */ new Set(),
961
+ options,
962
+ {
963
+ flat
964
+ },
965
+ schema
966
+ ),
967
+ type
968
+ );
969
+ }
970
+ break;
971
+ }
972
+ default: {
973
+ console.debug(`Unsupported type: ${gqlType.constructor.name}`);
974
+ }
975
+ }
976
+ if (ref) {
977
+ references.push(ref);
978
+ }
979
+ }
980
+ return references;
981
+ }
982
+ function isInternalOpenDocsType(type) {
983
+ const internalTypes = [
984
+ "OpenDocsScope",
985
+ "OpenDocsSidebarItemType",
986
+ "OpenDocsPage",
987
+ "OpenDocsExampleInput",
988
+ "OpenDocsSortInput",
989
+ "OpenDocsSortStackInput"
990
+ ];
991
+ return internalTypes.includes(type.name);
992
+ }
993
+
994
+ // src/converters/gql-query.ts
995
+ import { OperationTypeNode as OperationTypeNode3 } from "graphql";
996
+ import { ReferenceType as ReferenceType4 } from "@xyd-js/uniform";
997
+
998
+ // src/utils.ts
999
+ function filterFieldsByRegions(fields, prefix, regions) {
1000
+ if (!regions || regions.length === 0) {
1001
+ return fields;
1002
+ }
1003
+ const filteredFields = {};
1004
+ for (const [fieldName, field] of Object.entries(fields)) {
1005
+ const regionKey = `${prefix}.${fieldName}`;
1006
+ if (regions.some((region) => region === regionKey)) {
1007
+ filteredFields[fieldName] = field;
1008
+ }
1009
+ }
1010
+ return filteredFields;
1011
+ }
1012
+
1013
+ // src/converters/gql-operation.ts
1014
+ import { OperationTypeNode as OperationTypeNode2 } from "graphql";
1015
+ import { ReferenceType as ReferenceType3 } from "@xyd-js/uniform";
1016
+
1017
+ // src/converters/gql-sample.ts
1018
+ import {
1019
+ Kind,
1020
+ print,
1021
+ OperationTypeNode
1022
+ } from "graphql";
1023
+ import {
1024
+ ReferenceType as ReferenceType2
1025
+ } from "@xyd-js/uniform";
1026
+ var operationTypeToTypeNode = {
1027
+ [ReferenceType2.GRAPHQL_QUERY]: OperationTypeNode.QUERY,
1028
+ [ReferenceType2.GRAPHQL_MUTATION]: OperationTypeNode.MUTATION,
1029
+ [ReferenceType2.GRAPHQL_SUBSCRIPTION]: OperationTypeNode.SUBSCRIPTION
1030
+ };
1031
+ function simpleGraphqlExample(operationType, operationName, args, returns) {
1032
+ const requiredArgs = args.filter((arg) => {
1033
+ var _a;
1034
+ return (_a = arg.meta) == null ? void 0 : _a.some((m) => m.name === "required" && m.value === "true");
1035
+ });
1036
+ const selectedArgs = requiredArgs.length > 0 ? requiredArgs : args.slice(0, 1);
1037
+ let hasArgVars = false;
1038
+ const argDefaults = selectedArgs.reduce((acc, arg) => {
1039
+ var _a, _b, _c;
1040
+ if (operationType != ReferenceType2.GRAPHQL_QUERY) {
1041
+ return acc;
1042
+ }
1043
+ let defaultValue = "";
1044
+ const getDefaultValue = (_b = (_a = arg.meta) == null ? void 0 : _a.find((m) => m.name === "defaults")) == null ? void 0 : _b.value;
1045
+ if (getDefaultValue && typeof getDefaultValue !== "string") {
1046
+ defaultValue = JSON.stringify(getDefaultValue);
1047
+ }
1048
+ let sampleValue = void 0;
1049
+ switch ((_c = arg.context) == null ? void 0 : _c.graphqlTypeFlat) {
1050
+ case "String":
1051
+ sampleValue = { kind: Kind.STRING, value: defaultValue || `example-${arg.name}` };
1052
+ break;
1053
+ case "Int":
1054
+ case "Float":
1055
+ sampleValue = { kind: Kind.INT, value: defaultValue || "0" };
1056
+ break;
1057
+ case "Boolean":
1058
+ sampleValue = { kind: Kind.BOOLEAN, value: defaultValue === "true" };
1059
+ break;
1060
+ }
1061
+ if (sampleValue) {
1062
+ return {
1063
+ ...acc,
1064
+ [arg.name]: {
1065
+ kind: Kind.ARGUMENT,
1066
+ name: { kind: Kind.NAME, value: arg.name },
1067
+ value: sampleValue
1068
+ }
1069
+ };
1070
+ }
1071
+ return acc;
1072
+ }, {});
1073
+ const allDefaultArgs = Object.keys(argDefaults).length === selectedArgs.length;
1074
+ const argumentsList = selectedArgs.map((arg) => {
1075
+ if (allDefaultArgs) {
1076
+ return argDefaults[arg.name];
1077
+ }
1078
+ hasArgVars = true;
1079
+ return {
1080
+ kind: Kind.ARGUMENT,
1081
+ name: { kind: Kind.NAME, value: arg.name },
1082
+ value: {
1083
+ kind: Kind.VARIABLE,
1084
+ name: { kind: Kind.NAME, value: arg.name }
1085
+ }
1086
+ };
1087
+ });
1088
+ const queryAST = {
1089
+ kind: Kind.DOCUMENT,
1090
+ definitions: [
1091
+ {
1092
+ kind: Kind.OPERATION_DEFINITION,
1093
+ operation: operationTypeToTypeNode[operationType],
1094
+ name: hasArgVars ? {
1095
+ kind: Kind.NAME,
1096
+ value: operationName
1097
+ } : void 0,
1098
+ variableDefinitions: hasArgVars ? selectedArgs.map(variableDefinitions) : [],
1099
+ selectionSet: {
1100
+ kind: Kind.SELECTION_SET,
1101
+ selections: [
1102
+ {
1103
+ kind: Kind.FIELD,
1104
+ name: { kind: Kind.NAME, value: operationName },
1105
+ arguments: argumentsList,
1106
+ selectionSet: {
1107
+ kind: Kind.SELECTION_SET,
1108
+ selections: [
1109
+ { kind: Kind.FIELD, name: { kind: Kind.NAME, value: `# ${operationName} fields` } }
1110
+ ]
1111
+ }
1112
+ }
1113
+ ]
1114
+ }
1115
+ }
1116
+ ]
1117
+ };
1118
+ const sampleText = print(queryAST);
1119
+ return sampleText;
1120
+ }
1121
+ function variableDefinitions(arg) {
1122
+ var _a, _b, _c;
1123
+ const hasDefault = (_a = arg.meta) == null ? void 0 : _a.some((m) => m.name === "defaults");
1124
+ let defaultValue = "";
1125
+ const getDefaultValue = (_c = (_b = arg.meta) == null ? void 0 : _b.find((m) => m.name === "defaults")) == null ? void 0 : _c.value;
1126
+ if (getDefaultValue && typeof getDefaultValue !== "string") {
1127
+ defaultValue = JSON.stringify(getDefaultValue);
1128
+ }
1129
+ return {
1130
+ kind: Kind.VARIABLE_DEFINITION,
1131
+ variable: {
1132
+ kind: Kind.VARIABLE,
1133
+ name: { kind: Kind.NAME, value: arg.name }
1134
+ },
1135
+ type: {
1136
+ kind: Kind.NAMED_TYPE,
1137
+ name: { kind: Kind.NAME, value: arg.type }
1138
+ },
1139
+ ...hasDefault && defaultValue ? {
1140
+ defaultValue: {
1141
+ kind: Kind.STRING,
1142
+ value: defaultValue
1143
+ }
1144
+ } : {}
1145
+ };
1146
+ }
1147
+
1148
+ // src/converters/gql-operation.ts
1149
+ function gqlOperationToUniformRef(operationType, fieldsMap, schema, options) {
1150
+ const references = [];
1151
+ for (const [operationName, operationField] of Object.entries(fieldsMap)) {
1152
+ const definitions = [];
1153
+ let flatReturn = false;
1154
+ let flat = false;
1155
+ let argFlat = false;
1156
+ if (options == null ? void 0 : options.flat) {
1157
+ flatReturn = true;
1158
+ flat = true;
1159
+ argFlat = true;
1160
+ }
1161
+ const args = gqlArgToUniformDefinitionProperty(new Context(
1162
+ /* @__PURE__ */ new Set(),
1163
+ options,
1164
+ {
1165
+ flat,
1166
+ flatArg: argFlat
1167
+ },
1168
+ schema
1169
+ ), operationField.args);
1170
+ const returns = gqlFieldToUniformDefinitionProperty(new Context(
1171
+ /* @__PURE__ */ new Set(),
1172
+ options,
1173
+ {
1174
+ flatReturn
1175
+ },
1176
+ schema
1177
+ ), operationField);
1178
+ let returnProperties = returns.properties || [];
1179
+ if (options == null ? void 0 : options.flat) {
1180
+ returnProperties = [returns];
1181
+ }
1182
+ definitions.push({
1183
+ title: "Arguments",
1184
+ properties: args
1185
+ });
1186
+ definitions.push({
1187
+ title: "Returns",
1188
+ properties: returnProperties
1189
+ });
1190
+ const exampleQuery = simpleGraphqlExample(
1191
+ operationType,
1192
+ operationName,
1193
+ args,
1194
+ returnProperties
1195
+ );
1196
+ const examples = [
1197
+ {
1198
+ codeblock: {
1199
+ tabs: [
1200
+ {
1201
+ title: "",
1202
+ language: "graphql",
1203
+ code: exampleQuery
1204
+ }
1205
+ ]
1206
+ }
1207
+ }
1208
+ ];
1209
+ const exampleGroup = {
1210
+ description: "",
1211
+ examples
1212
+ };
1213
+ const operation = new GQLOperation(operationField);
1214
+ switch (operationType) {
1215
+ case ReferenceType3.GRAPHQL_QUERY: {
1216
+ operation.__operationType = OperationTypeNode2.QUERY;
1217
+ break;
1218
+ }
1219
+ case ReferenceType3.GRAPHQL_MUTATION: {
1220
+ operation.__operationType = OperationTypeNode2.MUTATION;
1221
+ break;
1222
+ }
1223
+ case ReferenceType3.GRAPHQL_SUBSCRIPTION: {
1224
+ operation.__operationType = OperationTypeNode2.SUBSCRIPTION;
1225
+ break;
1226
+ }
1227
+ default: {
1228
+ console.error(`Invalid operation type: ${operationType}`);
1229
+ }
1230
+ }
1231
+ const ref = uniformify(
1232
+ new Context(
1233
+ /* @__PURE__ */ new Set(),
1234
+ options,
1235
+ {},
1236
+ schema
1237
+ ),
1238
+ operation,
1239
+ definitions,
1240
+ [exampleGroup]
1241
+ );
1242
+ if (ref) {
1243
+ references.push(ref);
1244
+ }
1245
+ }
1246
+ return references;
1247
+ }
1248
+
1249
+ // src/converters/gql-query.ts
1250
+ function graphqlQueriesToUniformReferences(schema, options) {
1251
+ var _a;
1252
+ const references = [];
1253
+ const queries = schema.getRootType(OperationTypeNode3.QUERY);
1254
+ const queryFields = (_a = queries == null ? void 0 : queries.getFields) == null ? void 0 : _a.call(queries);
1255
+ if (queryFields) {
1256
+ const filteredQueryFields = filterFieldsByRegions(
1257
+ queryFields,
1258
+ "query",
1259
+ options == null ? void 0 : options.regions
1260
+ );
1261
+ references.push(...gqlOperationToUniformRef(
1262
+ ReferenceType4.GRAPHQL_QUERY,
1263
+ filteredQueryFields,
1264
+ schema,
1265
+ options
1266
+ ));
1267
+ }
1268
+ return references;
1269
+ }
1270
+
1271
+ // src/converters/gql-mutation.ts
1272
+ import { OperationTypeNode as OperationTypeNode4 } from "graphql";
1273
+ import { ReferenceType as ReferenceType5 } from "@xyd-js/uniform";
1274
+ function graphqlMutationsToUniformReferences(schema, options) {
1275
+ var _a;
1276
+ const references = [];
1277
+ const mutations = schema.getRootType(OperationTypeNode4.MUTATION);
1278
+ const mutationFields = (_a = mutations == null ? void 0 : mutations.getFields) == null ? void 0 : _a.call(mutations);
1279
+ if (mutationFields) {
1280
+ const filteredMutationFields = filterFieldsByRegions(
1281
+ mutationFields,
1282
+ "mutation",
1283
+ options == null ? void 0 : options.regions
1284
+ );
1285
+ references.push(...gqlOperationToUniformRef(
1286
+ ReferenceType5.GRAPHQL_MUTATION,
1287
+ filteredMutationFields,
1288
+ schema,
1289
+ options
1290
+ ));
1291
+ }
1292
+ return references;
1293
+ }
1294
+
1295
+ // src/converters/gql-subscription.ts
1296
+ import { OperationTypeNode as OperationTypeNode5 } from "graphql";
1297
+ import { ReferenceType as ReferenceType6 } from "@xyd-js/uniform";
1298
+ function graphqlSubscriptionsToUniformReferences(schema, options) {
1299
+ var _a;
1300
+ const references = [];
1301
+ const mutations = schema.getRootType(OperationTypeNode5.SUBSCRIPTION);
1302
+ const mutationFields = (_a = mutations == null ? void 0 : mutations.getFields) == null ? void 0 : _a.call(mutations);
1303
+ if (mutationFields) {
1304
+ const filteredMutationFields = filterFieldsByRegions(
1305
+ mutationFields,
1306
+ "mutation",
1307
+ options == null ? void 0 : options.regions
1308
+ );
1309
+ references.push(...gqlOperationToUniformRef(
1310
+ ReferenceType6.GRAPHQL_SUBSCRIPTION,
1311
+ filteredMutationFields,
1312
+ schema,
1313
+ options
1314
+ ));
1315
+ }
1316
+ return references;
1317
+ }
1318
+
1319
+ // src/opendocs.graphql
1320
+ var opendocs_default = 'directive @scope(value: String!) on ENUM_VALUE\n\nenum OpenDocsScope {\n _PLACEHOLDER_\n}\n\ndirective @docs(\n route: String\n sort: [OpenDocsSortInput!]\n sortStack: [[String!]!]\n flattenTypes: Boolean\n sidebar: [OpenDocsPage]\n group: [String]\n) repeatable on SCHEMA\n\ninput OpenDocsPage {\n type: OpenDocsSidebarItemType!\n key: String!\n group: String\n path: String\n pages: [OpenDocsPage]\n}\n\nenum OpenDocsSidebarItemType {\n OPERATION\n TYPE\n}\n\ndirective @doc(\n group: [String!],\n path: String,\n example: OpenDocsExampleInput,\n scopes: [OpenDocsScope],\n) on QUERY |\n FIELD_DEFINITION |\n MUTATION |\n SUBSCRIPTION |\n OBJECT |\n INPUT_OBJECT |\n ENUM |\n SCALAR |\n UNION\n\n# TODO: finish\ninput OpenDocsExampleInput {\n """\n The name of the code\n """\n code: String!\n}\n\ninput OpenDocsSortInput {\n node: String\n group: [String!]\n stack: Int\n}';
1321
+
1322
+ // src/schema.ts
1323
+ async function gqlSchemaToReferences(schemaLocation, options) {
1324
+ const schemaLocations = Array.isArray(schemaLocation) ? schemaLocation : [schemaLocation];
1325
+ schemaLocations.unshift(opendocs_default);
1326
+ const schemaContents = await Promise.all(schemaLocations.map(async (location) => {
1327
+ if (location.startsWith("http://") || location.startsWith("https://")) {
1328
+ const response = await fetch(location);
1329
+ if (!response.ok) {
1330
+ throw new Error(`Failed to fetch schema from URL: ${location}`);
1331
+ }
1332
+ return response.text();
1333
+ }
1334
+ if (fs.existsSync(location)) {
1335
+ return fs.readFileSync(location, "utf-8");
1336
+ }
1337
+ return location;
1338
+ }));
1339
+ const mergedTypeDefs = mergeTypeDefs(schemaContents);
1340
+ const schemaString = print2(mergedTypeDefs);
1341
+ const schema = buildSchema(schemaString, {
1342
+ assumeValid: true
1343
+ });
1344
+ if (schemaContents.length > 2) {
1345
+ console.warn(`Warning: More than 2 schema files provided - no all featues will be supported!`);
1346
+ }
1347
+ docDirectiveChain(schemaContents[1], schema);
1348
+ if (!options) {
1349
+ options = {};
1350
+ }
1351
+ if (!options.hasOwnProperty("flat")) {
1352
+ options.flat = true;
1353
+ }
1354
+ options = {
1355
+ ...options,
1356
+ ...openDocsExtensionsToOptions(schema)
1357
+ };
1358
+ const references = [
1359
+ // types
1360
+ ...graphqlTypesToUniformReferences(schema, options),
1361
+ // queries
1362
+ ...graphqlQueriesToUniformReferences(schema, options),
1363
+ // mutations
1364
+ ...graphqlMutationsToUniformReferences(schema, options),
1365
+ // subscriptions
1366
+ ...graphqlSubscriptionsToUniformReferences(schema, options)
1367
+ ];
1368
+ const sortConfig = options.sort ?? { sort: DEFAULT_SORT_ORDER };
1369
+ references.sort((a, b) => {
1370
+ const aOrder = getSortOrder(a, sortConfig);
1371
+ const bOrder = getSortOrder(b, sortConfig);
1372
+ return aOrder - bOrder;
1373
+ });
1374
+ if (options.route) {
1375
+ references.__UNSAFE_route = () => options.route;
1376
+ }
1377
+ return references;
1378
+ }
1379
+ function getSortOrder(reference, sortConfig) {
1380
+ const sortItems = sortConfig.sort ?? DEFAULT_SORT_ORDER;
1381
+ const sortStacks = sortConfig.sortStack ?? [];
1382
+ const referenceGroups = getReferenceGroups(reference);
1383
+ for (let groupIndex = 0; groupIndex < sortItems.length; groupIndex++) {
1384
+ const sortItem = sortItems[groupIndex];
1385
+ if (matchesPrimaryGroup(reference, sortItem)) {
1386
+ const stackIndex = sortItem.stack !== void 0 ? sortItem.stack : 0;
1387
+ const positionInGroup = calculatePositionInGroup(reference, stackIndex, sortStacks);
1388
+ const result = groupIndex * 1e3 + positionInGroup;
1389
+ return result;
1390
+ }
1391
+ }
1392
+ return Number.MAX_SAFE_INTEGER;
1393
+ }
1394
+ function matchesPrimaryGroup(reference, sortItem) {
1395
+ if (sortItem.node) {
1396
+ const context = reference.context;
1397
+ if ((context == null ? void 0 : context.graphqlTypeShort) === sortItem.node) {
1398
+ return true;
1399
+ }
1400
+ return false;
1401
+ }
1402
+ if (sortItem.group && sortItem.group.length > 0) {
1403
+ const referenceGroups = getReferenceGroups(reference);
1404
+ const match = sortItem.group.some((group) => referenceGroups.includes(group));
1405
+ return match;
1406
+ }
1407
+ return true;
1408
+ }
1409
+ function calculatePositionInGroup(reference, stackIndex, sortStacks) {
1410
+ if (stackIndex < 0 || stackIndex >= sortStacks.length) {
1411
+ return 0;
1412
+ }
1413
+ const stackGroups = sortStacks[stackIndex];
1414
+ const referenceGroups = getReferenceGroups(reference);
1415
+ for (let i = 0; i < stackGroups.length; i++) {
1416
+ const stackGroup = stackGroups[i];
1417
+ if (referenceGroups.includes(stackGroup)) {
1418
+ return i;
1419
+ }
1420
+ }
1421
+ return 999;
1422
+ }
1423
+ function getReferenceGroups(reference) {
1424
+ const context = reference.context;
1425
+ if ((context == null ? void 0 : context.group) && Array.isArray(context.group)) {
1426
+ return context.group;
1427
+ }
1428
+ if (reference.__UNSAFE_selector) {
1429
+ try {
1430
+ const selector = reference.__UNSAFE_selector;
1431
+ const metadata = selector("[metadata]");
1432
+ if (metadata == null ? void 0 : metadata.groups) {
1433
+ return metadata.groups;
1434
+ }
1435
+ } catch (e) {
1436
+ }
1437
+ }
1438
+ return [];
1439
+ }
1440
+ function docDirectiveChain(rawSDL, schema) {
1441
+ const ast = parse(rawSDL);
1442
+ const metadata = {
1443
+ fields: /* @__PURE__ */ new Map()
1444
+ };
1445
+ visit(ast, {
1446
+ SchemaExtension(node) {
1447
+ var _a;
1448
+ for (const directive of node.directives || []) {
1449
+ if (directive.name.value === OPEN_DOCS_SCHEMA_DIRECTIVE_NAME) {
1450
+ const groupArg = (_a = directive.arguments) == null ? void 0 : _a.find((arg) => arg.name.value === "group");
1451
+ if ((groupArg == null ? void 0 : groupArg.value.kind) === "ListValue") {
1452
+ metadata.rootGroups = groupArg.value.values.filter((v) => v.kind === "StringValue").map((v) => v.value);
1453
+ }
1454
+ }
1455
+ }
1456
+ }
1457
+ });
1458
+ visit(ast, {
1459
+ ObjectTypeExtension(node) {
1460
+ var _a, _b;
1461
+ const validNodeTypes = ["Query", "Mutation", "Subscription"];
1462
+ if (!validNodeTypes.includes(node.name.value)) return;
1463
+ const typeLevelDoc = (_a = node.directives) == null ? void 0 : _a.find((d) => d.name.value === OPEN_DOCS_DIRECTIVE_NAME);
1464
+ const typeLevelDocArgs = typeLevelDoc ? extractDocArgs(typeLevelDoc) : {};
1465
+ for (const field of node.fields ?? []) {
1466
+ const fieldName = field.name.value;
1467
+ const fieldDoc = (_b = field.directives) == null ? void 0 : _b.find((d) => d.name.value === OPEN_DOCS_DIRECTIVE_NAME);
1468
+ const fieldDocArgs = fieldDoc ? extractDocArgs(fieldDoc) : {};
1469
+ let path = fieldDocArgs.path;
1470
+ if (typeLevelDocArgs.path && fieldDocArgs.path) {
1471
+ path = `${typeLevelDocArgs.path}/${fieldDocArgs.path}`;
1472
+ } else if (typeLevelDocArgs.path) {
1473
+ path = typeLevelDocArgs.path;
1474
+ }
1475
+ if (!fieldDocArgs.path && path) {
1476
+ path += "/" + fieldName;
1477
+ }
1478
+ const groups = fieldDocArgs.groups ?? typeLevelDocArgs.groups ?? [];
1479
+ const effectiveDoc = {
1480
+ groups,
1481
+ path
1482
+ };
1483
+ metadata.fields.set(`${node.name.value}.${fieldName}`, effectiveDoc);
1484
+ }
1485
+ }
1486
+ });
1487
+ schema.__metadata = metadata;
1488
+ }
1489
+ function extractDocArgs(directive) {
1490
+ const info = {};
1491
+ for (const arg of directive.arguments ?? []) {
1492
+ if (arg.name.value === "group" && arg.value.kind === "ListValue") {
1493
+ info.groups = arg.value.values.filter((v) => v.kind === "StringValue").map((v) => v.value);
1494
+ } else if (arg.name.value === "path" && arg.value.kind === "StringValue") {
1495
+ info.path = arg.value.value;
1496
+ }
1497
+ }
1498
+ return info;
1499
+ }
1500
+ export {
1501
+ gqlSchemaToReferences
1502
+ };
1503
+ //# sourceMappingURL=index.js.map