Skip to content

Commit a4a48d3

Browse files
committed
Add support for named queries/mutations
1 parent d48a9a7 commit a4a48d3

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

query.go

+40-5
Original file line numberDiff line numberDiff line change
@@ -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+
1332
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+
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

2149
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+
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.

query_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ func TestConstructQuery(t *testing.T) {
227227
}{},
228228
want: `{viewer{login,createdAt,id,databaseId}}`,
229229
},
230+
{
231+
inV: NewNamedOperation("FindPerson", struct {
232+
Login string
233+
}{}),
234+
inVariables: nil,
235+
want: `query FindPerson{login}`,
236+
},
237+
{
238+
inV: NewNamedOperation("FindPerson", struct {
239+
Login string
240+
}{}),
241+
inVariables: map[string]interface{}{"id": Int(1)},
242+
want: `query FindPerson($id:Int!){login}`,
243+
},
230244
}
231245
for _, tc := range tests {
232246
got := constructQuery(tc.inV, tc.inVariables)
@@ -262,6 +276,24 @@ func TestConstructMutation(t *testing.T) {
262276
},
263277
want: `mutation($input:AddReactionInput!){addReaction(input:$input){subject{reactionGroups{users{totalCount}}}}}`,
264278
},
279+
{
280+
inV: NewNamedOperation("ThumbsUp", struct {
281+
AddReaction struct {
282+
Name string
283+
} `graphql:"addReaction(input:$input)"`
284+
}{}),
285+
inVariables: nil,
286+
want: `mutation ThumbsUp{addReaction(input:$input){name}}`,
287+
},
288+
{
289+
inV: NewNamedOperation("ThumbsUp", struct {
290+
AddReaction struct {
291+
Name string
292+
} `graphql:"addReaction(input:$input)"`
293+
}{}),
294+
inVariables: map[string]interface{}{"id": Int(1)},
295+
want: `mutation ThumbsUp($id:Int!){addReaction(input:$input){name}}`,
296+
},
265297
}
266298
for _, tc := range tests {
267299
got := constructMutation(tc.inV, tc.inVariables)

0 commit comments

Comments
 (0)