Skip to content

Commit

Permalink
fix: change cwd to load gqlgen config correctly (#103)
Browse files Browse the repository at this point in the history
Code copied from 99designs/gqlgen#1511
  • Loading branch information
giautm authored and a8m committed Jul 8, 2021
1 parent a52eb13 commit 0f41de6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions entgql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package entgql
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -31,7 +32,6 @@ import (
"github.com/graphql-go/graphql/language/printer"
"github.com/graphql-go/graphql/language/source"
"github.com/graphql-go/graphql/language/visitor"
gqlparserast "github.com/vektah/gqlparser/v2/ast"
)

type (
Expand Down Expand Up @@ -84,18 +84,28 @@ func WithSchemaPath(path string) ExtensionOption {
// Note that, enabling this option is recommended as it improves the
// GraphQL integration,
func WithConfigPath(path string) ExtensionOption {
return func(ex *Extension) error {
cfg, err := config.LoadConfig(path)
return func(ex *Extension) (err error) {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get working directory: %w", err)
}
if err := os.Chdir(filepath.Dir(path)); err != nil {
return fmt.Errorf("unable to enter config dir: %w", err)
}
defer func() {
if cerr := os.Chdir(cwd); cerr != nil {
err = fmt.Errorf("unable to restore working directory: %w", cerr)
}
}()
cfg, err := config.LoadConfig(filepath.Base(path))
if err != nil {
return err
}

if cfg.Schema == nil {
if err := cfg.LoadSchema(); err != nil {
return err
}
}

ex.cfg = cfg
return nil
}
Expand Down Expand Up @@ -271,11 +281,8 @@ func (e *Extension) hasMapping(f *gen.Field) (string, bool) {
// isInput reports if the given type is an input object.
func (e *Extension) isInput(name string) bool {
if t, ok := e.cfg.Schema.Types[name]; ok && t != nil {
return t.Kind == gqlparserast.Enum ||
t.Kind == gqlparserast.Scalar ||
t.Kind == gqlparserast.InputObject
return t.IsInputType()
}

return false
}

Expand Down

0 comments on commit 0f41de6

Please sign in to comment.