put config-file-relative paths in exported queries (and error messages)
This commit is contained in:
+13
-4
@@ -70,16 +70,25 @@ type Config struct {
|
||||
// getter function, global var, or a context-key-type?
|
||||
// TODO: what if you want to return err?
|
||||
ClientGetter string `yaml:"client_getter"`
|
||||
|
||||
// Set automatically to the filename of the config file itself.
|
||||
configFilename string
|
||||
}
|
||||
|
||||
// BaseDir returns the directory of the config-file (relative to which
|
||||
// all the other paths are resolved).
|
||||
func (c *Config) BaseDir() string {
|
||||
return filepath.Dir(c.configFilename)
|
||||
}
|
||||
|
||||
func (c *Config) ValidateAndFillDefaults(configFilename string) error {
|
||||
c.configFilename = configFilename
|
||||
// Make paths relative to config dir
|
||||
configDir := filepath.Dir(configFilename)
|
||||
c.Schema = filepath.Join(configDir, c.Schema)
|
||||
c.Schema = filepath.Join(c.BaseDir(), c.Schema)
|
||||
for i := range c.Operations {
|
||||
c.Operations[i] = filepath.Join(configDir, c.Operations[i])
|
||||
c.Operations[i] = filepath.Join(c.BaseDir(), c.Operations[i])
|
||||
}
|
||||
c.Generated = filepath.Join(configDir, c.Generated)
|
||||
c.Generated = filepath.Join(c.BaseDir(), c.Generated)
|
||||
|
||||
if c.Package == "" {
|
||||
abs, err := filepath.Abs(c.Generated)
|
||||
|
||||
@@ -159,7 +159,7 @@ func Generate(config *Config) (map[string][]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
document, err := getAndValidateQueries(config.Operations, schema)
|
||||
document, err := getAndValidateQueries(config.BaseDir(), config.Operations, schema)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+14
-8
@@ -33,8 +33,8 @@ func getSchema(filename string) (*ast.Schema, error) {
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
func getAndValidateQueries(filenames []string, schema *ast.Schema) (*ast.QueryDocument, error) {
|
||||
queryDoc, err := getQueries(filenames)
|
||||
func getAndValidateQueries(basedir string, filenames []string, schema *ast.Schema) (*ast.QueryDocument, error) {
|
||||
queryDoc, err := getQueries(basedir, filenames)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func getAndValidateQueries(filenames []string, schema *ast.Schema) (*ast.QueryDo
|
||||
return queryDoc, nil
|
||||
}
|
||||
|
||||
func getQueries(filenames []string) (*ast.QueryDocument, error) {
|
||||
func getQueries(basedir string, filenames []string) (*ast.QueryDocument, error) {
|
||||
// We merge all the queries into a single query-document, since operations
|
||||
// in one might reference fragments in another.
|
||||
//
|
||||
@@ -77,7 +77,7 @@ func getQueries(filenames []string) (*ast.QueryDocument, error) {
|
||||
|
||||
switch filepath.Ext(filename) {
|
||||
case ".graphql":
|
||||
queryDoc, err := getQueriesFromString(string(text), filename)
|
||||
queryDoc, err := getQueriesFromString(string(text), basedir, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func getQueries(filenames []string) (*ast.QueryDocument, error) {
|
||||
addQueryDoc(queryDoc)
|
||||
|
||||
case ".go":
|
||||
queryDocs, err := getQueriesFromGo(string(text), filename)
|
||||
queryDocs, err := getQueriesFromGo(string(text), basedir, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -102,7 +102,13 @@ func getQueries(filenames []string) (*ast.QueryDocument, error) {
|
||||
return mergedQueryDoc, nil
|
||||
}
|
||||
|
||||
func getQueriesFromString(text string, filename string) (*ast.QueryDocument, error) {
|
||||
func getQueriesFromString(text string, basedir, filename string) (*ast.QueryDocument, error) {
|
||||
// make path relative to the config-directory
|
||||
relname, err := filepath.Rel(basedir, filename)
|
||||
if err == nil {
|
||||
filename = relname
|
||||
}
|
||||
|
||||
// Cf. gqlparser.LoadQuery
|
||||
document, graphqlError := parser.ParseQuery(
|
||||
&ast.Source{Name: filename, Input: text})
|
||||
@@ -113,7 +119,7 @@ func getQueriesFromString(text string, filename string) (*ast.QueryDocument, err
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func getQueriesFromGo(text string, filename string) ([]*ast.QueryDocument, error) {
|
||||
func getQueriesFromGo(text string, basedir, filename string) ([]*ast.QueryDocument, error) {
|
||||
fset := goToken.NewFileSet()
|
||||
f, err := goParser.ParseFile(fset, filename, text, 0)
|
||||
if err != nil {
|
||||
@@ -143,7 +149,7 @@ func getQueriesFromGo(text string, filename string) ([]*ast.QueryDocument, error
|
||||
|
||||
fakeFilename := fset.Position(basicLit.Pos()).String()
|
||||
var query *ast.QueryDocument
|
||||
query, err = getQueriesFromString(value, fakeFilename)
|
||||
query, err = getQueriesFromString(value, basedir, fakeFilename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
+16
-12
@@ -19,6 +19,20 @@ func sortQueries(queryDoc *ast.QueryDocument) {
|
||||
})
|
||||
}
|
||||
|
||||
func getTestQueries(t *testing.T, ext string) *ast.QueryDocument {
|
||||
graphqlQueries, err := getQueries(
|
||||
parseDataDir, []string{filepath.Join(parseDataDir, "*."+ext)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The different file-types may have the operations/fragments in a
|
||||
// different order.
|
||||
sortQueries(graphqlQueries)
|
||||
|
||||
return graphqlQueries
|
||||
}
|
||||
|
||||
// TestParse tests that query-extraction from different language source files
|
||||
// produces equivalent results. We do not test the results it produces (that's
|
||||
// covered by TestGenerate), just that they are equivalent in different
|
||||
@@ -26,10 +40,7 @@ func sortQueries(queryDoc *ast.QueryDocument) {
|
||||
func TestParse(t *testing.T) {
|
||||
extensions := []string{"go"}
|
||||
|
||||
graphqlQueries, err := getQueries([]string{filepath.Join(parseDataDir, "*.graphql")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
graphqlQueries := getTestQueries(t, "graphql")
|
||||
|
||||
// check it's at least non-empty
|
||||
if len(graphqlQueries.Operations) == 0 || len(graphqlQueries.Fragments) == 0 {
|
||||
@@ -40,14 +51,7 @@ func TestParse(t *testing.T) {
|
||||
|
||||
for _, ext := range extensions {
|
||||
t.Run(ext, func(t *testing.T) {
|
||||
queries, err := getQueries([]string{filepath.Join(parseDataDir, "*."+ext)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The different file-types may have the operations/fragments in a
|
||||
// different order.
|
||||
sortQueries(queries)
|
||||
queries := getTestQueries(t, ext)
|
||||
|
||||
got, want := ast.Dump(graphqlQueries), ast.Dump(queries)
|
||||
if got != want {
|
||||
|
||||
Reference in New Issue
Block a user