From e4986d42d4012cc77a5c8edaafd0cfb06481c86a Mon Sep 17 00:00:00 2001 From: Alec Aivazis Date: Mon, 23 Mar 2020 04:01:27 -0700 Subject: [PATCH] updated imports (#7) --- format.go | 66 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++- go.sum | 14 +++++++++ graphql.go | 4 +-- introspection.go | 2 +- introspection_test.go | 2 +- language.go | 11 +++----- language_test.go | 2 +- printer.go | 2 +- printer_test.go | 2 +- queryer.go | 2 +- 11 files changed, 96 insertions(+), 16 deletions(-) create mode 100755 format.go mode change 100644 => 100755 go.sum diff --git a/format.go b/format.go new file mode 100755 index 0000000..811f1d9 --- /dev/null +++ b/format.go @@ -0,0 +1,66 @@ +package graphql + +import ( + "fmt" + "strings" + + "github.com/vektah/gqlparser/v2/ast" +) + +func formatIndentPrefix(level int) string { + acc := "\n" + // build up the prefix + for i := 0; i <= level; i++ { + acc += " " + } + + return acc +} +func formatSelectionSelectionSet(level int, selectionSet ast.SelectionSet) string { + acc := " {" + // and any sub selection + acc += formatSelection(level+1, selectionSet) + acc += formatIndentPrefix(level) + "}" + + return acc +} + +func formatSelection(level int, selectionSet ast.SelectionSet) string { + acc := "" + + for _, selection := range selectionSet { + acc += formatIndentPrefix(level) + switch selection := selection.(type) { + case *ast.Field: + // add the field name + acc += selection.Name + if len(selection.SelectionSet) > 0 { + acc += formatSelectionSelectionSet(level, selection.SelectionSet) + } + case *ast.InlineFragment: + // print the fragment name + acc += fmt.Sprintf("... on %v", selection.TypeCondition) + + formatSelectionSelectionSet(level, selection.SelectionSet) + case *ast.FragmentSpread: + // print the fragment name + acc += "..." + selection.Name + } + } + + return acc +} + +// FormatSelectionSet returns a pretty printed version of a selection set +func FormatSelectionSet(selection ast.SelectionSet) string { + acc := "{" + + insides := formatSelection(0, selection) + + if strings.TrimSpace(insides) != "" { + acc += insides + "\n}" + } else { + acc += "}" + } + + return acc +} diff --git a/go.mod b/go.mod index 7e2b77b..26f3094 100755 --- a/go.mod +++ b/go.mod @@ -6,7 +6,10 @@ require ( github.com/graphql-go/graphql v0.7.7 github.com/mitchellh/mapstructure v1.1.2 github.com/opentracing/opentracing-go v1.0.2 // indirect - github.com/stretchr/testify v1.3.0 + github.com/stretchr/testify v1.4.0 github.com/vektah/gqlparser v1.1.0 + github.com/vektah/gqlparser/v2 v2.0.1 golang.org/x/net v0.0.0-20190213061140-3a22650c66bd // indirect ) + +go 1.13 diff --git a/go.sum b/go.sum old mode 100644 new mode 100755 index 6ded1af..8894750 --- a/go.sum +++ b/go.sum @@ -5,10 +5,15 @@ github.com/carted/graphql v0.7.6 h1:1DAom3l7Irln/hHlPMBR6w/RirCXjopsCY9WCZc7WUc= github.com/carted/graphql v0.7.6/go.mod h1:aIVByVaa4avHzEnahcnHbP48OrkT/+gTtxBT+dPV2R0= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/graph-gophers/dataloader v5.0.0+incompatible h1:R+yjsbrNq1Mo3aPG+Z/EKYrXrXXUNJHOgbRt+U6jOug= github.com/graph-gophers/dataloader v5.0.0+incompatible/go.mod h1:jk4jk0c5ZISbKaMe8WsVopGB5/15GvGHMdMdPtwlRp4= github.com/graphql-go/graphql v0.7.7 h1:nwEsJGwPq9N6cElOO+NYyoWuELAQZ4GuJks0Rlco5og= github.com/graphql-go/graphql v0.7.7/go.mod h1:k6yrAYQaSP59DC5UVxbgxESlmVyojThKdORUqGDGmrI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= @@ -16,13 +21,22 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/vektah/gqlparser v1.1.0 h1:3668p2gUlO+PiS81x957Rpr3/FPRWG6cxgCXAvTS1hw= github.com/vektah/gqlparser v1.1.0/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw= +github.com/vektah/gqlparser v1.3.1 h1:8b0IcD3qZKWJQHSzynbDlrtP3IxVydZ2DZepCGofqfU= +github.com/vektah/gqlparser/v2 v2.0.1 h1:xgl5abVnsd4hkN9rk65OJID9bfcLSMuTaTcZj777q1o= +github.com/vektah/gqlparser/v2 v2.0.1/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/graphql.go b/graphql.go index d35777a..3caa9f1 100644 --- a/graphql.go +++ b/graphql.go @@ -1,8 +1,8 @@ package graphql import ( - "github.com/vektah/gqlparser" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2" + "github.com/vektah/gqlparser/v2/ast" ) // LoadSchema takes an SDL string and returns the parsed version diff --git a/introspection.go b/introspection.go index 493a6ac..4d0a5e2 100644 --- a/introspection.go +++ b/introspection.go @@ -5,7 +5,7 @@ import ( "errors" "fmt" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) // IntrospectRemoteSchema is used to build a RemoteSchema by firing the introspection query diff --git a/introspection_test.go b/introspection_test.go index fafe04f..3f0023f 100644 --- a/introspection_test.go +++ b/introspection_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) func TestIntrospectQuery_savesQueryType(t *testing.T) { diff --git a/language.go b/language.go index aab52fa..245e239 100644 --- a/language.go +++ b/language.go @@ -3,7 +3,7 @@ package graphql import ( "fmt" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) // CollectedField is a representations of a field with the list of selection sets that @@ -43,17 +43,14 @@ func (c *CollectedFieldList) GetOrCreateForAlias(alias string, creator func() *C // ApplyFragments takes a list of selections and merges them into one, embedding any fragments it // runs into along the way func ApplyFragments(selectionSet ast.SelectionSet, fragmentDefs ast.FragmentDefinitionList) (ast.SelectionSet, error) { - // build up a list of selection sets - final := ast.SelectionSet{} - - // look for all of the collected fields + // apply the fragments collectedFields, err := collectFields([]ast.SelectionSet{selectionSet}, fragmentDefs) if err != nil { return nil, err } - // the final result of collecting fields should have a single selection in its selection set - // which should be a selection for the same field referenced by collected.Field + // turn the CollectedField list into a selection set + final := ast.SelectionSet{} for _, collected := range *collectedFields { final = append(final, collected.Field) } diff --git a/language_test.go b/language_test.go index b7d787e..62d2385 100644 --- a/language_test.go +++ b/language_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) func TestApplyFragments_mergesFragments(t *testing.T) { diff --git a/printer.go b/printer.go index 77d4093..905c112 100644 --- a/printer.go +++ b/printer.go @@ -7,7 +7,7 @@ import ( "github.com/carted/graphql/language/printer" gAst "github.com/graphql-go/graphql/language/ast" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) // PrintQuery creates a string representation of an operation diff --git a/printer_test.go b/printer_test.go index 20039ae..c17ff1a 100644 --- a/printer_test.go +++ b/printer_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) func TestPrintQuery(t *testing.T) { diff --git a/queryer.go b/queryer.go index a0d8dd1..8783d00 100755 --- a/queryer.go +++ b/queryer.go @@ -9,7 +9,7 @@ import ( "net/http" "reflect" - "github.com/vektah/gqlparser/ast" + "github.com/vektah/gqlparser/v2/ast" ) // RemoteSchema encapsulates a particular schema that can be executed by sending network requests to the