@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.
- package/dist/development/GraphQlQueryPrinter.cjs +139 -0
- package/dist/development/GraphQlQueryPrinter.cjs.map +1 -0
- package/dist/development/GraphQlQueryPrinter.js +139 -0
- package/dist/development/GraphQlQueryPrinter.js.map +1 -0
- package/dist/development/index.cjs +13 -0
- package/dist/development/index.cjs.map +1 -0
- package/dist/development/index.js +13 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/nodes/GraphQlField.cjs +12 -0
- package/dist/development/nodes/GraphQlField.cjs.map +1 -0
- package/dist/development/nodes/GraphQlField.js +12 -0
- package/dist/development/nodes/GraphQlField.js.map +1 -0
- package/dist/development/nodes/GraphQlFragment.cjs +11 -0
- package/dist/development/nodes/GraphQlFragment.cjs.map +1 -0
- package/dist/development/nodes/GraphQlFragment.js +11 -0
- package/dist/development/nodes/GraphQlFragment.js.map +1 -0
- package/dist/development/nodes/GraphQlFragmentSpread.cjs +9 -0
- package/dist/development/nodes/GraphQlFragmentSpread.cjs.map +1 -0
- package/dist/development/nodes/GraphQlFragmentSpread.js +9 -0
- package/dist/development/nodes/GraphQlFragmentSpread.js.map +1 -0
- package/dist/development/nodes/GraphQlInlineFragment.cjs +10 -0
- package/dist/development/nodes/GraphQlInlineFragment.cjs.map +1 -0
- package/dist/development/nodes/GraphQlInlineFragment.js +10 -0
- package/dist/development/nodes/GraphQlInlineFragment.js.map +1 -0
- package/dist/production/GraphQlQueryPrinter.cjs +139 -0
- package/dist/production/GraphQlQueryPrinter.cjs.map +1 -0
- package/dist/production/GraphQlQueryPrinter.js +139 -0
- package/dist/production/GraphQlQueryPrinter.js.map +1 -0
- package/dist/production/index.cjs +13 -0
- package/dist/production/index.cjs.map +1 -0
- package/dist/production/index.js +13 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/nodes/GraphQlField.cjs +12 -0
- package/dist/production/nodes/GraphQlField.cjs.map +1 -0
- package/dist/production/nodes/GraphQlField.js +12 -0
- package/dist/production/nodes/GraphQlField.js.map +1 -0
- package/dist/production/nodes/GraphQlFragment.cjs +11 -0
- package/dist/production/nodes/GraphQlFragment.cjs.map +1 -0
- package/dist/production/nodes/GraphQlFragment.js +11 -0
- package/dist/production/nodes/GraphQlFragment.js.map +1 -0
- package/dist/production/nodes/GraphQlFragmentSpread.cjs +9 -0
- package/dist/production/nodes/GraphQlFragmentSpread.cjs.map +1 -0
- package/dist/production/nodes/GraphQlFragmentSpread.js +9 -0
- package/dist/production/nodes/GraphQlFragmentSpread.js.map +1 -0
- package/dist/production/nodes/GraphQlInlineFragment.cjs +10 -0
- package/dist/production/nodes/GraphQlInlineFragment.cjs.map +1 -0
- package/dist/production/nodes/GraphQlInlineFragment.js +10 -0
- package/dist/production/nodes/GraphQlInlineFragment.js.map +1 -0
- package/dist/types/GraphQlQueryPrinter.d.ts +23 -0
- package/dist/types/GraphQlQueryPrinter.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/nodes/GraphQlField.d.ts +17 -0
- package/dist/types/nodes/GraphQlField.d.ts.map +1 -0
- package/dist/types/nodes/GraphQlFragment.d.ts +8 -0
- package/dist/types/nodes/GraphQlFragment.d.ts.map +1 -0
- package/dist/types/nodes/GraphQlFragmentSpread.d.ts +5 -0
- package/dist/types/nodes/GraphQlFragmentSpread.d.ts.map +1 -0
- package/dist/types/nodes/GraphQlInlineFragment.d.ts +7 -0
- package/dist/types/nodes/GraphQlInlineFragment.d.ts.map +1 -0
- package/dist/types/nodes/index.d.ts +5 -0
- package/dist/types/nodes/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +46 -0
- package/src/GraphQlQueryPrinter.ts +146 -0
- package/src/index.ts +2 -0
- package/src/nodes/GraphQlField.ts +22 -0
- package/src/nodes/GraphQlFragment.ts +10 -0
- package/src/nodes/GraphQlFragmentSpread.ts +6 -0
- package/src/nodes/GraphQlInlineFragment.ts +9 -0
- package/src/nodes/index.ts +4 -0
- 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,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[]
|