@@ -10,20 +10,55 @@ import (
10
10
"github.com/shurcooL/graphql/ident"
11
11
)
12
12
13
+ type NamedOperation struct {
14
+ Name string
15
+ Document interface {}
16
+ }
17
+
18
+ func NewNamedOperation (name string , v interface {}) * NamedOperation {
19
+ return & NamedOperation {
20
+ Name : name ,
21
+ Document : v ,
22
+ }
23
+ }
24
+
25
+ func deconstructOperation (v interface {}) (string , interface {}) {
26
+ if named , ok := v .(* NamedOperation ); ok {
27
+ return named .Name , named .Document
28
+ }
29
+ return "" , v
30
+ }
31
+
13
32
func constructQuery (v interface {}, variables map [string ]interface {}) string {
14
- query := query (v )
33
+ queryName , queryDoc := deconstructOperation (v )
34
+ query := query (queryDoc )
35
+
36
+ queryPrefix := "query"
37
+ if queryName != "" {
38
+ queryPrefix = "query " + queryName
39
+ }
40
+
15
41
if len (variables ) > 0 {
16
- return "query(" + queryArguments (variables ) + ")" + query
42
+ return queryPrefix + "(" + queryArguments (variables ) + ")" + query
43
+ } else if queryName != "" {
44
+ return queryPrefix + query
17
45
}
18
46
return query
19
47
}
20
48
21
49
func constructMutation (v interface {}, variables map [string ]interface {}) string {
22
- query := query (v )
50
+ mutationName , queryDoc := deconstructOperation (v )
51
+ query := query (queryDoc )
52
+
53
+ mutationPrefix := "mutation"
54
+ if mutationName != "" {
55
+ mutationPrefix = "mutation " + mutationName
56
+ }
57
+
23
58
if len (variables ) > 0 {
24
- return "mutation (" + queryArguments (variables ) + ")" + query
59
+ return mutationPrefix + " (" + queryArguments (variables ) + ")" + query
25
60
}
26
- return "mutation" + query
61
+ return mutationPrefix + query
27
62
}
28
63
29
64
// queryArguments constructs a minified arguments string for variables.
0 commit comments