Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: fix bug when exporting metadata to remote_schemas.yaml #8921

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cli/internal/metadataobject/remoteschemas/remote_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package remoteschemas

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"

"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
"github.com/sirupsen/logrus"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/formatter"
"github.com/vektah/gqlparser/v2/parser"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func (r *RemoteSchemaConfig) CreateFiles() error {
if err := os.MkdirAll(filepath.Dir(path), 0744); err != nil {
return err
}
err = ioutil.WriteFile(path, buf.Bytes(), 0644)
err = os.WriteFile(path, buf.Bytes(), 0644)
if err != nil {
return err
}
Expand All @@ -69,7 +68,7 @@ func (r *RemoteSchemaConfig) Build() (map[string]interface{}, metadataobject.Err

type remoteSchema struct {
Name interface{} `yaml:"name,omitempty"`
Defintion yaml.Node `yaml:"definition,omitempty"`
Definition yaml.Node `yaml:"definition,omitempty"`
Comment yaml.Node `yaml:"comment,omitempty"`
Permissions []permission `yaml:"permissions,omitempty"`
RemoteRelationships yaml.Node `yaml:"remote_relationships,omitempty"`
Expand Down Expand Up @@ -102,15 +101,15 @@ func (r *RemoteSchemaConfig) Export(metadata map[string]yaml.Node) (map[string][
for pIdx := range remoteSchemas[rsIdx].Permissions {
buf := new(bytes.Buffer)
gqlFormatter := formatter.NewFormatter(buf, formatter.WithIndent(" "))
schema, err := gqlparser.LoadSchema(&ast.Source{
schema, err := parser.ParseSchema(&ast.Source{
Input: remoteSchemas[rsIdx].Permissions[pIdx].Definition.Schema,
})
if err != nil {
r.logger.Infof("formatting permission for role %v in remote schema %v failed", remoteSchemas[rsIdx].Permissions[pIdx].Role, remoteSchemas[rsIdx].Name)
r.logger.Debugf("loading schema failed for role: %v remote schema: %v error: %v", remoteSchemas[rsIdx].Permissions[pIdx].Role, remoteSchemas[rsIdx].Name, err)
continue
}
gqlFormatter.FormatSchema(schema)
gqlFormatter.FormatSchemaDocument(schema)
if buf.Len() > 0 {
remoteSchemas[rsIdx].Permissions[pIdx].Definition.Schema = buf.String()
}
Expand Down
28 changes: 14 additions & 14 deletions cli/internal/metadataobject/remoteschemas/remote_schemas_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package remoteschemas

import (
"io/ioutil"
"os"
"testing"

goyaml "github.com/goccy/go-yaml"
Expand Down Expand Up @@ -81,9 +81,9 @@ func TestRemoteSchemaConfig_Build(t *testing.T) {
assert.NoError(t, err)

// uncomment following lines to update golden file
// assert.NoError(t, ioutil.WriteFile(tt.wantGolden, jsonbs, os.ModePerm))
// assert.NoError(t, os.WriteFile(tt.wantGolden, jsonbs, os.ModePerm))

wantbs, err := ioutil.ReadFile(tt.wantGolden)
wantbs, err := os.ReadFile(tt.wantGolden)
assert.NoError(t, err)
assert.Equal(t, string(wantbs), string(jsonbs))
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t1/metadata.json")
bs, err := os.ReadFile("testdata/export_test/t1/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
Expand All @@ -127,7 +127,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
map[string][]byte{
"metadata/remote_schemas.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t1/want.remote_schemas.yaml")
bs, err := os.ReadFile("testdata/export_test/t1/want.remote_schemas.yaml")
assert.NoError(t, err)
return bs
}(),
Expand All @@ -144,7 +144,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t2/metadata.json")
bs, err := os.ReadFile("testdata/export_test/t2/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
Expand All @@ -155,7 +155,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
map[string][]byte{
"metadata/remote_schemas.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t2/want.remote_schemas.yaml")
bs, err := os.ReadFile("testdata/export_test/t2/want.remote_schemas.yaml")
assert.NoError(t, err)
return bs
}(),
Expand All @@ -172,7 +172,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t3/metadata.json")
bs, err := os.ReadFile("testdata/export_test/t3/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
Expand All @@ -183,7 +183,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
map[string][]byte{
"metadata/remote_schemas.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t3/want.remote_schemas.yaml")
bs, err := os.ReadFile("testdata/export_test/t3/want.remote_schemas.yaml")
assert.NoError(t, err)
return bs
}(),
Expand All @@ -199,7 +199,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t4/metadata.json")
bs, err := os.ReadFile("testdata/export_test/t4/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
Expand All @@ -210,7 +210,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
map[string][]byte{
"metadata/remote_schemas.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t4/want.remote_schemas.yaml")
bs, err := os.ReadFile("testdata/export_test/t4/want.remote_schemas.yaml")
assert.NoError(t, err)
return bs
}(),
Expand All @@ -226,7 +226,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
args{
metadata: func() map[string]yaml.Node {
bs, err := ioutil.ReadFile("testdata/export_test/t5/metadata.json")
bs, err := os.ReadFile("testdata/export_test/t5/metadata.json")
assert.NoError(t, err)
yamlbs, err := metadatautil.JSONToYAML(bs)
assert.NoError(t, err)
Expand All @@ -237,7 +237,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
},
map[string][]byte{
"metadata/remote_schemas.yaml": func() []byte {
bs, err := ioutil.ReadFile("testdata/export_test/t5/want.remote_schemas.yaml")
bs, err := os.ReadFile("testdata/export_test/t5/want.remote_schemas.yaml")
assert.NoError(t, err)
return bs
}(),
Expand All @@ -259,7 +259,7 @@ func TestRemoteSchemaConfig_Export(t *testing.T) {
for k, v := range got {
assert.Contains(t, tt.want, k)
// uncomment to update golden files
// assert.NoError(t, ioutil.WriteFile(fmt.Sprintf("testdata/export_test/%v/want.%v", tt.id, filepath.Base(k)), v, os.ModePerm))
// assert.NoError(t, os.WriteFile(fmt.Sprintf("testdata/export_test/%v/want.%v", tt.id, filepath.Base(k)), v, os.ModePerm))
assert.Equalf(t, string(tt.want[k]), string(v), "%v", k)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- role: user
definition:
schema: |
schema {
query: Query
}
type Continent {
code: ID!
countries: [Country!]!
Expand All @@ -29,20 +32,12 @@
phone: String!
states: [State!]!
}
input CountryFilterInput {
code: StringQueryOperatorInput
continent: StringQueryOperatorInput
currency: StringQueryOperatorInput
}
type Language {
code: ID!
name: String
native: String
rtl: Boolean!
}
input LanguageFilterInput {
code: StringQueryOperatorInput
}
type Query {
continent(code: ID!): Continent
countries(filter: CountryFilterInput): [Country!]!
Expand All @@ -53,6 +48,14 @@
country: Country!
name: String!
}
input CountryFilterInput {
code: StringQueryOperatorInput
continent: StringQueryOperatorInput
currency: StringQueryOperatorInput
}
input LanguageFilterInput {
code: StringQueryOperatorInput
}
input StringQueryOperatorInput {
eq: String
glob: String
Expand Down
Loading