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.
(queries []*dql.GraphQuery)
| 17 | // validate query, and so doesn't return an error if query is 'malformed' - it might |
| 18 | // just write something that wouldn't parse as a Dgraph query. |
| 19 | func AsString(queries []*dql.GraphQuery) string { |
| 20 | if len(queries) == 0 { |
| 21 | return "" |
| 22 | } |
| 23 | |
| 24 | var b strings.Builder |
| 25 | queryName := queries[len(queries)-1].Attr |
| 26 | x.Check2(b.WriteString("query ")) |
| 27 | addQueryVars(&b, queryName, queries[0].Args) |
| 28 | x.Check2(b.WriteString("{\n")) |
| 29 | |
| 30 | numRewrittenQueries := 0 |
| 31 | for _, q := range queries { |
| 32 | if q == nil { |
| 33 | // Don't call writeQuery on a nil query |
| 34 | continue |
| 35 | } |
| 36 | writeQuery(&b, q, " ") |
| 37 | numRewrittenQueries++ |
| 38 | } |
| 39 | x.Check2(b.WriteString("}")) |
| 40 | |
| 41 | if numRewrittenQueries == 0 { |
| 42 | // In case writeQuery has not been called on any query or all queries |
| 43 | // are nil. Then, return empty string. This case needs to be considered as |
| 44 | // we don't want to return query{} in this case. |
| 45 | return "" |
| 46 | } |
| 47 | return b.String() |
| 48 | } |
| 49 | |
| 50 | func addQueryVars(b *strings.Builder, queryName string, args map[string]string) { |
| 51 | dollarFound := false |