@@ -10,20 +10,55 @@ import (
1010 "github.com/shurcooL/graphql/ident"
1111)
1212
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+
1332func 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+
1541 if len (variables ) > 0 {
16- return "query(" + queryArguments (variables ) + ")" + query
42+ return queryPrefix + "(" + queryArguments (variables ) + ")" + query
43+ } else if queryName != "" {
44+ return queryPrefix + query
1745 }
1846 return query
1947}
2048
2149func 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+
2358 if len (variables ) > 0 {
24- return "mutation (" + queryArguments (variables ) + ")" + query
59+ return mutationPrefix + " (" + queryArguments (variables ) + ")" + query
2560 }
26- return "mutation" + query
61+ return mutationPrefix + query
2762}
2863
2964// queryArguments constructs a minified arguments string for variables.
0 commit comments