FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
dgraph/graphql/dgraph/graphquery.go at dgraph-personal · unigraph-dev/dgraph · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
unigraph-dev
/
dgraph
Public
forked from
dgraph-io/dgraph
Notifications
You must be signed in to change notification settings
Fork
9
Star
15
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
dgraph
/
graphql
/
dgraph
/
graphquery.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
342 lines (310 loc) · 8.52 KB
Breadcrumbs
dgraph
/
graphql
/
dgraph
/
graphquery.go
Copy path
File metadata and controls
342 lines (310 loc) · 8.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
dgraph
import
(
"fmt"
"strings"
"github.com/dgraph-io/dgraph/gql"
"github.com/dgraph-io/dgraph/graphql/schema"
"github.com/dgraph-io/dgraph/x"
)
// AsString writes query as an indented dql query string. AsString doesn't
// validate query, and so doesn't return an error if query is 'malformed' - it might
// just write something that wouldn't parse as a Dgraph query.
func
AsString
(
queries
[]
*
gql.
GraphQuery
)
string
{
if
queries
==
nil
{
return
""
}
var
b
strings.
Builder
x
.
Check2
(
b
.
WriteString
(
"query {
\n
"
))
numRewrittenQueries
:=
0
for
_
,
q
:=
range
queries
{
if
q
==
nil
{
// Don't call writeQuery on a nil query
continue
}
writeQuery
(
&
b
,
q
,
" "
)
numRewrittenQueries
++
}
x
.
Check2
(
b
.
WriteString
(
"}"
))
if
numRewrittenQueries
==
0
{
// In case writeQuery has not been called on any query or all queries
// are nil. Then, return empty string. This case needs to be considered as
// we don't want to return query{} in this case.
return
""
}
return
b
.
String
()
}
func
writeQuery
(
b
*
strings.
Builder
,
query
*
gql.
GraphQuery
,
prefix
string
) {
if
query
.
Var
!=
""
||
query
.
Alias
!=
""
||
query
.
Attr
!=
""
{
x
.
Check2
(
b
.
WriteString
(
prefix
))
}
if
query
.
Var
!=
""
{
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"%s as "
,
query
.
Var
)))
}
if
query
.
Alias
!=
""
{
x
.
Check2
(
b
.
WriteString
(
query
.
Alias
))
x
.
Check2
(
b
.
WriteString
(
" : "
))
}
if
query
.
IsCount
{
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"count(%s)"
,
query
.
Attr
)))
}
else
if
query
.
Attr
!=
"val"
{
x
.
Check2
(
b
.
WriteString
(
query
.
Attr
))
}
else
if
isAggregateFn
(
query
.
Func
) {
x
.
Check2
(
b
.
WriteString
(
"sum(val("
))
writeNeedVar
(
b
,
query
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
else
{
x
.
Check2
(
b
.
WriteString
(
"val("
))
writeNeedVar
(
b
,
query
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
if
query
.
Func
!=
nil
{
writeRoot
(
b
,
query
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
if
query
.
Filter
!=
nil
{
x
.
Check2
(
b
.
WriteString
(
" @filter("
))
writeFilter
(
b
,
query
.
Filter
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
if
query
.
Func
==
nil
&&
hasOrderOrPage
(
query
) {
x
.
Check2
(
b
.
WriteString
(
" ("
))
writeOrderAndPage
(
b
,
query
,
false
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
if
len
(
query
.
Cascade
)
!=
0
{
if
query
.
Cascade
[
0
]
==
"__all__"
{
x
.
Check2
(
b
.
WriteString
(
" @cascade"
))
}
else
{
x
.
Check2
(
b
.
WriteString
(
" @cascade("
))
x
.
Check2
(
b
.
WriteString
(
strings
.
Join
(
query
.
Cascade
,
", "
)))
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
}
if
query
.
IsGroupby
{
x
.
Check2
(
b
.
WriteString
(
" @groupby("
))
writeGroupByAttributes
(
b
,
query
.
GroupbyAttrs
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
switch
{
case
len
(
query
.
Children
)
>
0
:
prefixAdd
:=
""
if
query
.
Attr
!=
""
{
x
.
Check2
(
b
.
WriteString
(
" {
\n
"
))
prefixAdd
=
" "
}
for
_
,
c
:=
range
query
.
Children
{
writeQuery
(
b
,
c
,
prefix
+
prefixAdd
)
}
if
query
.
Attr
!=
""
{
x
.
Check2
(
b
.
WriteString
(
prefix
))
x
.
Check2
(
b
.
WriteString
(
"}
\n
"
))
}
case
query
.
Var
!=
""
||
query
.
Alias
!=
""
||
query
.
Attr
!=
""
:
x
.
Check2
(
b
.
WriteString
(
"
\n
"
))
}
}
// writeNeedVar writes the NeedsVar of the query. For eg :-
// `userFollowerCount as sum(val(followers))` has `followers`
// as NeedsVar.
func
writeNeedVar
(
b
*
strings.
Builder
,
query
*
gql.
GraphQuery
) {
for
i
,
v
:=
range
query
.
NeedsVar
{
if
i
!=
0
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
v
.
Name
))
}
}
func
isAggregateFn
(
f
*
gql.
Function
)
bool
{
if
f
==
nil
{
return
false
}
switch
f
.
Name
{
case
"min"
,
"max"
,
"avg"
,
"sum"
:
return
true
}
return
false
}
func
writeGroupByAttributes
(
b
*
strings.
Builder
,
attrList
[]gql.
GroupByAttr
) {
for
i
,
attr
:=
range
attrList
{
if
i
!=
0
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
if
attr
.
Alias
!=
""
{
x
.
Check2
(
b
.
WriteString
(
attr
.
Alias
))
x
.
Check2
(
b
.
WriteString
(
" : "
))
}
x
.
Check2
(
b
.
WriteString
(
attr
.
Attr
))
}
}
func
writeUIDFunc
(
b
*
strings.
Builder
,
uids
[]
uint64
,
args
[]gql.
Arg
,
needVar
[]gql.
VarContext
) {
x
.
Check2
(
b
.
WriteString
(
"uid("
))
if
len
(
uids
)
>
0
{
// uid function with uint64 - uid(0x123, 0x456, ...)
for
i
,
uid
:=
range
uids
{
if
i
!=
0
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"%#x"
,
uid
)))
}
}
else
if
len
(
args
)
>
0
{
// uid function with a Dgraph query variable - uid(Post1)
for
i
,
arg
:=
range
args
{
if
i
!=
0
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
arg
.
Value
))
}
}
else
{
for
i
,
v
:=
range
needVar
{
if
i
!=
0
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
v
.
Name
))
}
}
x
.
Check2
(
b
.
WriteString
(
")"
))
}
// writeRoot writes the root function as well as any ordering and paging
// specified in q.
//
// Only uid(0x123, 0x124), type(...) and eq(Type.Predicate, ...) functions are supported at root.
// Multiple arguments for `eq` filter will be required in case of resolving `entities` query.
func
writeRoot
(
b
*
strings.
Builder
,
q
*
gql.
GraphQuery
) {
if
q
.
Func
==
nil
{
return
}
switch
{
case
q
.
Func
.
Name
==
"has"
:
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"(func: has(%s)"
,
q
.
Func
.
Attr
)))
case
q
.
Func
.
Name
==
"uid"
:
x
.
Check2
(
b
.
WriteString
(
"(func: "
))
writeUIDFunc
(
b
,
q
.
Func
.
UID
,
q
.
Func
.
Args
,
q
.
Func
.
NeedsVar
)
case
q
.
Func
.
Name
==
"type"
&&
len
(
q
.
Func
.
Args
)
==
1
:
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"(func: type(%s)"
,
q
.
Func
.
Args
[
0
].
Value
)))
case
q
.
Func
.
Name
==
"eq"
:
x
.
Check2
(
b
.
WriteString
(
"(func: eq("
))
writeFilterArguments
(
b
,
q
.
Func
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
writeOrderAndPage
(
b
,
q
,
true
)
}
// writeFilterArguments writes the filter arguments. If the filter
// is constructed in graphql query rewriting then `Attr` is an empty
// string since we add Attr in the argument itself.
func
writeFilterArguments
(
b
*
strings.
Builder
,
q
*
gql.
Function
) {
if
q
.
Attr
!=
""
{
x
.
Check2
(
b
.
WriteString
(
q
.
Attr
))
}
for
i
,
arg
:=
range
q
.
Args
{
if
i
!=
0
||
q
.
Attr
!=
""
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
if
q
.
Attr
!=
""
{
// quote the arguments since this is the case of
// @custom DQL string.
arg
.
Value
=
schema
.
MaybeQuoteArg
(
q
.
Name
,
arg
.
Value
)
}
x
.
Check2
(
b
.
WriteString
(
arg
.
Value
))
}
}
func
writeFilterFunction
(
b
*
strings.
Builder
,
f
*
gql.
Function
) {
if
f
==
nil
{
return
}
switch
{
case
f
.
Name
==
"uid"
:
writeUIDFunc
(
b
,
f
.
UID
,
f
.
Args
,
f
.
NeedsVar
)
default
:
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
"%s("
,
f
.
Name
)))
writeFilterArguments
(
b
,
f
)
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
}
func
writeFilter
(
b
*
strings.
Builder
,
ft
*
gql.
FilterTree
) {
if
ft
==
nil
{
return
}
switch
ft
.
Op
{
case
"and"
,
"or"
:
x
.
Check2
(
b
.
WriteRune
(
'('
))
for
i
,
child
:=
range
ft
.
Child
{
if
i
>
0
&&
i
<=
len
(
ft
.
Child
)
-
1
{
x
.
Check2
(
b
.
WriteString
(
fmt
.
Sprintf
(
" %s "
,
strings
.
ToUpper
(
ft
.
Op
))))
}
writeFilter
(
b
,
child
)
}
x
.
Check2
(
b
.
WriteRune
(
')'
))
case
"not"
:
if
len
(
ft
.
Child
)
>
0
{
x
.
Check2
(
b
.
WriteString
(
"NOT ("
))
writeFilter
(
b
,
ft
.
Child
[
0
])
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
default
:
writeFilterFunction
(
b
,
ft
.
Func
)
}
}
func
hasOrderOrPage
(
q
*
gql.
GraphQuery
)
bool
{
_
,
hasFirst
:=
q
.
Args
[
"first"
]
_
,
hasOffset
:=
q
.
Args
[
"offset"
]
return
len
(
q
.
Order
)
>
0
||
hasFirst
||
hasOffset
}
func
IsValueVar
(
attr
string
,
q
*
gql.
GraphQuery
)
bool
{
for
_
,
vars
:=
range
q
.
NeedsVar
{
if
attr
==
vars
.
Name
&&
vars
.
Typ
==
2
{
return
true
}
}
return
false
}
func
writeOrderAndPage
(
b
*
strings.
Builder
,
query
*
gql.
GraphQuery
,
root
bool
) {
var
wroteOrder
,
wroteFirst
bool
for
_
,
ord
:=
range
query
.
Order
{
if
root
||
wroteOrder
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
if
ord
.
Desc
{
x
.
Check2
(
b
.
WriteString
(
"orderdesc: "
))
}
else
{
x
.
Check2
(
b
.
WriteString
(
"orderasc: "
))
}
if
IsValueVar
(
ord
.
Attr
,
query
) {
x
.
Check2
(
b
.
WriteString
(
"val("
))
x
.
Check2
(
b
.
WriteString
(
ord
.
Attr
))
x
.
Check2
(
b
.
WriteRune
(
')'
))
}
else
{
x
.
Check2
(
b
.
WriteString
(
ord
.
Attr
))
}
wroteOrder
=
true
}
if
first
,
ok
:=
query
.
Args
[
"first"
];
ok
{
if
root
||
wroteOrder
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
"first: "
))
x
.
Check2
(
b
.
WriteString
(
first
))
wroteFirst
=
true
}
if
offset
,
ok
:=
query
.
Args
[
"offset"
];
ok
{
if
root
||
wroteOrder
||
wroteFirst
{
x
.
Check2
(
b
.
WriteString
(
", "
))
}
x
.
Check2
(
b
.
WriteString
(
"offset: "
))
x
.
Check2
(
b
.
WriteString
(
offset
))
}
}
Back
|
FazBrowse Home
|
New Git URL