Skip to content

Commit

Permalink
cli: port metadataobject/query_collections to use internal/errors
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#6410
GitOrigin-RevId: d82eb90669f3a915f2a2fc9d87347ff64f4ee97b
  • Loading branch information
scriptonist authored and hasura-bot committed Oct 24, 2022
1 parent 792ef31 commit 5d75918
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
26 changes: 16 additions & 10 deletions cli/internal/metadataobject/querycollections/query_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"path/filepath"

"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -35,28 +36,30 @@ func (q *QueryCollectionConfig) Validate() error {
}

func (q *QueryCollectionConfig) CreateFiles() error {
var op errors.Op = "querycollections.QueryCollectionsConfig.CreateFiles"
v := make([]interface{}, 0)
buf := new(bytes.Buffer)
err := metadataobject.GetEncoder(buf).Encode(v)
if err != nil {
return err
return errors.E(op, err)
}
err = ioutil.WriteFile(filepath.Join(q.MetadataDir, q.Filename()), buf.Bytes(), 0644)
if err != nil {
return err
return errors.E(op, err)
}
return nil
}

func (q *QueryCollectionConfig) Build() (map[string]interface{}, error) {
var op errors.Op = "querycollections.QueryCollectionConfig.Build"
data, err := metadataobject.ReadMetadataFile(filepath.Join(q.MetadataDir, q.Filename()))
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
var obj []yaml.Node
err = yaml.Unmarshal(data, &obj)
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, errors.KindBadInput, q.error(err))
}
return map[string]interface{}{q.Key(): obj}, nil
}
Expand All @@ -75,17 +78,18 @@ type query struct {
}

func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[string][]byte, error) {
var op errors.Op = "querycollections.QueryCollectionConfig.Export"
var value interface{}
if v, ok := metadata[q.Key()]; !ok {
value = []yaml.Node{}
} else {
var collections []querycollection
bs, err := yaml.Marshal(v)
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
if err := yaml.Unmarshal(bs, &collections); err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
for collectionIdx := range collections {
for queryIdx := range collections[collectionIdx].Definition.Queries {
Expand All @@ -94,7 +98,7 @@ func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[strin
Input: collections[collectionIdx].Definition.Queries[queryIdx].Query,
})
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
gqlFormatter := formatter.NewFormatter(buf, formatter.WithIndent(" "))
gqlFormatter.FormatQueryDocument(queryDoc)
Expand All @@ -109,7 +113,7 @@ func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[strin
var buf bytes.Buffer
err := metadataobject.GetEncoder(&buf).Encode(value)
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
return map[string][]byte{
filepath.ToSlash(filepath.Join(q.BaseDirectory(), q.Filename())): buf.Bytes(),
Expand All @@ -125,18 +129,20 @@ func (q *QueryCollectionConfig) Filename() string {
}

func (q *QueryCollectionConfig) GetFiles() ([]string, error) {
var op errors.Op = "querycollections.QueryCollectionConfig.GetFiles"
rootFile := filepath.Join(q.BaseDirectory(), q.Filename())
files, err := metadataobject.DefaultGetFiles(rootFile)
if err != nil {
return nil, q.error(err)
return nil, errors.E(op, q.error(err))
}
return files, nil
}

func (q *QueryCollectionConfig) WriteDiff(opts metadataobject.WriteDiffOpts) error {
var op errors.Op = "querycollections.QueryCollectionConfig.WriteDiff"
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: q, WriteDiffOpts: opts})
if err != nil {
return q.error(err)
return errors.E(op, q.error(err))
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

Expand All @@ -22,6 +23,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) {
fields fields
wantGolden string
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"t1",
Expand All @@ -32,6 +34,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) {
},
"testdata/build_test/t1/want.golden.json",
false,
require.NoError,
},
{
"t2",
Expand All @@ -42,6 +45,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) {
},
"testdata/build_test/t2/want.golden.json",
false,
require.NoError,
},
}
for _, tt := range tests {
Expand All @@ -51,9 +55,8 @@ func TestQueryCollectionConfig_Build(t *testing.T) {
logger: tt.fields.logger,
}
got, err := q.Build()
if tt.wantErr {
assert.Error(t, err)
} else {
tt.assertErr(t, err)
if !tt.wantErr {
assert.NoError(t, err)
gotbs, err := yaml.Marshal(got)
assert.NoError(t, err)
Expand All @@ -80,12 +83,13 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
metadata map[string]yaml.Node
}
tests := []struct {
id string
name string
fields fields
args args
want map[string][]byte
wantErr bool
id string
name string
fields fields
args args
want map[string][]byte
wantErr bool
assertErr require.ErrorAssertionFunc
}{
{
"t1",
Expand Down Expand Up @@ -113,6 +117,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
}(),
},
false,
require.NoError,
},
{
"t2",
Expand Down Expand Up @@ -140,6 +145,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
}(),
},
false,
require.NoError,
},
{
"t3",
Expand Down Expand Up @@ -167,6 +173,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
}(),
},
false,
require.NoError,
},
{
"t4",
Expand Down Expand Up @@ -194,6 +201,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
}(),
},
false,
require.NoError,
},
{
"t5",
Expand All @@ -209,6 +217,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
"metadata/query_collections.yaml": readYamlFileAndEmitBytes(t, "testdata/export_test/t5/want.query_collections.yaml"),
},
false,
require.NoError,
},
}
for _, tt := range tests {
Expand All @@ -218,9 +227,8 @@ func TestQueryCollectionConfig_Export(t *testing.T) {
logger: tt.fields.logger,
}
got, err := q.Export(tt.args.metadata)
if tt.wantErr {
assert.Error(t, err)
} else {
tt.assertErr(t, err)
if !tt.wantErr {
assert.NoError(t, err)
for k, v := range got {
assert.Contains(t, tt.want, k)
Expand Down

0 comments on commit 5d75918

Please sign in to comment.