2017-08-08 12:12:45 +00:00
|
|
|
package graphql
|
2017-08-08 10:40:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
"github.com/shurcooL/graphql/ident"
|
2017-08-08 10:40:09 +00:00
|
|
|
)
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
func constructQuery(qctx *queryContext, v interface{}, variables map[string]interface{}) string {
|
|
|
|
query := qctx.Query(v)
|
2017-08-08 10:40:09 +00:00
|
|
|
if variables != nil {
|
|
|
|
return "query(" + queryArguments(variables) + ")" + query
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
func constructMutation(qctx *queryContext, v interface{}, variables map[string]interface{}) string {
|
|
|
|
query := qctx.Query(v)
|
2017-08-08 10:40:09 +00:00
|
|
|
if variables != nil {
|
|
|
|
return "mutation(" + queryArguments(variables) + ")" + query
|
|
|
|
}
|
|
|
|
return "mutation" + query
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryArguments constructs a minified arguments string for variables.
|
|
|
|
//
|
|
|
|
// E.g., map[string]interface{}{"a": Int(123), "b": NewBoolean(true)} -> "$a:Int!$b:Boolean".
|
|
|
|
func queryArguments(variables map[string]interface{}) string {
|
|
|
|
sorted := make([]string, 0, len(variables))
|
|
|
|
for k := range variables {
|
|
|
|
sorted = append(sorted, k)
|
|
|
|
}
|
|
|
|
sort.Strings(sorted)
|
|
|
|
var s string
|
|
|
|
for _, k := range sorted {
|
|
|
|
v := variables[k]
|
|
|
|
s += "$" + k + ":"
|
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
// TODO: Support t.Elem() being a pointer, if needed. Probably want to do this recursively.
|
|
|
|
s += "[" + t.Elem().Name() + "!]" // E.g., "[IssueState!]".
|
|
|
|
case reflect.Ptr:
|
|
|
|
// Pointer is an optional type, so no "!" at the end.
|
|
|
|
s += t.Elem().Name() // E.g., "Int".
|
|
|
|
default:
|
|
|
|
name := t.Name()
|
|
|
|
if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubql/issues/12.
|
|
|
|
name = "ID"
|
|
|
|
}
|
|
|
|
// Value is a required type, so add "!" to the end.
|
|
|
|
s += name + "!" // E.g., "Int!".
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
type queryContext struct {
|
|
|
|
// Scalars are Go types that map to GraphQL scalars, and therefore we don't want to expand them.
|
|
|
|
Scalars []reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query uses writeQuery to recursively construct
|
2017-08-08 10:40:09 +00:00
|
|
|
// a minified query string from the provided struct v.
|
|
|
|
//
|
2017-08-08 12:12:45 +00:00
|
|
|
// E.g., struct{Foo Int, BarBaz *Boolean} -> "{foo,barBaz}".
|
|
|
|
func (c *queryContext) Query(v interface{}) string {
|
2017-08-08 10:40:09 +00:00
|
|
|
var buf bytes.Buffer
|
2017-08-08 12:12:45 +00:00
|
|
|
c.writeQuery(&buf, reflect.TypeOf(v), false)
|
2017-08-08 10:40:09 +00:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
// writeQuery writes a minified query for t to w. If inline is true,
|
|
|
|
// the struct fields of t are inlined into parent struct.
|
|
|
|
func (c *queryContext) writeQuery(w io.Writer, t reflect.Type, inline bool) {
|
2017-08-08 10:40:09 +00:00
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Ptr, reflect.Slice:
|
2017-08-08 12:12:45 +00:00
|
|
|
c.writeQuery(w, t.Elem(), false)
|
2017-08-08 10:40:09 +00:00
|
|
|
case reflect.Struct:
|
2017-08-08 12:12:45 +00:00
|
|
|
// Special handling of scalar struct types. Don't expand them.
|
|
|
|
for _, scalar := range c.Scalars {
|
|
|
|
if t == scalar {
|
|
|
|
return
|
|
|
|
}
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
if !inline {
|
|
|
|
io.WriteString(w, "{")
|
|
|
|
}
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
2017-08-08 12:12:45 +00:00
|
|
|
if i != 0 {
|
2017-08-08 10:40:09 +00:00
|
|
|
io.WriteString(w, ",")
|
|
|
|
}
|
|
|
|
f := t.Field(i)
|
|
|
|
value, ok := f.Tag.Lookup("graphql")
|
|
|
|
inlineField := f.Anonymous && !ok
|
|
|
|
if !inlineField {
|
|
|
|
if ok {
|
|
|
|
io.WriteString(w, value)
|
|
|
|
} else {
|
2017-08-08 12:12:45 +00:00
|
|
|
io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase())
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 12:12:45 +00:00
|
|
|
c.writeQuery(w, f.Type, inlineField)
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
if !inline {
|
|
|
|
io.WriteString(w, "}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|