From 35a904829a89e9d29d1404ffed882cc5eda64ccf Mon Sep 17 00:00:00 2001 From: Benjamin Boudreau Date: Sun, 5 Apr 2020 09:30:24 -0400 Subject: [PATCH] Add LoadDefaultConfig to load the schema by default --- cmd/gen.go | 6 +++-- codegen/config/config.go | 19 ++++++++++++++ codegen/config/config_test.go | 26 ++++++++++++++++++- .../testdata/defaultconfig/schema.graphql | 0 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 codegen/config/testdata/defaultconfig/schema.graphql diff --git a/cmd/gen.go b/cmd/gen.go index d2ec8ba3452..b875bb43d02 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -27,8 +27,10 @@ var genCmd = &cli.Command{ } else { cfg, err = config.LoadConfigFromDefaultLocations() if os.IsNotExist(errors.Cause(err)) { - cfg = config.DefaultConfig() - } else if err != nil { + cfg, err = config.LoadDefaultConfig() + } + + if err != nil { return err } } diff --git a/codegen/config/config.go b/codegen/config/config.go index af9a610a175..ba939fcf59a 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -49,6 +49,25 @@ func DefaultConfig() *Config { } } +// LoadDefaultConfig loads the default config so that it is ready to be used +func LoadDefaultConfig() (*Config, error) { + config := DefaultConfig() + + for _, filename := range config.SchemaFilename { + filename = filepath.ToSlash(filename) + var err error + var schemaRaw []byte + schemaRaw, err = ioutil.ReadFile(filename) + if err != nil { + return nil, errors.Wrap(err, "unable to open schema") + } + + config.Sources = append(config.Sources, &ast.Source{Name: filename, Input: string(schemaRaw)}) + } + + return config, nil +} + // LoadConfigFromDefaultLocations looks for a config file in the current directory, and all parent directories // walking up the tree. The closest config file will be returned. func LoadConfigFromDefaultLocations() (*Config, error) { diff --git a/codegen/config/config_test.go b/codegen/config/config_test.go index dbea2811b68..b16e90c11a5 100644 --- a/codegen/config/config_test.go +++ b/codegen/config/config_test.go @@ -6,6 +6,7 @@ import ( "runtime" "testing" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/vektah/gqlparser/v2" @@ -53,7 +54,7 @@ func TestLoadConfig(t *testing.T) { }) } -func TestLoadDefaultConfig(t *testing.T) { +func TestLoadConfigFromDefaultLocation(t *testing.T) { testDir, err := os.Getwd() require.NoError(t, err) var cfg *Config @@ -85,6 +86,29 @@ func TestLoadDefaultConfig(t *testing.T) { }) } +func TestLoadDefaultConfig(t *testing.T) { + testDir, err := os.Getwd() + require.NoError(t, err) + var cfg *Config + + t.Run("will find the schema", func(t *testing.T) { + err = os.Chdir(filepath.Join(testDir, "testdata", "defaultconfig")) + require.NoError(t, err) + + cfg, err = LoadDefaultConfig() + require.NoError(t, err) + require.NotEmpty(t, cfg.Sources) + }) + + t.Run("will return error if schema doesn't exist", func(t *testing.T) { + err = os.Chdir(testDir) + require.NoError(t, err) + + cfg, err = LoadDefaultConfig() + require.True(t, os.IsNotExist(errors.Cause(err))) + }) +} + func TestReferencedPackages(t *testing.T) { t.Run("valid", func(t *testing.T) { tm := TypeMap{ diff --git a/codegen/config/testdata/defaultconfig/schema.graphql b/codegen/config/testdata/defaultconfig/schema.graphql new file mode 100644 index 00000000000..e69de29bb2d