Skip to content

Commit 389bf62

Browse files
add ArgumentAny and refactor
1 parent 510c56d commit 389bf62

7 files changed

Lines changed: 50 additions & 9 deletions

File tree

argument.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ type Argument struct {
1313
Value string
1414
}
1515

16+
func ArgumentAny(name string, value interface{}) (Argument, error) {
17+
switch v := value.(type) {
18+
case bool:
19+
return ArgumentBool(name, v), nil
20+
case int:
21+
return ArgumentInt(name, v), nil
22+
case string:
23+
return ArgumentString(name, v), nil
24+
case []string:
25+
return ArgumentStringSlice(name, v...), nil
26+
default:
27+
return Argument{}, ArgumentTypeNotSupportErr{Value: value}
28+
}
29+
}
30+
1631
func ArgumentBool(name string, value bool) Argument {
1732
return Argument{name, fmt.Sprintf("%v", value)}
1833
}

argument_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ func TestArgumentStringSlice(t *testing.T) {
3232

3333
a = ArgumentStringSlice("blocked")
3434
assert.Equal(t, Argument{"blocked", "[]"}, a)
35-
}
35+
}

error.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package graphb
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
type nameType string
68

@@ -46,3 +48,12 @@ type CyclicFieldErr struct {
4648
func (e CyclicFieldErr) Error() string {
4749
return fmt.Sprintf("Field %+v contains cyclic loop", e.Field)
4850
}
51+
52+
// ArgumentTypeNotSupportErr is returned when user tries to pass an unsupported type to ArgumentAny.
53+
type ArgumentTypeNotSupportErr struct {
54+
Value interface{}
55+
}
56+
57+
func (e ArgumentTypeNotSupportErr) Error() string {
58+
return fmt.Sprintf("Argument %+v of Type %T is not supported", e.Value, e.Value)
59+
}

example/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestMethodChaining(t *testing.T) {
2626
SetAlias("some_alias"),
2727
).
2828
AddFields(graphb.MakeField("b"))
29-
s, err := q.JSONBody()
29+
s, err := q.JSON()
3030
assert.Nil(t, err)
3131
assert.Equal(t, `{"query":"query{some_alias:a(string:\"123\"){x(string:\"123\",string_slice:[\"a\"]),y,},b,}"}`, s)
3232
}

field.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ func (f *Field) SetArguments(arguments ...Argument) *Field {
143143
return f
144144
}
145145

146+
func (f *Field) AddArguments(argument ...Argument) *Field {
147+
f.Arguments = append(f.Arguments, argument...)
148+
return f
149+
}
150+
146151
// SetFields sets the sub fields of a Field and return the pointer to this Field.
147152
func (f *Field) SetFields(fs ...*Field) *Field {
148153
f.Fields = fs

graphb_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestTheWholePackage(t *testing.T) {
2929
assert.Nil(t, err)
3030
assert.Equal(t, `query{Alias:courses(uid:123,blocked_nds:["nd013","nd014"]){key,id,},}`, str)
3131

32-
str, err = q.JSONBody()
32+
str, err = q.JSON()
3333
assert.Nil(t, err)
3434
assert.Equal(t, `{"query":"query{Alias:courses(uid:123,blocked_nds:[\"nd013\",\"nd014\"]){key,id,},}"}`, str)
3535

@@ -123,7 +123,7 @@ func TestTheWholePackage(t *testing.T) {
123123
),
124124
)
125125
assert.Nil(t, err)
126-
s, err := q.JSONBody()
126+
s, err := q.JSON()
127127
assert.Nil(t, err)
128128
assert.Equal(t, `{"query":"query another_test{users{id,username,threads(title:\"A Good Title\"){title,created_at,},},}"}`, s)
129129
})
@@ -136,7 +136,7 @@ func TestTheWholePackage(t *testing.T) {
136136
assert.Equal(t, "", s)
137137
assert.False(t, ok)
138138

139-
s, err = q.JSONBody()
139+
s, err = q.JSON()
140140
assert.Equal(t, "'muTatio' is an invalid operation type in GraphQL. A valid type is one of 'query', 'mutation', 'subscription'", err.Error())
141141
assert.Equal(t, "", s)
142142
})
@@ -176,7 +176,7 @@ func TestMethodChaining(t *testing.T) {
176176
SetAlias("some_alias"),
177177
).
178178
AddFields(MakeField("x"))
179-
s, err := q.JSONBody()
179+
s, err := q.JSON()
180180
assert.Nil(t, err)
181181
assert.Equal(t, `{"query":"query{some_alias:x(string:\"123\"){x(string:\"123\",string_slice:[\"a\"]),x,},x,}"}`, s)
182182
}

query.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func MakeQuery(Type operationType) *Query {
102102
return &Query{Type: Type}
103103
}
104104

105-
// JSONBody returns a json string with "query" field.
106-
func (q *Query) JSONBody() (string, error) {
105+
// JSON returns a json string with "query" field.
106+
func (q *Query) JSON() (string, error) {
107107
strCh, err := q.StringChan()
108108
if err != nil {
109109
return "", errors.WithStack(err)
@@ -118,6 +118,16 @@ func (q *Query) SetName(name string) *Query {
118118
return q
119119
}
120120

121+
// GetField return the field identified by the name. Nil if not exist.
122+
func (q *Query) GetField(name string) *Field {
123+
for _, f := range q.Fields {
124+
if f.Name == name {
125+
return f
126+
}
127+
}
128+
return nil
129+
}
130+
121131
// SetFields sets the Fields field of this Query.
122132
// If q.Fields already contains data, they will be replaced.
123133
func (q *Query) SetFields(fields ...*Field) *Query {

0 commit comments

Comments
 (0)