@contember/graphql-builder 1.3.0-alpha.10

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 (72) hide show
  1. package/dist/development/GraphQlQueryPrinter.cjs +139 -0
  2. package/dist/development/GraphQlQueryPrinter.cjs.map +1 -0
  3. package/dist/development/GraphQlQueryPrinter.js +139 -0
  4. package/dist/development/GraphQlQueryPrinter.js.map +1 -0
  5. package/dist/development/index.cjs +13 -0
  6. package/dist/development/index.cjs.map +1 -0
  7. package/dist/development/index.js +13 -0
  8. package/dist/development/index.js.map +1 -0
  9. package/dist/development/nodes/GraphQlField.cjs +12 -0
  10. package/dist/development/nodes/GraphQlField.cjs.map +1 -0
  11. package/dist/development/nodes/GraphQlField.js +12 -0
  12. package/dist/development/nodes/GraphQlField.js.map +1 -0
  13. package/dist/development/nodes/GraphQlFragment.cjs +11 -0
  14. package/dist/development/nodes/GraphQlFragment.cjs.map +1 -0
  15. package/dist/development/nodes/GraphQlFragment.js +11 -0
  16. package/dist/development/nodes/GraphQlFragment.js.map +1 -0
  17. package/dist/development/nodes/GraphQlFragmentSpread.cjs +9 -0
  18. package/dist/development/nodes/GraphQlFragmentSpread.cjs.map +1 -0
  19. package/dist/development/nodes/GraphQlFragmentSpread.js +9 -0
  20. package/dist/development/nodes/GraphQlFragmentSpread.js.map +1 -0
  21. package/dist/development/nodes/GraphQlInlineFragment.cjs +10 -0
  22. package/dist/development/nodes/GraphQlInlineFragment.cjs.map +1 -0
  23. package/dist/development/nodes/GraphQlInlineFragment.js +10 -0
  24. package/dist/development/nodes/GraphQlInlineFragment.js.map +1 -0
  25. package/dist/production/GraphQlQueryPrinter.cjs +139 -0
  26. package/dist/production/GraphQlQueryPrinter.cjs.map +1 -0
  27. package/dist/production/GraphQlQueryPrinter.js +139 -0
  28. package/dist/production/GraphQlQueryPrinter.js.map +1 -0
  29. package/dist/production/index.cjs +13 -0
  30. package/dist/production/index.cjs.map +1 -0
  31. package/dist/production/index.js +13 -0
  32. package/dist/production/index.js.map +1 -0
  33. package/dist/production/nodes/GraphQlField.cjs +12 -0
  34. package/dist/production/nodes/GraphQlField.cjs.map +1 -0
  35. package/dist/production/nodes/GraphQlField.js +12 -0
  36. package/dist/production/nodes/GraphQlField.js.map +1 -0
  37. package/dist/production/nodes/GraphQlFragment.cjs +11 -0
  38. package/dist/production/nodes/GraphQlFragment.cjs.map +1 -0
  39. package/dist/production/nodes/GraphQlFragment.js +11 -0
  40. package/dist/production/nodes/GraphQlFragment.js.map +1 -0
  41. package/dist/production/nodes/GraphQlFragmentSpread.cjs +9 -0
  42. package/dist/production/nodes/GraphQlFragmentSpread.cjs.map +1 -0
  43. package/dist/production/nodes/GraphQlFragmentSpread.js +9 -0
  44. package/dist/production/nodes/GraphQlFragmentSpread.js.map +1 -0
  45. package/dist/production/nodes/GraphQlInlineFragment.cjs +10 -0
  46. package/dist/production/nodes/GraphQlInlineFragment.cjs.map +1 -0
  47. package/dist/production/nodes/GraphQlInlineFragment.js +10 -0
  48. package/dist/production/nodes/GraphQlInlineFragment.js.map +1 -0
  49. package/dist/types/GraphQlQueryPrinter.d.ts +23 -0
  50. package/dist/types/GraphQlQueryPrinter.d.ts.map +1 -0
  51. package/dist/types/index.d.ts +3 -0
  52. package/dist/types/index.d.ts.map +1 -0
  53. package/dist/types/nodes/GraphQlField.d.ts +17 -0
  54. package/dist/types/nodes/GraphQlField.d.ts.map +1 -0
  55. package/dist/types/nodes/GraphQlFragment.d.ts +8 -0
  56. package/dist/types/nodes/GraphQlFragment.d.ts.map +1 -0
  57. package/dist/types/nodes/GraphQlFragmentSpread.d.ts +5 -0
  58. package/dist/types/nodes/GraphQlFragmentSpread.d.ts.map +1 -0
  59. package/dist/types/nodes/GraphQlInlineFragment.d.ts +7 -0
  60. package/dist/types/nodes/GraphQlInlineFragment.d.ts.map +1 -0
  61. package/dist/types/nodes/index.d.ts +5 -0
  62. package/dist/types/nodes/index.d.ts.map +1 -0
  63. package/dist/types/tsconfig.tsbuildinfo +1 -0
  64. package/package.json +46 -0
  65. package/src/GraphQlQueryPrinter.ts +146 -0
  66. package/src/index.ts +2 -0
  67. package/src/nodes/GraphQlField.ts +22 -0
  68. package/src/nodes/GraphQlFragment.ts +10 -0
  69. package/src/nodes/GraphQlFragmentSpread.ts +6 -0
  70. package/src/nodes/GraphQlInlineFragment.ts +9 -0
  71. package/src/nodes/index.ts +4 -0
  72. package/src/tsconfig.json +6 -0
