Last active
November 13, 2020 04:18
-
-
Save maoueh/b07e3eee0b1dd653939e0aeca0311f01 to your computer and use it in GitHub Desktop.
A `*query.Document` printer for https://github.com/graph-gophers/graphql-go library (internal work)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package query | |
// Goes in internal/query/print.go | |
import "fmt" | |
func PrintGraphQLDocument(document *Document) { | |
if document == nil { | |
fmt.Println("Document <nil>") | |
return | |
} | |
for i, op := range document.Operations { | |
if i != 0 { | |
fmt.Println("") | |
} | |
PrintOperation(op) | |
} | |
} | |
func PrintOperation(op *Operation) { | |
if op == nil { | |
fmt.Println("Operation <nil>") | |
return | |
} | |
var printSelection func(string, Selection) | |
printSelection = func(indent string, sel Selection) { | |
fmt.Println(indent + "Selection") | |
switch v := sel.(type) { | |
case *Field: | |
fmt.Printf(indent+" Field %s (%s)\n", v.Name.Name, v.Alias.Name) | |
for _, subSel := range v.Selections { | |
printSelection(indent+" ", subSel) | |
} | |
fmt.Println("End") | |
case *InlineFragment: | |
fmt.Println(indent+" InlineFragment", v.Fragment.On.Name) | |
case *FragmentSpread: | |
fmt.Println(indent+" FragmentSpread", v.Name.Name) | |
default: | |
fmt.Printf(indent+" Unknown %T\n", v) | |
} | |
fmt.Println(indent + "End") | |
} | |
fmt.Println("Operation", op.Name.Name, op.Type) | |
for _, sel := range op.Selections { | |
printSelection(" ", sel) | |
} | |
fmt.Println("End") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment