Skip to content

Commit

Permalink
Fix panic on gateway.Query() with a nil QueryInput.QueryDocument (#195)
Browse files Browse the repository at this point in the history
This can occur when calling graphql.IntrospectAPI() on the gateway itself.
  • Loading branch information
JohnStarich authored Jun 8, 2023
1 parent 99fd083 commit 4f5f377
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (

"github.com/99designs/gqlgen/graphql/introspection"
"github.com/mitchellh/mapstructure"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"

"github.com/nautilus/graphql"
)
Expand Down Expand Up @@ -48,6 +50,18 @@ func (g *Gateway) Query(ctx context.Context, input *graphql.QueryInput, receiver
// wrap the schema in something capable of introspection
introspectionSchema := introspection.WrapSchema(g.schema)

if input.QueryDocument == nil && input.Query != "" {
internalSchema, err := g.internalSchema()
if err != nil {
return err
}
var loadErr gqlerror.List
input.QueryDocument, loadErr = gqlparser.LoadQuery(internalSchema, input.Query)
if len(loadErr) > 0 {
return loadErr
}
}

// for local stuff we don't care about fragment directives
querySelection, err := graphql.ApplyFragments(input.QueryDocument.Operations[0].SelectionSet, input.QueryDocument.Fragments)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,48 @@ func TestSchemaIntrospection_missingType(t *testing.T) {
assert.Nil(t, result.Type)
}

func TestSchemaIntrospection_missingQueryDocument(t *testing.T) {
t.Parallel()
schema, err := graphql.LoadSchema("")
require.NoError(t, err)
gateway, err := New([]*graphql.RemoteSchema{
{Schema: schema, URL: "url1"},
})
require.NoError(t, err)

t.Run("valid query", func(t *testing.T) {
t.Parallel()
var result interface{}
err := gateway.Query(context.Background(), &graphql.QueryInput{
Query: `
{
__type(name: "Query") {
name
}
}
`,
QueryDocument: nil,
}, &result)
assert.NoError(t, err)
name := "Query"
assert.Equal(t, map[string]interface{}{
"__type": map[string]interface{}{
"name": &name,
},
}, result)
})

t.Run("invalid query", func(t *testing.T) {
t.Parallel()
var result interface{}
err := gateway.Query(context.Background(), &graphql.QueryInput{
Query: "garbage",
QueryDocument: nil,
}, &result)
assert.EqualError(t, err, "input:1: Unexpected Name \"garbage\"\n")
})
}

func TestSchema_resolveNodeInlineID(t *testing.T) {
t.Parallel()
type Result struct {
Expand Down

0 comments on commit 4f5f377

Please sign in to comment.