@@ -0,0 +1,146 @@
1
+ import { JSONValue } from '@contember/schema'
2
+ import { GraphQlField, GraphQlFragment, GraphQlFragmentSpread, GraphQlInlineFragment, GraphQlSelectionSet } from './nodes'
3
+
4
+ export type GraphQlPrintResult = { query: string; variables: Record<string, JSONValue> }
5
+
6
+ export class GraphQlQueryPrinter {
7
+ private indentString = '\t'
8
+
9
+ private variableCounter = 0
10
+ private variables: Record<string, {
11
+ type: string
12
+ value: JSONValue
13
+ }> = {}
14
+
15
+ private variableValueToName: Record<string, Map<JSONValue, string>> = {}
16
+
17
+ private usedFragments = new Set<string>()
18
+
19
+ private body = ''
20
+
21
+
22
+ public printDocument(
23
+ operation: 'query' | 'mutation',
24
+ select: GraphQlSelectionSet,
25
+ fragments: Record<string, GraphQlFragment>,
26
+ ): GraphQlPrintResult {
27
+ this.cleanState()
28
+ this.processSelectionSet(select, 1)
29
+ const body = this.body
30
+ this.body = ''
31
+ this.processUsedFragments(fragments)
32
+ const fragmentsStr = this.body
33
+ const variablesString = this.printVariables()
34
+ const query = `${operation}${variablesString} {\n${body}}${fragmentsStr}\n`
35
+ return {
36
+ query: query,
37
+ variables: Object.fromEntries(Object.entries(this.variables).map(([key, value]) => [key, value.value])),
38
+ }
39
+ }
40
+
41
+
42
+ private processUsedFragments(fragments: Record<string, GraphQlFragment>): void {
43
+ const printed = new Set()
44
+ while (this.usedFragments.size > 0) {
45
+ const fragmentName = this.usedFragments.values().next().value
46
+ this.usedFragments.delete(fragmentName)
47
+ if (printed.has(fragmentName)) {
48
+ continue
49
+ }
50
+ printed.add(fragmentName)
51
+ if (!fragments[fragmentName]) {
52
+ throw new Error(`Unknown fragment ${fragmentName}`)
53
+ }
54
+ this.processFragment(fragments[fragmentName])
55
+ }
56
+ }
57
+
58
+ private processFragment(fragment: GraphQlFragment): void {
59
+ this.body += `\nfragment ${fragment.name} on ${fragment.type} {\n`
60
+ this.processSelectionSet(fragment.selectionSet, 1)
61
+ this.body += '}'
62
+ }
63
+
64
+ private printVariables(): string {
65
+ let variablesString = ''
66
+ let i = 0
67
+ for (const [variableName, variable] of Object.entries(this.variables)) {
68
+ if (i++ === 0) {
69
+ variablesString += '('
70
+ } else {
71
+ variablesString += ', '
72
+ }
73
+ variablesString += `$${variableName}: ${variable.type}`
74
+ }
75
+ if (i > 0) {
76
+ variablesString += ')'
77
+ }
78
+ return variablesString
79
+ }
80
+
81
+ private processSelectionSet(selectionSet: GraphQlSelectionSet, indent: number): void {
82
+ for (const node of selectionSet) {
83
+ if (node instanceof GraphQlField) {
84
+ this.processField(node, indent)
85
+ } else if (node instanceof GraphQlFragmentSpread) {
86
+ this.body += this.indentString.repeat(indent) + '... ' + node.name + '\n'
87
+ this.usedFragments.add(node.name)
88
+ } else if (node instanceof GraphQlInlineFragment) {
89
+ this.body += this.indentString.repeat(indent) + '... on ' + node.type + ' {\n'
90
+ this.processSelectionSet(node.selectionSet, indent + 1)
91
+ this.body += this.indentString.repeat(indent) + '}\n'
92
+ }
93
+ }
94
+ }
95
+
96
+ private processField(field: GraphQlField, indent: number): void {
97
+ const indentString = this.indentString.repeat(indent)
98
+ this.body += indentString + (field.alias && field.alias !== field.name ? (field.alias + ': ' + field.name) : field.name)
99
+ let i = 0
100
+ for (const [argName, arg] of Object.entries(field.args)) {
101
+ if (arg.value === undefined) {
102
+ continue
103
+ }
104
+ const variableName = this.resolveVariableName(arg.graphQlType, arg.value)
105
+ if (i++ === 0) {
106
+ this.body += '('
107
+ } else {
108
+ this.body += ', '
109
+ }
110
+ this.body += `${argName}: $${variableName}`
111
+ }
112
+ if (i > 0) {
113
+ this.body += ')'
114
+ }
115
+ if (field.selectionSet) {
116
+ this.body += ' {\n'
117
+ this.processSelectionSet(field.selectionSet, indent + 1)
118
+ this.body += this.indentString.repeat(indent) + '}'
119
+ }
120
+ this.body += '\n'
121
+ }
122
+
123
+ private resolveVariableName(graphQlType: string, value: JSONValue): string {
124
+ const variableName = this.variableValueToName[graphQlType]?.get(value)
125
+ if (variableName) {
126
+ return variableName
127
+ }
128
+ const newVariableName = `${graphQlType.replace(/[\W]/g, '')}_${this.variableCounter++}`
129
+ this.variables[newVariableName] = {
130
+ type: graphQlType,
131
+ value: value,
132
+ }
133
+ if (!this.variableValueToName[graphQlType]) {
134
+ this.variableValueToName[graphQlType] = new Map()
135
+ }
136
+ this.variableValueToName[graphQlType].set(value, newVariableName)
137
+ return newVariableName
138
+ }
139
+
140
+ private cleanState(): void {
141
+ this.body = ''
142
+ this.variableCounter = 0
143
+ this.variables = {}
144
+ this.usedFragments = new Set<string>()
145
+ }
146
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './nodes'
2
+ export * from './GraphQlQueryPrinter'
@@ -0,0 +1,22 @@
1
+ import { GraphQlFragmentSpread } from './GraphQlFragmentSpread'
2
+ import { GraphQlInlineFragment } from './GraphQlInlineFragment'
3
+ import { JSONValue } from '@contember/schema'
4
+
5
+ export class GraphQlField {
6
+ constructor(
7
+ public readonly alias: string | null,
8
+ public readonly name: string,
9
+ public readonly args: GraphQlFieldTypedArgs = {},
10
+ public readonly selectionSet?: GraphQlSelectionSet,
11
+ ) {
12
+ }
13
+ }
14
+
15
+ export type GraphQlFieldTypedArgs = Record<string, {
16
+ graphQlType: string
17
+ value?: JSONValue
18
+ }>
19
+
20
+ export type GraphQlSelectionSetItem = GraphQlField | GraphQlFragmentSpread | GraphQlInlineFragment
21
+
22
+ export type GraphQlSelectionSet = GraphQlSelectionSetItem[]
@@ -0,0 +1,10 @@
1
+ import { GraphQlSelectionSet } from './GraphQlField'
2
+
3
+ export class GraphQlFragment {
4
+ constructor(
5
+ public readonly name: string,
6
+ public readonly type: string,
7
+ public readonly selectionSet: GraphQlSelectionSet,
8
+ ) {
9
+ }
10
+ }
@@ -0,0 +1,6 @@
1
+ export class GraphQlFragmentSpread {
2
+ constructor(
3
+ public readonly name: string,
4
+ ) {
5
+ }
6
+ }
@@ -0,0 +1,9 @@
1
+ import { GraphQlSelectionSet } from './GraphQlField'
2
+
3
+ export class GraphQlInlineFragment {
4
+ constructor(
5
+ public readonly type: string,
6
+ public readonly selectionSet: GraphQlSelectionSet,
7
+ ) {
8
+ }
9
+ }
@@ -0,0 +1,4 @@
1
+ export * from './GraphQlField'
2
+ export * from './GraphQlFragmentSpread'
3
+ export * from './GraphQlFragment'
4
+ export * from './GraphQlInlineFragment'
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../../tsconfig.settings.json",
3
+ "compilerOptions": {
4
+ "outDir": "../dist/types"
5
+ }
6
+ }