-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.go
40 lines (32 loc) · 970 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"bytes"
"fmt"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/formatter"
"github.com/vektah/gqlparser/v2/parser"
)
func process(filename string, src []byte, opt *Options) ([]byte, error) {
source := &ast.Source{Name: filename, Input: string(src)}
query, err := parser.ParseQuery(source)
if err == nil {
return queryFormat(query), nil
}
schema, err := parser.ParseSchema(source)
if err == nil {
return schemaFormat(schema), nil
}
return nil, fmt.Errorf("%v is not GraphQL file: %w", filename, err)
}
func queryFormat(queryDocument *ast.QueryDocument) []byte {
var buf bytes.Buffer
astFormatter := formatter.NewFormatter(&buf)
astFormatter.FormatQueryDocument(queryDocument)
return buf.Bytes()
}
func schemaFormat(schemaDocument *ast.SchemaDocument) []byte {
var buf bytes.Buffer
astFormatter := formatter.NewFormatter(&buf)
astFormatter.FormatSchemaDocument(schemaDocument)
return buf.Bytes()
}