@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
@@ -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
+ }
@@ -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 graphqlQueriesToUniformReferences(
11
+ schema: GraphQLSchema,
12
+ options?: GQLSchemaToReferencesOptions,
13
+ ): Reference[] {
14
+ const references: Reference[] = []
15
+
16
+ const queries = schema.getRootType(OperationTypeNode.QUERY)
17
+ const queryFields = queries?.getFields?.()
18
+
19
+ if (queryFields) {
20
+ // Filter query fields based on regions if provided
21
+ const filteredQueryFields = filterFieldsByRegions(
22
+ queryFields,
23
+ "query",
24
+ options?.regions
25
+ );
26
+
27
+ references.push(...gqlOperationToUniformRef(
28
+ ReferenceType.GRAPHQL_QUERY,
29
+ filteredQueryFields,
30
+ schema,
31
+ options
32
+ ))
33
+ }
34
+
35
+ return references;
36
+ }
@@ -0,0 +1,159 @@
1
+ import {
2
+ Kind,
3
+ print,
4
+ OperationTypeNode,
5
+ ASTNode,
6
+ ArgumentNode,
7
+ TypeNode,
8
+ ValueNode,
9
+ VariableDefinitionNode,
10
+ } from 'graphql';
11
+
12
+ import {
13
+ DefinitionProperty,
14
+ ReferenceType
15
+ } from "@xyd-js/uniform";
16
+
17
+ const operationTypeToTypeNode = {
18
+ [ReferenceType.GRAPHQL_QUERY]: OperationTypeNode.QUERY,
19
+ [ReferenceType.GRAPHQL_MUTATION]: OperationTypeNode.MUTATION,
20
+ [ReferenceType.GRAPHQL_SUBSCRIPTION]: OperationTypeNode.SUBSCRIPTION,
21
+ }
22
+
23
+ // simpleGraphqlExample is a helper function to create a simple GraphQL example query or mutation.
24
+ export function simpleGraphqlExample(
25
+ operationType: ReferenceType.GRAPHQL_QUERY | ReferenceType.GRAPHQL_MUTATION | ReferenceType.GRAPHQL_SUBSCRIPTION,
26
+ operationName: string,
27
+ args: DefinitionProperty[],
28
+ returns: DefinitionProperty[],
29
+ ) {
30
+ // Filter required arguments or take first one if no required args
31
+ const requiredArgs = args.filter(arg => arg.meta?.some(m => m.name === 'required' && m.value === 'true'));
32
+ const selectedArgs = requiredArgs.length > 0 ? requiredArgs : args.slice(0, 1);
33
+
34
+ let hasArgVars = false
35
+
36
+ const argDefaults = selectedArgs.reduce<Record<string, ArgumentNode>>((acc, arg) => {
37
+ if (operationType != ReferenceType.GRAPHQL_QUERY) {
38
+ return acc; // Skip for mutations, as we use variables
39
+ }
40
+
41
+ let defaultValue: string = ""
42
+
43
+ const getDefaultValue = arg.meta?.find(m => m.name === 'defaults')?.value
44
+ if (getDefaultValue && typeof getDefaultValue !== 'string') {
45
+ defaultValue = JSON.stringify(getDefaultValue)
46
+ }
47
+
48
+ let sampleValue: ValueNode | undefined = undefined;
49
+
50
+ switch (arg.context?.graphqlTypeFlat) {
51
+ case 'String':
52
+ sampleValue = {kind: Kind.STRING, value: defaultValue || `example-${arg.name}`};
53
+ break;
54
+ case 'Int':
55
+ case 'Float':
56
+ sampleValue = {kind: Kind.INT, value: defaultValue || '0'};
57
+ break;
58
+ case 'Boolean':
59
+ sampleValue = {kind: Kind.BOOLEAN, value: defaultValue === 'true'};
60
+ break;
61
+ }
62
+
63
+ // For queries, use direct values; for mutations, use variables
64
+ if (sampleValue) {
65
+ return {
66
+ ...acc,
67
+ [arg.name]: {
68
+ kind: Kind.ARGUMENT,
69
+ name: {kind: Kind.NAME, value: arg.name},
70
+ value: sampleValue
71
+ }
72
+ };
73
+ }
74
+
75
+ return acc;
76
+ }, {});
77
+
78
+ const allDefaultArgs = Object.keys(argDefaults).length === selectedArgs.length
79
+
80
+ const argumentsList: ArgumentNode[] = selectedArgs.map(arg => {
81
+ if (allDefaultArgs) {
82
+ return argDefaults[arg.name]
83
+ }
84
+
85
+ hasArgVars = true
86
+
87
+ return {
88
+ kind: Kind.ARGUMENT,
89
+ name: {kind: Kind.NAME, value: arg.name},
90
+ value: {
91
+ kind: Kind.VARIABLE,
92
+ name: {kind: Kind.NAME, value: arg.name}
93
+ }
94
+ };
95
+ });
96
+
97
+ const queryAST: ASTNode = {
98
+ kind: Kind.DOCUMENT,
99
+ definitions: [
100
+ {
101
+ kind: Kind.OPERATION_DEFINITION,
102
+ operation: operationTypeToTypeNode[operationType],
103
+ name: hasArgVars ? {
104
+ kind: Kind.NAME,
105
+ value: operationName
106
+ } : undefined,
107
+ variableDefinitions: hasArgVars ? selectedArgs.map(variableDefinitions) : [],
108
+ selectionSet: {
109
+ kind: Kind.SELECTION_SET,
110
+ selections: [
111
+ {
112
+ kind: Kind.FIELD,
113
+ name: {kind: Kind.NAME, value: operationName},
114
+ arguments: argumentsList,
115
+ selectionSet: {
116
+ kind: Kind.SELECTION_SET,
117
+ selections: [
118
+ {kind: Kind.FIELD, name: {kind: Kind.NAME, value: `# ${operationName} fields`}}
119
+ ]
120
+ }
121
+ }
122
+ ]
123
+ }
124
+ }
125
+ ]
126
+ } as const;
127
+
128
+ const sampleText = print(queryAST);
129
+
130
+ return sampleText;
131
+ }
132
+
133
+ function variableDefinitions(arg: DefinitionProperty): VariableDefinitionNode {
134
+ const hasDefault = arg.meta?.some(m => m.name === 'defaults');
135
+ let defaultValue: string = ""
136
+
137
+ const getDefaultValue = arg.meta?.find(m => m.name === 'defaults')?.value
138
+ if (getDefaultValue && typeof getDefaultValue !== 'string') {
139
+ defaultValue = JSON.stringify(getDefaultValue)
140
+ }
141
+
142
+ return {
143
+ kind: Kind.VARIABLE_DEFINITION,
144
+ variable: {
145
+ kind: Kind.VARIABLE,
146
+ name: {kind: Kind.NAME, value: arg.name}
147
+ },
148
+ type: {
149
+ kind: Kind.NAMED_TYPE,
150
+ name: {kind: Kind.NAME, value: arg.type}
151
+ },
152
+ ...(hasDefault && defaultValue ? {
153
+ defaultValue: {
154
+ kind: Kind.STRING,
155
+ value: defaultValue
156
+ }
157
+ } : {})
158
+ };
159
+ }
@@ -0,0 +1,16 @@
1
+ import {GraphQLScalarType} from "@graphql-markdown/types";
2
+
3
+ import type {Reference} from "@xyd-js/uniform";
4
+
5
+ import {uniformify} from "../gql-core";
6
+ import {Context} from "../context";
7
+
8
+ // gqlScalarToUniformRef is a helper function to convert a GraphQL scalar type into a 'uniform' reference.
9
+ export function gqlScalarToUniformRef(ctx: Context, gqlType: GraphQLScalarType): Reference {
10
+ return uniformify(
11
+ ctx,
12
+ gqlType,
13
+ [],
14
+ []
15
+ )
16
+ }
@@ -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 graphqlSubscriptionsToUniformReferences(
11
+ schema: GraphQLSchema,
12
+ options?: GQLSchemaToReferencesOptions,
13
+ ) {
14
+ const references: Reference[] = []
15
+
16
+ const mutations = schema.getRootType(OperationTypeNode.SUBSCRIPTION)
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_SUBSCRIPTION,
29
+ filteredMutationFields,
30
+ schema,
31
+ options,
32
+ ))
33
+ }
34
+
35
+ return references;
36
+ }