@xyd-js/gql 0.1.0-xyd.9 → 0.1.0-xyd.96

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 (89) hide show
  1. package/CHANGELOG.md +810 -0
  2. package/LICENSE +21 -0
  3. package/TODO.md +8 -0
  4. package/__fixtures__/-1.opendocs.docs-nested/input.graphql +66 -0
  5. package/__fixtures__/-1.opendocs.docs-nested/output.json +554 -0
  6. package/__fixtures__/-1.opendocs.flat/input.graphql +19 -0
  7. package/__fixtures__/-1.opendocs.flat/output.json +243 -0
  8. package/__fixtures__/-1.opendocs.scopes/input.graphql +33 -0
  9. package/__fixtures__/-1.opendocs.scopes/output.json +378 -0
  10. package/__fixtures__/-1.opendocs.sidebar/input.graphql +44 -0
  11. package/__fixtures__/-1.opendocs.sort/input.graphql +92 -0
  12. package/__fixtures__/-1.opendocs.sort/output.json +1078 -0
  13. package/__fixtures__/-1.opendocs.sort+group/input.graphql +111 -0
  14. package/__fixtures__/-1.opendocs.sort+group/output.json +1114 -0
  15. package/__fixtures__/-1.opendocs.sort+group+path/input.graphql +118 -0
  16. package/__fixtures__/-1.opendocs.sort+group+path/output.json +1114 -0
  17. package/__fixtures__/-2.complex.github/input.graphql +69424 -0
  18. package/__fixtures__/-2.complex.github/output.json +269874 -0
  19. package/__fixtures__/-2.complex.livesession/input.graphql +23 -0
  20. package/__fixtures__/-2.complex.livesession/output.json +302 -0
  21. package/__fixtures__/-2.complex.monday/input.graphql +6089 -0
  22. package/__fixtures__/-2.complex.monday/output.json +1 -0
  23. package/__fixtures__/-3.array-non-null-return/input.graphql +9 -0
  24. package/__fixtures__/-3.array-non-null-return/output.json +151 -0
  25. package/__fixtures__/1.basic/input.graphql +118 -0
  26. package/__fixtures__/1.basic/output.json +630 -0
  27. package/__fixtures__/2.circular/input.graphql +17 -0
  28. package/__fixtures__/2.circular/output.json +248 -0
  29. package/__fixtures__/3.opendocs/input.graphql +27 -0
  30. package/__fixtures__/3.opendocs/output.json +338 -0
  31. package/__fixtures__/4.union/input.graphql +19 -0
  32. package/__fixtures__/4.union/output.json +344 -0
  33. package/__fixtures__/5.flat/input.graphql +27 -0
  34. package/__fixtures__/5.flat/output.json +383 -0
  35. package/__fixtures__/6.default-values/input.graphql +47 -0
  36. package/__fixtures__/6.default-values/output.json +655 -0
  37. package/__fixtures__/7.type-args/input.graphql +19 -0
  38. package/__fixtures__/7.type-args/output.json +301 -0
  39. package/__fixtures__/8.default-sort/input.graphql +60 -0
  40. package/__fixtures__/8.default-sort/output.json +1078 -0
  41. package/__tests__/gqlSchemaToReferences.test.ts +109 -0
  42. package/__tests__/utils.ts +45 -0
  43. package/declarations.d.ts +4 -0
  44. package/dist/index.d.ts +17 -1
  45. package/dist/index.js +1334 -19871
  46. package/dist/index.js.map +1 -1
  47. package/dist/opendocs.graphql +56 -0
  48. package/package.json +7 -9
  49. package/src/context.ts +17 -0
  50. package/src/converters/gql-arg.ts +51 -0
  51. package/src/converters/gql-enum.ts +27 -0
  52. package/src/converters/gql-field.ts +164 -0
  53. package/src/converters/gql-input.ts +34 -0
  54. package/src/converters/gql-interface.ts +35 -0
  55. package/src/converters/gql-mutation.ts +36 -0
  56. package/src/converters/gql-object.ts +83 -0
  57. package/src/converters/gql-operation.ts +128 -0
  58. package/src/converters/gql-query.ts +36 -0
  59. package/src/converters/gql-sample.ts +159 -0
  60. package/src/converters/gql-scalar.ts +16 -0
  61. package/src/converters/gql-subscription.ts +36 -0
  62. package/src/converters/gql-types.ts +195 -0
  63. package/src/converters/gql-union.ts +40 -0
  64. package/src/gql-core.ts +362 -0
  65. package/src/opendocs.graphql +56 -0
  66. package/src/opendocs.ts +253 -0
  67. package/src/schema.ts +253 -67
  68. package/src/types.ts +103 -0
  69. package/src/utils.ts +21 -96
  70. package/tsconfig.json +1 -1
  71. package/tsup.config.ts +15 -1
  72. package/vitest.config.ts +15 -1
  73. package/examples/basic/index.ts +0 -12
  74. package/examples/basic/schema.graphqls +0 -89
  75. package/examples/basic/todo-app.graphqls +0 -184
  76. package/examples/graphql-types/graphql-types.0.basic.graphqls +0 -28
  77. package/examples/nested/nested-arg.0.not-required.graphqls +0 -8
  78. package/examples/nested/nested-arg.0.required.graphqls +0 -8
  79. package/examples/nested/nested-arg.1.deep.graphqls +0 -12
  80. package/src/hydration/README.md +0 -1
  81. package/src/hydration/gql-arg.ts +0 -53
  82. package/src/hydration/gql-field.ts +0 -206
  83. package/src/hydration/gql-input.ts +0 -67
  84. package/src/hydration/gql-object.ts +0 -35
  85. package/src/hydration/gql-types.ts +0 -50
  86. package/src/samples/index.ts +0 -95
  87. package/test/graphql-types.0.test.ts +0 -125
  88. package/test/nested-arg.0.test.ts +0 -208
  89. package/test/nested-arg.1.test.ts +0 -19
