2020-02-24 19:14:46 +03:00
|
|
|
package querycollections
|
|
|
|
|
|
|
|
import (
|
2022-03-10 11:12:55 +03:00
|
|
|
"bytes"
|
2020-02-24 19:14:46 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-03-10 11:12:55 +03:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-31 17:01:47 +03:00
|
|
|
|
|
|
|
"github.com/vektah/gqlparser/ast"
|
|
|
|
"github.com/vektah/gqlparser/formatter"
|
|
|
|
"github.com/vektah/gqlparser/parser"
|
2020-02-24 19:14:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type QueryCollectionConfig struct {
|
|
|
|
MetadataDir string
|
|
|
|
|
|
|
|
logger *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ec *cli.ExecutionContext, baseDir string) *QueryCollectionConfig {
|
|
|
|
return &QueryCollectionConfig{
|
|
|
|
MetadataDir: baseDir,
|
|
|
|
logger: ec.Logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryCollectionConfig) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryCollectionConfig) CreateFiles() error {
|
|
|
|
v := make([]interface{}, 0)
|
2022-03-10 11:12:55 +03:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := metadataobject.GetEncoder(buf).Encode(v)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(q.MetadataDir, q.Filename()), buf.Bytes(), 0644)
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
func (q *QueryCollectionConfig) Build() (map[string]interface{}, metadataobject.ErrParsingMetadataObject) {
|
2021-07-23 12:49:44 +03:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join(q.MetadataDir, q.Filename()))
|
2020-02-24 19:14:46 +03:00
|
|
|
if err != nil {
|
2022-03-10 11:12:55 +03:00
|
|
|
return nil, q.error(err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
var obj []yaml.Node
|
2020-02-24 19:14:46 +03:00
|
|
|
err = yaml.Unmarshal(data, &obj)
|
|
|
|
if err != nil {
|
2022-03-10 11:12:55 +03:00
|
|
|
return nil, q.error(err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
return map[string]interface{}{q.Key(): obj}, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 17:01:47 +03:00
|
|
|
type querycollection struct {
|
|
|
|
Name yaml.Node `yaml:"name,omitempty"`
|
|
|
|
Definition definition `yaml:"definition,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type definition struct {
|
|
|
|
Queries []query `yaml:"queries,omitempty"`
|
|
|
|
}
|
|
|
|
type query struct {
|
|
|
|
Name yaml.Node `yaml:"name,omitempty"`
|
|
|
|
Query string `yaml:"query,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
|
2022-03-31 17:01:47 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := yaml.Unmarshal(bs, &collections); err != nil {
|
|
|
|
return nil, q.error(err)
|
|
|
|
}
|
|
|
|
for collectionIdx := range collections {
|
|
|
|
for queryIdx := range collections[collectionIdx].Definition.Queries {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
queryDoc, err := parser.ParseQuery(&ast.Source{
|
|
|
|
Input: collections[collectionIdx].Definition.Queries[queryIdx].Query,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, q.error(err)
|
|
|
|
}
|
|
|
|
gqlFormatter := formatter.NewFormatter(buf)
|
|
|
|
gqlFormatter.FormatQueryDocument(queryDoc)
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
collections[collectionIdx].Definition.Queries[queryIdx].Query = buf.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = collections
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := metadataobject.GetEncoder(&buf).Encode(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, q.error(err)
|
|
|
|
}
|
|
|
|
return map[string][]byte{
|
|
|
|
filepath.ToSlash(filepath.Join(q.BaseDirectory(), q.Filename())): buf.Bytes(),
|
|
|
|
}, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (q *QueryCollectionConfig) Key() string {
|
2022-03-10 11:12:55 +03:00
|
|
|
return metadataobject.QueryCollectionsKey
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (q *QueryCollectionConfig) Filename() string {
|
|
|
|
return "query_collections.yaml"
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:25:54 +03:00
|
|
|
func (q *QueryCollectionConfig) GetFiles() ([]string, metadataobject.ErrParsingMetadataObject) {
|
|
|
|
rootFile := filepath.Join(q.BaseDirectory(), q.Filename())
|
|
|
|
files, err := metadataobject.DefaultGetFiles(rootFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, q.error(err)
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryCollectionConfig) WriteDiff(opts metadataobject.WriteDiffOpts) metadataobject.ErrParsingMetadataObject {
|
|
|
|
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: q, WriteDiffOpts: opts})
|
|
|
|
if err != nil {
|
|
|
|
return q.error(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *QueryCollectionConfig) BaseDirectory() string {
|
|
|
|
return q.MetadataDir
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
func (q *QueryCollectionConfig) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject {
|
|
|
|
return metadataobject.NewErrParsingMetadataObject(q, err, additionalContext...)
|
2021-06-18 20:38:29 +03:00
|
|
|
}
|