From 79ba1283b97260f686ad9a4956c1bf28cf831886 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Thu, 8 Jul 2021 17:59:46 +0000 Subject: [PATCH] fix: change cwd to load gqlgen config correctly Code copied from https://github.com/99designs/gqlgen/pull/1511 --- entgql/extension.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/entgql/extension.go b/entgql/extension.go index 4f542cf50..5f9cf7feb 100644 --- a/entgql/extension.go +++ b/entgql/extension.go @@ -17,6 +17,7 @@ package entgql import ( "fmt" "io/ioutil" + "os" "path/filepath" "strings" @@ -85,7 +86,21 @@ func WithSchemaPath(path string) ExtensionOption { // GraphQL integration, func WithConfigPath(path string) ExtensionOption { return func(ex *Extension) error { - cfg, err := config.LoadConfig(path) + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("unable to get working directory: %w", err) + } + err = os.Chdir(filepath.Dir(path)) + if 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 }