@contember/graphql-builder 1.3.0-alpha.2

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 +126 -0
  2. package/dist/development/GraphQlQueryPrinter.cjs.map +1 -0
  3. package/dist/development/GraphQlQueryPrinter.js +126 -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 +126 -0
  26. package/dist/production/GraphQlQueryPrinter.cjs.map +1 -0
  27. package/dist/production/GraphQlQueryPrinter.js +126 -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 +21 -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 +131 -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,131 @@
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 usedFragments = new Set<string>()
16
+
17
+ private body = ''
18
+
19
+
20
+ public printDocument(
21
+ operation: 'query' | 'mutation',
22
+ select: GraphQlSelectionSet,
23
+ fragments: Record<string, GraphQlFragment>,
24
+ ): GraphQlPrintResult {
25
+ this.cleanState()
26
+ this.processSelectionSet(select, 1)
27
+ const body = this.body
28
+ this.body = ''
29
+ this.processUsedFragments(fragments)
30
+ const fragmentsStr = this.body
31
+ const variablesString = this.printVariables()
32
+ const query = `${operation}${variablesString} {\n${body}}${fragmentsStr}\n`
33
+ return {
34
+ query: query,
35
+ variables: Object.fromEntries(Object.entries(this.variables).map(([key, value]) => [key, value.value])),
36
+ }
37
+ }
38
+
39
+
40
+ private processUsedFragments(fragments: Record<string, GraphQlFragment>): void {
41
+ const printed = new Set()
42
+ while (this.usedFragments.size > 0) {
43
+ const fragmentName = this.usedFragments.values().next().value
44
+ this.usedFragments.delete(fragmentName)
45
+ if (printed.has(fragmentName)) {
46
+ continue
47
+ }
48
+ printed.add(fragmentName)
49
+ if (!fragments[fragmentName]) {
50
+ throw new Error(`Unknown fragment ${fragmentName}`)
51
+ }
52
+ this.processFragment(fragments[fragmentName])
53
+ }
54
+ }
55
+
56
+ private processFragment(fragment: GraphQlFragment): void {
57
+ this.body += `\nfragment ${fragment.name} on ${fragment.type} {\n`
58
+ this.processSelectionSet(fragment.selectionSet, 1)
59
+ this.body += '}'
60
+ }
61
+
62
+ private printVariables(): string {
63
+ let variablesString = ''
64
+ let i = 0
65
+ for (const [variableName, variable] of Object.entries(this.variables)) {
66
+ if (i++ === 0) {
67
+ variablesString += '('
68
+ } else {
69
+ variablesString += ', '
70
+ }
71
+ variablesString += `$${variableName}: ${variable.type}`
72
+ }
73
+ if (i > 0) {
74
+ variablesString += ')'
75
+ }
76
+ return variablesString
77
+ }
78
+
79
+ private processSelectionSet(selectionSet: GraphQlSelectionSet, indent: number): void {
80
+ for (const node of selectionSet) {
81
+ if (node instanceof GraphQlField) {
82
+ this.processField(node, indent)
83
+ } else if (node instanceof GraphQlFragmentSpread) {
84
+ this.body += this.indentString.repeat(indent) + '... ' + node.name + '\n'
85
+ this.usedFragments.add(node.name)
86
+ } else if (node instanceof GraphQlInlineFragment) {
87
+ this.body += this.indentString.repeat(indent) + '... on ' + node.type + ' {\n'
88
+ this.processSelectionSet(node.selectionSet, indent + 1)
89
+ this.body += this.indentString.repeat(indent) + '}\n'
90
+ }
91
+ }
92
+ }
93
+
94
+ private processField(field: GraphQlField, indent: number): void {
95
+ const indentString = this.indentString.repeat(indent)
96
+ this.body += indentString + (field.alias && field.alias !== field.name ? (field.alias + ': ' + field.name) : field.name)
97
+ let i = 0
98
+ for (const [argName, arg] of Object.entries(field.args)) {
99
+ if (arg.value === undefined) {
100
+ continue
101
+ }
102
+ const variableName = `${argName}_${arg.graphQlType.replace(/[\W]/g, '')}_${this.variableCounter++}`
103
+ if (i++ === 0) {
104
+ this.body += '('
105
+ } else {
106
+ this.body += ', '
107
+ }
108
+ this.body += `${argName}: $${variableName}`
109
+ this.variables[variableName] = {
110
+ type: arg.graphQlType,
111
+ value: arg.value,
112
+ }
113
+ }
114
+ if (i > 0) {
115
+ this.body += ')'
116
+ }
117
+ if (field.selectionSet) {
118
+ this.body += ' {\n'
119
+ this.processSelectionSet(field.selectionSet, indent + 1)
120
+ this.body += this.indentString.repeat(indent) + '}'
121
+ }
122
+ this.body += '\n'
123
+ }
124
+
125
+ private cleanState(): void {
126
+ this.body = ''
127
+ this.variableCounter = 0
128
+ this.variables = {}
129
+ this.usedFragments = new Set<string>()
130
+ }
131
+ }
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
+ }