From 6d340b58c690658c9181ae7707b152e9495e3410 Mon Sep 17 00:00:00 2001 From: Vishnu Bharathi Date: Mon, 17 May 2021 10:22:41 +0530 Subject: [PATCH] cli: adds support for `graphql_schema_introspection` metadata object GitOrigin-RevId: a0c372bfa7ee0620bdd80d110da4b1e85e45f73f --- CHANGELOG.md | 1 + .../graphql_schema_introspection.go | 94 +++++++++++++ .../graphql_schema_introspection_test.go | 129 ++++++++++++++++++ .../testdata/metadata.json | 9 ++ .../graphql_schema_introspection.yaml | 2 + cli/internal/metadataobject/metadataobject.go | 2 + 6 files changed, 237 insertions(+) create mode 100644 cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection.go create mode 100644 cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection_test.go create mode 100644 cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata.json create mode 100644 cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata/graphql_schema_introspection.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 34769a57086..ae28c597659 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ # export metadata and write to stdout $ hasura metadata export -o json ``` +- cli: add support for `graphql_schema_introspection` metadata object ## v2.0.0-alpha.10 diff --git a/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection.go b/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection.go new file mode 100644 index 00000000000..1319076cda2 --- /dev/null +++ b/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection.go @@ -0,0 +1,94 @@ +package graphqlschemaintrospection + +import ( + "io/ioutil" + "path/filepath" + + "github.com/sirupsen/logrus" + + "github.com/hasura/graphql-engine/cli" + "gopkg.in/yaml.v2" +) + +const ( + MetadataFilename string = "graphql_schema_introspection.yaml" +) + +type MetadataObject struct { + MetadataDir string + + logger *logrus.Logger +} + +func New(ec *cli.ExecutionContext, baseDir string) *MetadataObject { + return &MetadataObject{ + MetadataDir: baseDir, + logger: ec.Logger, + } +} + +func (o *MetadataObject) Validate() error { + return nil +} + +func (o *MetadataObject) CreateFiles() error { + var v interface{} + data, err := yaml.Marshal(v) + if err != nil { + return err + } + err = ioutil.WriteFile(filepath.Join(o.MetadataDir, MetadataFilename), data, 0644) + if err != nil { + return err + } + return nil +} + +func (o *MetadataObject) Build(metadata *yaml.MapSlice) error { + data, err := ioutil.ReadFile(filepath.Join(o.MetadataDir, MetadataFilename)) + if err != nil { + return err + } + item := yaml.MapItem{ + Key: o.Name(), + } + var obj yaml.MapSlice + err = yaml.Unmarshal(data, &obj) + if err != nil { + return err + } + if len(obj) > 0 { + item.Value = obj + *metadata = append(*metadata, item) + } + return nil +} + +func (o *MetadataObject) Export(metadata yaml.MapSlice) (map[string][]byte, error) { + var graphqlSchemaIntroSpection interface{} + for _, item := range metadata { + k, ok := item.Key.(string) + if !ok || k != o.Name() { + continue + } + graphqlSchemaIntroSpection = item.Value + } + if graphqlSchemaIntroSpection == nil { + o.logger.WithFields(logrus.Fields{ + "object": o.Name(), + "reason": "not found in metadata", + }).Debugf("skipped building %s", o.Name()) + return nil, nil + } + data, err := yaml.Marshal(graphqlSchemaIntroSpection) + if err != nil { + return nil, err + } + return map[string][]byte{ + filepath.Join(o.MetadataDir, MetadataFilename): data, + }, nil +} + +func (o *MetadataObject) Name() string { + return "graphql_schema_introspection" +} diff --git a/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection_test.go b/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection_test.go new file mode 100644 index 00000000000..964d3649312 --- /dev/null +++ b/cli/internal/metadataobject/graphql_schema_introspection/graphql_schema_introspection_test.go @@ -0,0 +1,129 @@ +package graphqlschemaintrospection + +import ( + "io/ioutil" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" +) + +func TestMetadataObject_Build(t *testing.T) { + type fields struct { + MetadataDir string + logger *logrus.Logger + } + type args struct { + metadata *yaml.MapSlice + } + tests := []struct { + name string + fields fields + args args + want string + wantErr bool + }{ + { + "can build from file", + fields{ + MetadataDir: "testdata/metadata", + logger: logrus.New(), + }, + args{ + metadata: new(yaml.MapSlice), + }, + `graphql_schema_introspection: + disabled_for_roles: + - child +`, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + m := &MetadataObject{ + MetadataDir: tt.fields.MetadataDir, + logger: tt.fields.logger, + } + err := m.Build(tt.args.metadata) + if tt.wantErr { + require.Error(t, err) + } else { + b, err := yaml.Marshal(tt.args.metadata) + assert.NoError(t, err) + assert.Equal(t, tt.want, string(b)) + } + }) + } +} + +func TestMetadataObject_Export(t *testing.T) { + type fields struct { + MetadataDir string + logger *logrus.Logger + } + type args struct { + metadata yaml.MapSlice + } + tests := []struct { + name string + fields fields + args args + want map[string][]byte + wantErr bool + }{ + { + "can export metadata with graphql_schema_introspection", + fields{ + MetadataDir: "testdata/metadata", + logger: logrus.New(), + }, + args{ + metadata: func() yaml.MapSlice { + var metadata yaml.MapSlice + jsonb, err := ioutil.ReadFile("testdata/metadata.json") + assert.NoError(t, err) + assert.NoError(t, yaml.Unmarshal(jsonb, &metadata)) + return metadata + }(), + }, + map[string][]byte{ + "testdata/metadata/graphql_schema_introspection.yaml": []byte(`disabled_for_roles: +- child +`), + }, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + obj := &MetadataObject{ + MetadataDir: tt.fields.MetadataDir, + logger: tt.fields.logger, + } + got, err := obj.Export(tt.args.metadata) + if tt.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) + var wantContent = map[string]string{} + var gotContent = map[string]string{} + for k, v := range got { + gotContent[k] = string(v) + } + for k, v := range tt.want { + wantContent[k] = string(v) + } + assert.NoError(t, err) + assert.NoError(t, err) + if diff := cmp.Diff(wantContent, gotContent); diff != "" { + t.Errorf("Export() mismatch (-want +got):\n%s", diff) + } + } + }) + } +} diff --git a/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata.json b/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata.json new file mode 100644 index 00000000000..7c3222b2922 --- /dev/null +++ b/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata.json @@ -0,0 +1,9 @@ +{ + "version": 3, + "sources": [], + "graphql_schema_introspection": { + "disabled_for_roles": [ + "child" + ] + } +} \ No newline at end of file diff --git a/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata/graphql_schema_introspection.yaml b/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata/graphql_schema_introspection.yaml new file mode 100644 index 00000000000..c38bfedcc51 --- /dev/null +++ b/cli/internal/metadataobject/graphql_schema_introspection/testdata/metadata/graphql_schema_introspection.yaml @@ -0,0 +1,2 @@ +disabled_for_roles: +- child \ No newline at end of file diff --git a/cli/internal/metadataobject/metadataobject.go b/cli/internal/metadataobject/metadataobject.go index 53c3528de31..301531d794f 100644 --- a/cli/internal/metadataobject/metadataobject.go +++ b/cli/internal/metadataobject/metadataobject.go @@ -7,6 +7,7 @@ import ( apilimits "github.com/hasura/graphql-engine/cli/internal/metadataobject/api_limits" crontriggers "github.com/hasura/graphql-engine/cli/internal/metadataobject/cron_triggers" "github.com/hasura/graphql-engine/cli/internal/metadataobject/functions" + graphqlschemaintrospection "github.com/hasura/graphql-engine/cli/internal/metadataobject/graphql_schema_introspection" inheritedroles "github.com/hasura/graphql-engine/cli/internal/metadataobject/inherited_roles" "github.com/hasura/graphql-engine/cli/internal/metadataobject/querycollections" "github.com/hasura/graphql-engine/cli/internal/metadataobject/remoteschemas" @@ -64,6 +65,7 @@ func GetMetadataObjectsWithDir(ec *cli.ExecutionContext, dir ...string) Objects // hasura pro specific metadata objects objects = append(objects, apilimits.New(ec, metadataDir)) + objects = append(objects, graphqlschemaintrospection.New(ec, metadataDir)) } }