@@ -0,0 +1,56 @@
1
+ directive @scope(value: String!) on ENUM_VALUE
2
+
3
+ enum OpenDocsScope {
4
+ _PLACEHOLDER_
5
+ }
6
+
7
+ directive @docs(
8
+ route: String
9
+ sort: [OpenDocsSortInput!]
10
+ sortStack: [[String!]!]
11
+ flattenTypes: Boolean
12
+ sidebar: [OpenDocsPage]
13
+ group: [String]
14
+ ) repeatable on SCHEMA
15
+
16
+ input OpenDocsPage {
17
+ type: OpenDocsSidebarItemType!
18
+ key: String!
19
+ group: String
20
+ path: String
21
+ pages: [OpenDocsPage]
22
+ }
23
+
24
+ enum OpenDocsSidebarItemType {
25
+ OPERATION
26
+ TYPE
27
+ }
28
+
29
+ directive @doc(
30
+ group: [String!],
31
+ path: String,
32
+ example: OpenDocsExampleInput,
33
+ scopes: [OpenDocsScope],
34
+ ) on QUERY |
35
+ FIELD_DEFINITION |
36
+ MUTATION |
37
+ SUBSCRIPTION |
38
+ OBJECT |
39
+ INPUT_OBJECT |
40
+ ENUM |
41
+ SCALAR |
42
+ UNION
43
+
44
+ # TODO: finish
45
+ input OpenDocsExampleInput {
46
+ """
47
+ The name of the code
48
+ """
49
+ code: String!
50
+ }
51
+
52
+ input OpenDocsSortInput {
53
+ node: String
54
+ group: [String!]
55
+ stack: Int
56
+ }
package/package.json CHANGED
@@ -1,18 +1,16 @@
1
1
  {
2
2
  "name": "@xyd-js/gql",
3
- "version": "0.1.0-xyd.9",
3
+ "version": "0.1.0-xyd.96",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
7
  "dependencies": {
8
- "@graphql-markdown/core": "^1.12.0",
9
- "@graphql-markdown/graphql": "^1.1.4",
10
- "@graphql-markdown/types": "^1.4.0",
11
- "graphql-config": "^5.1.2",
12
- "gray-matter": "^4.0.3",
13
- "json-to-graphql-query": "^2.3.0",
14
- "@xyd-js/core": "0.1.0-xyd.9",
15
- "@xyd-js/uniform": "0.1.0-xyd.11"
8
+ "@graphql-tools/merge": "^8.4.0",
9
+ "@graphql-tools/schema": "^10.0.0",
10
+ "github-slugger": "^2.0.0",
11
+ "graphql": "^16.8.1",
12
+ "@xyd-js/core": "0.1.0-xyd.96",
13
+ "@xyd-js/uniform": "0.1.0-xyd.98"
16
14
  },
17
15
  "devDependencies": {
18
16
  "rimraf": "^3.0.2",
package/src/context.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { GraphQLSchema } from "graphql";
2
+ import {GQLSchemaToReferencesOptions, NestedGraphqlType} from "./types";
3
+
4
+ export class Context {
5
+ constructor(
6
+ public processedTypes: Set<NestedGraphqlType> = new Set(),
7
+ public globalOptions?: GQLSchemaToReferencesOptions,
8
+ public config?: {
9
+ flatReturn?: boolean,
10
+ flat?: boolean,
11
+ flatArg?: boolean,
12
+ },
13
+ public schema?: GraphQLSchema
14
+ ) {
15
+ }
16
+ }
17
+
@@ -0,0 +1,51 @@
1
+ import {GraphQLArgument, GraphQLInputObjectType} from "graphql";
2
+
3
+ import {DefinitionProperty} from "@xyd-js/uniform";
4
+
5
+ import {gqlInputToUniformDefinitionProperty} from "./gql-input";
6
+ import {Context} from "../context";
7
+ import {gqlFieldToUniformMeta, gqlFieldTypeInfo, propsUniformify} from "../gql-core";
8
+
9
+ // gqlArgToUniformDefinitionProperty converts GraphQL arguments into xyd 'uniform' definition properties
10
+ export function gqlArgToUniformDefinitionProperty(
11
+ ctx: Context,
12
+ args: readonly GraphQLArgument[],
13
+ ): DefinitionProperty[] {
14
+ const resp: DefinitionProperty[] = []
15
+
16
+ args.forEach(arg => {
17
+ const fieldInfo = gqlFieldTypeInfo(arg);
18
+ if (!fieldInfo.typeFlat) {
19
+ console.error("gqlArgToUniformDefinitionProperty: no typeFlat for", arg.name);
20
+ return;
21
+ }
22
+
23
+ const flatType = fieldInfo.typeFlat
24
+
25
+ if (flatType instanceof GraphQLInputObjectType) {
26
+ const inputObj = flatType
27
+ const meta = gqlFieldToUniformMeta(arg)
28
+ const defProperty = gqlInputToUniformDefinitionProperty(
29
+ ctx,
30
+ inputObj,
31
+ )
32
+
33
+ resp.push({
34
+ ...defProperty,
35
+ type: arg.type.toJSON(),
36
+ name: arg.name,
37
+ description: arg.description || "",
38
+ meta: [
39
+ ...defProperty.meta || [],
40
+ ...meta || []
41
+ ],
42
+ });
43
+ } else {
44
+ const defProperty = propsUniformify(ctx, arg)
45
+ resp.push(defProperty)
46
+ }
47
+ });
48
+
49
+ return resp;
50
+ }
51
+
@@ -0,0 +1,27 @@
1
+ import {GraphQLEnumType} from "graphql";
2
+
3
+ import type {DefinitionProperty, Reference} from "@xyd-js/uniform";
4
+ import {uniformify} from "../gql-core";
5
+ import {NestedGraphqlType} from "../types";
6
+ import {Context} from "../context";
7
+
8
+ // gqlEnumToUniformRef is a helper function to convert a GraphQL enum type into a 'uniform' reference.
9
+ export function gqlEnumToUniformRef(ctx: Context, gqlType: GraphQLEnumType): Reference {
10
+ const props: DefinitionProperty[] = gqlType.getValues().map(value => ({
11
+ name: value.name,
12
+ type: "string", // TODO: other types?
13
+ description: value.description || "",
14
+ }))
15
+
16
+ return uniformify(
17
+ ctx,
18
+ gqlType,
19
+ [
20
+ {
21
+ title: "Valid values",
22
+ properties: props
23
+ }
24
+ ],
25
+ [],
26
+ )
27
+ }
@@ -0,0 +1,164 @@
1
+ import {
2
+ GraphQLField,
3
+ GraphQLFieldMap,
4
+ GraphQLInputField,
5
+ GraphQLInputFieldMap,
6
+ GraphQLInputObjectType,
7
+ GraphQLUnionType,
8
+ GraphQLObjectType,
9
+ GraphQLInterfaceType,
10
+ GraphQLScalarType,
11
+ GraphQLNonNull,
12
+ } from "graphql";
13
+
14
+ import type {DefinitionProperty, DefinitionPropertyMeta} from "@xyd-js/uniform";
15
+
16
+ import {NestedGraphqlType} from "../types";
17
+ import {gqlFieldTypeInfo, propsUniformify} from "../gql-core";
18
+ import {gqlUnionToUniformDefinitionProperties} from "./gql-union";
19
+ import {Context} from "../context";
20
+
21
+ // gqlFieldToUniformDefinitionProperty converts GraphQL fields (field or input field) into xyd 'uniform' definition property
22
+ export function gqlFieldToUniformDefinitionProperty(
23
+ ctx: Context,
24
+ field: GraphQLField<any, any> | GraphQLInputField,
25
+ ): DefinitionProperty {
26
+ return GQLFieldConverter.convert(ctx, field);
27
+ }
28
+
29
+ export class GQLFieldConverter {
30
+ private constructor(private ctx: Context) {
31
+ }
32
+
33
+ public static convert(
34
+ ctx: Context,
35
+ field: GraphQLField<any, any> | GraphQLInputField,
36
+ ): DefinitionProperty {
37
+ return new GQLFieldConverter(ctx).convert(field);
38
+ }
39
+
40
+ private convert(
41
+ field: GraphQLField<any, any> | GraphQLInputField,
42
+ parent?: NestedGraphqlType,
43
+ ): DefinitionProperty {
44
+ if (this.ctx.globalOptions?.flat && (this.ctx.config?.flatReturn || this.ctx.config?.flat)) {
45
+ const info = gqlFieldTypeInfo(field)
46
+ const meta: DefinitionPropertyMeta[] = []
47
+
48
+ const props = propsUniformify(this.ctx, field, [], meta)
49
+ if (this.ctx.config?.flatReturn) {
50
+ return {
51
+ ...props,
52
+ name: ""
53
+ }
54
+ }
55
+
56
+ if (this.ctx.config?.flat) {
57
+ return props
58
+ }
59
+ }
60
+
61
+ const fieldInfo = gqlFieldTypeInfo(field)
62
+ const graphqlTypeFlat = fieldInfo?.typeFlat
63
+
64
+ let properties
65
+
66
+ switch (graphqlTypeFlat?.constructor) {
67
+ case GraphQLObjectType:
68
+ case GraphQLInputObjectType:
69
+ case GraphQLInterfaceType: {
70
+ properties = this.nestedProperties(graphqlTypeFlat as NestedGraphqlType)
71
+ break;
72
+ }
73
+ case GraphQLScalarType:
74
+ case GraphQLNonNull: {
75
+ properties = this.definitionPropsFromNestedObj(field) || []
76
+ break;
77
+ }
78
+ case GraphQLUnionType: {
79
+ properties = gqlUnionToUniformDefinitionProperties(this.ctx, graphqlTypeFlat as GraphQLUnionType)
80
+ break;
81
+ }
82
+ }
83
+
84
+ const resp = propsUniformify(
85
+ this.ctx,
86
+ field,
87
+ properties,
88
+ )
89
+
90
+ if (graphqlTypeFlat) {
91
+ const nestedType = graphqlTypeFlat as NestedGraphqlType
92
+ nestedType.__definitionProperties = resp.properties
93
+ }
94
+
95
+ if (parent) {
96
+ parent.__definitionProperties = resp.properties
97
+ }
98
+
99
+ return resp
100
+ }
101
+
102
+ // TODO: fix any + another more safety solution?
103
+ // definitionPropsFromNestedObj converts graphql nested obj into xyd 'uniform' definition properties
104
+ private definitionPropsFromNestedObj(obj: any): DefinitionProperty[] | null {
105
+ if (!obj) {
106
+ return null
107
+ }
108
+ if (obj.getFields) {
109
+ return this.nestedProperties(obj)
110
+ }
111
+
112
+ if (obj.ofType) {
113
+ return this.definitionPropsFromNestedObj(obj.ofType)
114
+ }
115
+
116
+ if (obj.type) {
117
+ return this.definitionPropsFromNestedObj(obj.type)
118
+ }
119
+
120
+ return null
121
+ }
122
+
123
+ // deepFieldMap iterates over GraphQL field (field or input fields) maps
124
+ private deepFieldMap(
125
+ fieldsMap: GraphQLFieldMap<any, any> | GraphQLInputFieldMap,
126
+ parent: NestedGraphqlType
127
+ ): DefinitionProperty[] {
128
+ const properties: DefinitionProperty[] = []
129
+
130
+ for (const [name, field] of Object.entries(fieldsMap)) {
131
+ const prop = this.convert(
132
+ field,
133
+ parent
134
+ )
135
+
136
+ if (prop) {
137
+ properties.push(prop)
138
+ }
139
+ }
140
+
141
+ return properties
142
+ }
143
+
144
+ // nestedProperties get fields from object or input object types and iterates over them
145
+ private nestedProperties(objectType: NestedGraphqlType) {
146
+ // Check if we've already processed this type to prevent circular dependencies
147
+ if (this.ctx.processedTypes.has(objectType)) {
148
+ if (objectType?.__definitionProperties) {
149
+ return objectType.__definitionProperties || []
150
+ }
151
+
152
+ return [] // Return empt array for already processed types to break the cycle
153
+ }
154
+
155
+ // Mark this type as being processed
156
+ this.ctx.processedTypes.add(objectType)
157
+
158
+ const nestedFields = objectType?.getFields?.()
159
+ const result = this.deepFieldMap(nestedFields, objectType)
160
+
161
+ return result
162
+ }
163
+ }
164
+
@@ -0,0 +1,34 @@
1
+ import {GraphQLInputObjectType} from "graphql";
2
+
3
+ import {Reference} from "@xyd-js/uniform";
4
+
5
+ import {gqlObjectPropsUniformify, uniformify} from "../gql-core";
6
+ import {Context} from "../context";
7
+
8
+ // gqlInputToUniformRef is a helper function to convert a GraphQL input object type into a 'uniform' reference.
9
+ export function gqlInputToUniformRef(ctx: Context, gqlType: GraphQLInputObjectType): Reference {
10
+ const prop = gqlInputToUniformDefinitionProperty(
11
+ ctx,
12
+ gqlType,
13
+ )
14
+
15
+ return uniformify(
16
+ ctx,
17
+ gqlType,
18
+ [
19
+ {
20
+ title: "Fields",
21
+ properties: prop.properties || []
22
+ }
23
+ ],
24
+ []
25
+ )
26
+ }
27
+
28
+ // gqlInputToUniformDefinitionProperty is a helper function to convert a GraphQL input object into a xyd definition property.
29
+ export function gqlInputToUniformDefinitionProperty(
30
+ ctx: Context,
31
+ obj: GraphQLInputObjectType,
32
+ ) {
33
+ return gqlObjectPropsUniformify(ctx, obj)
34
+ }
@@ -0,0 +1,35 @@
1
+ import {GraphQLInterfaceType, GraphQLField} from "graphql";
2
+
3
+ import {Definition, DefinitionProperty, Reference} from "@xyd-js/uniform";
4
+
5
+ import {gqlFieldToUniformDefinitionProperty} from "./gql-field";
6
+ import {uniformify} from "../gql-core";
7
+ import {NestedGraphqlType} from "../types";
8
+ import {Context} from "../context";
9
+
10
+ export function gqlInterfaceToUniformRef(ctx: Context, interfaceType: GraphQLInterfaceType): Reference {
11
+ const properties = gqlInterfaceToUniformDefinitionProperties(ctx, interfaceType)
12
+
13
+ const definitions: Definition[] = [
14
+ {
15
+ title: "Fields",
16
+ properties,
17
+ }
18
+ ]
19
+
20
+ return uniformify(
21
+ ctx,
22
+ interfaceType,
23
+ definitions,
24
+ []
25
+ )
26
+ }
27
+
28
+ export function gqlInterfaceToUniformDefinitionProperties(ctx: Context, interfaceType: GraphQLInterfaceType): DefinitionProperty[] {
29
+ return Object.values(interfaceType.getFields()).map((field: GraphQLField<any, any>) => {
30
+ return gqlFieldToUniformDefinitionProperty(
31
+ ctx,
32
+ field,
33
+ )
34
+ })
35
+ }
@@ -0,0 +1,36 @@
1
+ import type {GraphQLSchema} from "graphql";
2
+ import {OperationTypeNode} from "graphql";
3
+
4
+ import {Reference, ReferenceType} from "@xyd-js/uniform";
5
+
6
+ import type {GQLSchemaToReferencesOptions} from "../types";
7
+ import {filterFieldsByRegions} from "../utils";
8
+ import {gqlOperationToUniformRef} from "./gql-operation";
9
+
10
+ export function graphqlMutationsToUniformReferences(
11
+ schema: GraphQLSchema,
12
+ options?: GQLSchemaToReferencesOptions,
13
+ ) {
14
+ const references: Reference[] = []
15
+
16
+ const mutations = schema.getRootType(OperationTypeNode.MUTATION)
17
+ const mutationFields = mutations?.getFields?.()
18
+
19
+ if (mutationFields) {
20
+ // Filter mutation fields based on regions if provided
21
+ const filteredMutationFields = filterFieldsByRegions(
22
+ mutationFields,
23
+ "mutation",
24
+ options?.regions
25
+ );
26
+
27
+ references.push(...gqlOperationToUniformRef(
28
+ ReferenceType.GRAPHQL_MUTATION,
29
+ filteredMutationFields,
30
+ schema,
31
+ options,
32
+ ))
33
+ }
34
+
35
+ return references;
36
+ }
@@ -0,0 +1,83 @@
1
+ import {GraphQLObjectType} from "graphql";
2
+
3
+ import {Definition, DefinitionProperty, DefinitionVariant} from "@xyd-js/uniform";
4
+
5
+ import {gqlFieldToUniformDefinitionProperty} from "./gql-field";
6
+ import {gqlObjectPropsUniformify, uniformify} from "../gql-core";
7
+ import {Context} from "../context";
8
+ import {gqlArgToUniformDefinitionProperty} from "./gql-arg";
9
+
10
+ // gqlObjectToUniformRef is a helper function to convert a GraphQL object type into a 'uniform' reference.
11
+ export function gqlObjectToUniformRef(
12
+ ctx: Context,
13
+ gqlType: GraphQLObjectType
14
+ ) {
15
+ const definitions: Definition[] = []
16
+ const graphqlFields: DefinitionProperty[] = []
17
+
18
+ const variants: DefinitionVariant[] = []
19
+
20
+ const argumentDefinition: Definition = {
21
+ title: "Arguments",
22
+ properties: [],
23
+ variants,
24
+ meta: [
25
+ {
26
+ name: "type",
27
+ value: "arguments",
28
+ },
29
+ ]
30
+ }
31
+
32
+ for (const [name, field] of Object.entries(gqlType.getFields())) {
33
+ if (!field?.args?.length) {
34
+ continue;
35
+ }
36
+
37
+ const args = gqlArgToUniformDefinitionProperty(ctx, field.args)
38
+
39
+ variants.push({
40
+ title: "",
41
+ properties: args,
42
+ meta: [
43
+ {
44
+ name: "symbolName",
45
+ value: name,
46
+ }
47
+ ]
48
+ })
49
+ }
50
+ definitions.push(argumentDefinition)
51
+
52
+ for (const [name, field] of Object.entries(gqlType.getFields())) {
53
+ const prop = gqlFieldToUniformDefinitionProperty(ctx, field)
54
+
55
+ graphqlFields.push(prop)
56
+ }
57
+
58
+ definitions.push({
59
+ title: "Fields",
60
+ properties: graphqlFields,
61
+ meta: [
62
+ {
63
+ name: "type",
64
+ value: "fields",
65
+ }
66
+ ]
67
+ })
68
+
69
+ return uniformify(
70
+ ctx,
71
+ gqlType,
72
+ definitions,
73
+ []
74
+ )
75
+ }
76
+
77
+ // gqlObjectToUniformDefinitionProperty is a helper function to convert a GraphQL object type into a xyd definition property.
78
+ export function gqlObjectToUniformDefinitionProperty(
79
+ ctx: Context,
80
+ obj: GraphQLObjectType,
81
+ ) {
82
+ return gqlObjectPropsUniformify(ctx, obj)
83
+ }
@@ -0,0 +1,128 @@
1
+ import { GraphQLFieldMap, GraphQLSchema, OperationTypeNode } from "graphql";
2
+ import { Definition, Example, Reference, ReferenceType, } from "@xyd-js/uniform";
3
+
4
+ import { type GQLSchemaToReferencesOptions, GQLOperation } from "../types";
5
+ import { gqlArgToUniformDefinitionProperty } from "./gql-arg";
6
+ import { gqlFieldToUniformDefinitionProperty } from "./gql-field";
7
+ import { simpleGraphqlExample } from "./gql-sample";
8
+ import { uniformify } from "../gql-core";
9
+ import { Context } from "../context";
10
+
11
+ // gqlOperationToUniformRef is a helper function to create a list of xyd reference for a GraphQL operation (query or mutation).
12
+ export function gqlOperationToUniformRef(
13
+ operationType: ReferenceType.GRAPHQL_MUTATION | ReferenceType.GRAPHQL_QUERY | ReferenceType.GRAPHQL_SUBSCRIPTION,
14
+ fieldsMap: GraphQLFieldMap<any, any>,
15
+ schema: GraphQLSchema,
16
+ options?: GQLSchemaToReferencesOptions,
17
+ ) {
18
+ const references: Reference[] = []
19
+
20
+ for (const [operationName, operationField] of Object.entries(fieldsMap)) {
21
+ const definitions: Definition[] = []
22
+ let flatReturn = false
23
+ let flat = false
24
+ let argFlat = false
25
+
26
+ if (options?.flat) {
27
+ flatReturn = true
28
+ flat = true
29
+ argFlat = true
30
+ }
31
+
32
+ const args = gqlArgToUniformDefinitionProperty(new Context(
33
+ new Set(),
34
+ options,
35
+ {
36
+ flat,
37
+ flatArg: argFlat,
38
+ },
39
+ schema
40
+ ), operationField.args)
41
+
42
+
43
+ const returns = gqlFieldToUniformDefinitionProperty(new Context(
44
+ new Set(),
45
+ options,
46
+ {
47
+ flatReturn
48
+ },
49
+ schema
50
+ ), operationField)
51
+ let returnProperties = returns.properties || []
52
+ if (options?.flat) {
53
+ returnProperties = [returns]
54
+ }
55
+
56
+ definitions.push({
57
+ title: "Arguments",
58
+ properties: args,
59
+ })
60
+ definitions.push({
61
+ title: "Returns",
62
+ properties: returnProperties,
63
+ })
64
+
65
+ const exampleQuery = simpleGraphqlExample(
66
+ operationType,
67
+ operationName,
68
+ args,
69
+ returnProperties
70
+ )
71
+
72
+ const examples: Example[] = [
73
+ {
74
+ codeblock: {
75
+ tabs: [
76
+ {
77
+ title: "",
78
+ language: "graphql",
79
+ code: exampleQuery,
80
+ }
81
+ ]
82
+ }
83
+ }
84
+ ]
85
+
86
+ const exampleGroup = {
87
+ description: "",
88
+ examples,
89
+ }
90
+
91
+ const operation = new GQLOperation(operationField)
92
+ switch (operationType) {
93
+ case ReferenceType.GRAPHQL_QUERY: {
94
+ operation.__operationType = OperationTypeNode.QUERY;
95
+ break;
96
+ }
97
+ case ReferenceType.GRAPHQL_MUTATION: {
98
+ operation.__operationType = OperationTypeNode.MUTATION;
99
+ break;
100
+ }
101
+ case ReferenceType.GRAPHQL_SUBSCRIPTION: {
102
+ operation.__operationType = OperationTypeNode.SUBSCRIPTION;
103
+ break;
104
+ }
105
+ default: {
106
+ console.error(`Invalid operation type: ${operationType}`);
107
+ }
108
+ }
109
+
110
+ const ref = uniformify(
111
+ new Context(
112
+ new Set(),
113
+ options,
114
+ {},
115
+ schema
116
+ ),
117
+ operation,
118
+ definitions,
119
+ [exampleGroup]
120
+ )
121
+
122
+ if (ref) {
123
+ references.push(ref)
124
+ }
125
+ }
126
+
127
+ return references
128
+ }