Skip to content

Commit

Permalink
Fix nil pointer dereference when an invalid import is bound to a mod…
Browse files Browse the repository at this point in the history
…el (#1676)

* Fixes remaining Name field in singlefile test

* Fixes nill pointer dereference when an invalid import is bound to a model

* Only return error if we failed to find type

* Revert "Fixes remaining Name field in singlefile test"

This reverts commit e43ebf7.

* Undo change of log.Println -> fmt.Println

Totally accidental, sorry!

Co-authored-by: Giuliano Oliveira <giuliano.programador@gmail.com>
  • Loading branch information
johnmaguire and giuliano-macedo authored Oct 27, 2021
1 parent 6c65e8f commit 87f9e43
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/vektah/gqlparser/v2/ast"
)

var ErrTypeNotFound = errors.New("unable to find type")

// Binder connects graphql types to golang types using static analysis
type Binder struct {
pkgs *code.Packages
Expand Down Expand Up @@ -148,7 +150,7 @@ func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, erro
}
}

return nil, fmt.Errorf("unable to find type %s\n", fullName)
return nil, fmt.Errorf("%w: %s", ErrTypeNotFound, fullName)
}

func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
Expand Down
4 changes: 4 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codegen

import (
"errors"
"fmt"
"go/types"
"log"
Expand Down Expand Up @@ -64,6 +65,9 @@ func (b *builder) buildField(obj *Object, field *ast.FieldDefinition) (*Field, e

if err = b.bindField(obj, &f); err != nil {
f.IsResolver = true
if errors.Is(err, config.ErrTypeNotFound) {
return nil, err
}
log.Println(err.Error())
}

Expand Down
12 changes: 12 additions & 0 deletions plugin/resolvergen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ func TestLayoutFollowSchemaWithCustomFilename(t *testing.T) {
require.Contains(t, source, "// AUserHelperFunction implementation")
}

func TestLayoutInvalidModelPath(t *testing.T) {

cfg, err := config.LoadConfig("testdata/invalid_model_path/gqlgen.yml")
require.NoError(t, err)

require.NoError(t, cfg.Init())

_, err = codegen.BuildData(cfg)
require.Error(t, err)

}

func testFollowSchemaPersistence(t *testing.T, dir string) {
_ = syscall.Unlink(dir + "/out/resolver.go")

Expand Down
6 changes: 6 additions & 0 deletions plugin/resolvergen/testdata/invalid_model_path/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema:
- "testdata/schema.graphql"

models:
Resolver:
model: github.com/99designs/invalid/invalid/invalid/nope.Resolver

0 comments on commit 87f9e43

Please sign in to comment.