1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00
cowyo/vendor/github.com/shurcooL/graphql/query.go

132 lines
3.6 KiB
Go
Raw Normal View History

2017-10-03 21:43:55 +03:00
package graphql
import (
"bytes"
2018-01-22 13:07:50 +03:00
"encoding/json"
2017-10-03 21:43:55 +03:00
"io"
"reflect"
"sort"
"github.com/shurcooL/graphql/ident"
)
2018-01-22 13:07:50 +03:00
func constructQuery(v interface{}, variables map[string]interface{}) string {
query := query(v)
2017-10-03 21:43:55 +03:00
if variables != nil {
return "query(" + queryArguments(variables) + ")" + query
}
return query
}
2018-01-22 13:07:50 +03:00
func constructMutation(v interface{}, variables map[string]interface{}) string {
query := query(v)
2017-10-03 21:43:55 +03: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 {
2018-01-22 13:07:50 +03:00
// Sort keys in order to produce deterministic output for testing purposes.
// TODO: If tests can be made to work with non-deterministic output, then no need to sort.
keys := make([]string, 0, len(variables))
2017-10-03 21:43:55 +03:00
for k := range variables {
2018-01-22 13:07:50 +03:00
keys = append(keys, k)
2017-10-03 21:43:55 +03:00
}
2018-01-22 13:07:50 +03:00
sort.Strings(keys)
var buf bytes.Buffer
for _, k := range keys {
io.WriteString(&buf, "$")
io.WriteString(&buf, k)
io.WriteString(&buf, ":")
writeArgumentType(&buf, reflect.TypeOf(variables[k]), true)
// Don't insert a comma here.
// Commas in GraphQL are insignificant, and we want minified output.
// See https://facebook.github.io/graphql/October2016/#sec-Insignificant-Commas.
2017-10-03 21:43:55 +03:00
}
2018-01-22 13:07:50 +03:00
return buf.String()
2017-10-03 21:43:55 +03:00
}
2018-01-22 13:07:50 +03:00
// writeArgumentType writes a minified GraphQL type for t to w.
// value indicates whether t is a value (required) type or pointer (optional) type.
// If value is true, then "!" is written at the end of t.
func writeArgumentType(w io.Writer, t reflect.Type, value bool) {
if t.Kind() == reflect.Ptr {
// Pointer is an optional type, so no "!" at the end of the pointer's underlying type.
writeArgumentType(w, t.Elem(), false)
return
}
switch t.Kind() {
case reflect.Slice, reflect.Array:
// List. E.g., "[Int]".
io.WriteString(w, "[")
writeArgumentType(w, t.Elem(), true)
io.WriteString(w, "]")
default:
// Named type. E.g., "Int".
name := t.Name()
if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubql/issues/12.
name = "ID"
}
io.WriteString(w, name)
}
if value {
// Value is a required type, so add "!" to the end.
io.WriteString(w, "!")
}
2017-10-03 21:43:55 +03:00
}
2018-01-22 13:07:50 +03:00
// query uses writeQuery to recursively construct
2017-10-03 21:43:55 +03:00
// a minified query string from the provided struct v.
//
// E.g., struct{Foo Int, BarBaz *Boolean} -> "{foo,barBaz}".
2018-01-22 13:07:50 +03:00
func query(v interface{}) string {
2017-10-03 21:43:55 +03:00
var buf bytes.Buffer
2018-01-22 13:07:50 +03:00
writeQuery(&buf, reflect.TypeOf(v), false)
2017-10-03 21:43:55 +03:00
return buf.String()
}
2018-01-22 13:07:50 +03: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 writeQuery(w io.Writer, t reflect.Type, inline bool) {
2017-10-03 21:43:55 +03:00
switch t.Kind() {
case reflect.Ptr, reflect.Slice:
2018-01-22 13:07:50 +03:00
writeQuery(w, t.Elem(), false)
2017-10-03 21:43:55 +03:00
case reflect.Struct:
2018-01-22 13:07:50 +03:00
// If the type implements json.Unmarshaler, it's a scalar. Don't expand it.
if reflect.PtrTo(t).Implements(jsonUnmarshaler) {
return
2017-10-03 21:43:55 +03:00
}
if !inline {
io.WriteString(w, "{")
}
for i := 0; i < t.NumField(); i++ {
if i != 0 {
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 {
io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase())
}
}
2018-01-22 13:07:50 +03:00
writeQuery(w, f.Type, inlineField)
2017-10-03 21:43:55 +03:00
}
if !inline {
io.WriteString(w, "}")
}
}
}
2018-01-22 13:07:50 +03:00
var jsonUnmarshaler = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()