feat: re-format graphql queries saved in allow.list

This commit is contained in:
Vikram Rangnekar 2020-05-22 00:12:17 -04:00
parent 2acb05741e
commit f7d3760af7
1 changed files with 19 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/chirino/graphql/schema"
"github.com/dosco/super-graph/jsn" "github.com/dosco/super-graph/jsn"
) )
@ -140,6 +141,7 @@ func (al *List) Set(vars []byte, query, comment string) error {
func (al *List) Load() ([]Item, error) { func (al *List) Load() ([]Item, error) {
var list []Item var list []Item
varString := "variables"
b, err := ioutil.ReadFile(al.filepath) b, err := ioutil.ReadFile(al.filepath)
if err != nil { if err != nil {
@ -180,9 +182,9 @@ func (al *List) Load() ([]Item, error) {
s = e s = e
} }
ty = AL_QUERY ty = AL_QUERY
} else if matchPrefix(b, e, "variables") { } else if matchPrefix(b, e, varString) {
if c == 0 { if c == 0 {
s = e + len("variables") + 1 s = e + len(varString) + 1
} }
ty = AL_VARS ty = AL_VARS
} else if b[e] == '{' { } else if b[e] == '{' {
@ -234,7 +236,21 @@ func (al *List) Load() ([]Item, error) {
func (al *List) save(item Item) error { func (al *List) save(item Item) error {
var buf bytes.Buffer var buf bytes.Buffer
item.Name = QueryName(item.Query) qd := &schema.QueryDocument{}
if err := qd.Parse(item.Query); err != nil {
fmt.Println("##", item.Query)
return err
}
qd.WriteTo(&buf)
query := buf.String()
buf.Reset()
// fmt.Println(">", query)
item.Name = QueryName(query)
item.key = strings.ToLower(item.Name) item.key = strings.ToLower(item.Name)
if len(item.Name) == 0 { if len(item.Name) == 0 {