Skip to content

Commit

Permalink
Merge pull request #717 from cbelsole/schema_file_globbing
Browse files Browse the repository at this point in the history
 added schema file globbing fixes #631
  • Loading branch information
Mathew Byrne committed Jun 12, 2019
2 parents ba7092c + 39db147 commit 694f90a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 6 deletions.
34 changes: 31 additions & 3 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -67,9 +68,36 @@ func LoadConfig(filename string) (*Config, error) {
preGlobbing := config.SchemaFilename
config.SchemaFilename = StringList{}
for _, f := range preGlobbing {
matches, err := filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
var matches []string

// for ** we want to override default globbing patterns and walk all
// subdirectories to match schema files.
if strings.Contains(f, "**") {
pathParts := strings.SplitN(f, "**", 2)
if err := filepath.Walk(pathParts[0], func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// make sure paths match files
// <root>?.*<filename|.+>\.<ext>
fileRegex := regexp.MustCompile(
pathParts[0] +
"?.*" +
strings.Replace(strings.Replace(pathParts[1], ".", "\\.", -1), "*", ".+", -1))
if fileRegex.MatchString(path) {
matches = append(matches, path)
}

return nil
}); err != nil {
return nil, errors.Wrapf(err, "failed to walk schema at root %s", pathParts[0])
}
} else {
matches, err = filepath.Glob(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to glob schema filename %s", f)
}
}

for _, m := range matches {
Expand Down
13 changes: 13 additions & 0 deletions codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ func TestLoadConfig(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unknownkeys.yml")
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type config.Config")
})

t.Run("globbed filenames", func(t *testing.T) {
c, err := LoadConfig("testdata/cfg/glob.yml")
require.NoError(t, err)

require.Equal(t, c.SchemaFilename[0], "testdata/cfg/glob/bar/bar with spaces.graphql")
require.Equal(t, c.SchemaFilename[1], "testdata/cfg/glob/foo/foo.graphql")
})

t.Run("unwalkable path", func(t *testing.T) {
_, err := LoadConfig("testdata/cfg/unwalkable.yml")
require.EqualError(t, err, "failed to walk schema at root not_walkable/: lstat not_walkable/: no such file or directory")
})
}

func TestLoadDefaultConfig(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions codegen/config/testdata/cfg/glob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- testdata/cfg/glob/**/*.graphql
exec:
filename: generated.go
model:
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver
12 changes: 12 additions & 0 deletions codegen/config/testdata/cfg/glob/bar/bar with spaces.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Query {
todos: [Todo!]!
}

input NewTodo {
text: String!
userId: String!
}

type Mutation {
createTodo(input: NewTodo!): Todo!
}
11 changes: 11 additions & 0 deletions codegen/config/testdata/cfg/glob/foo/foo.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
}

type User {
id: ID!
name: String!
}
9 changes: 9 additions & 0 deletions codegen/config/testdata/cfg/unwalkable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- not_walkable/**/*.graphql
exec:
filename: generated.go
model:
filename: models_gen.go
resolver:
filename: resolver.go
type: Resolver
10 changes: 7 additions & 3 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ schema: schema.graphql
schema:
- schema.graphql
- user.graphql

# Or you can use globs
schema:
schema:
- "*.graphql"


# Or globs from a root directory
schema:
- "schema/**/*.graphql"

# Let gqlgen know where to put the generated server
exec:
filename: graph/generated/generated.go
Expand Down

0 comments on commit 694f90a

Please sign in to comment.