-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add follow-schema layout for exec (#1309)
* Define ExecConfig separate from PackageConfig When support for writing generated code to a directory instead of a single file is added, ExecConfig will need additional fields that will not be relevant to other users of PackageConfig. * Add single-file, follow-schema layouts When `ExecLayout` is set to `follow-schema`, output generated code to a directory instead of a single file. Each file in the output directory will correspond to a single *.graphql schema file (plus a root!.generated.go file containing top-level definitions that are not specific to a single schema file). `ExecLayout` defaults to `single-file`, which is the current behavior, so this new functionality is opt-in. These layouts expose similar functionality to the `ResolverLayout`s with the same name, just applied to `exec` instead of `resolver`. Resolves issue #1265. * Rebase, regenerate Signed-off-by: Steve Coffman <[email protected]> Co-authored-by: Steve Coffman <[email protected]>
- Loading branch information
1 parent
1297835
commit 1f50001
Showing
189 changed files
with
24,954 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"go/types" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/99designs/gqlgen/internal/code" | ||
) | ||
|
||
type ExecConfig struct { | ||
Package string `yaml:"package,omitempty"` | ||
Layout ExecLayout `yaml:"layout,omitempty"` // Default: single-file | ||
|
||
// Only for single-file layout: | ||
Filename string `yaml:"filename,omitempty"` | ||
|
||
// Only for follow-schema layout: | ||
FilenameTemplate string `yaml:"filename_template,omitempty"` // String template with {name} as placeholder for base name. | ||
DirName string `yaml:"dir"` | ||
} | ||
|
||
type ExecLayout string | ||
|
||
var ( | ||
// Write all generated code to a single file. | ||
ExecLayoutSingleFile ExecLayout = "single-file" | ||
// Write generated code to a directory, generating one Go source file for each GraphQL schema file. | ||
ExecLayoutFollowSchema ExecLayout = "follow-schema" | ||
) | ||
|
||
func (r *ExecConfig) Check() error { | ||
if r.Layout == "" { | ||
r.Layout = ExecLayoutSingleFile | ||
} | ||
|
||
switch r.Layout { | ||
case ExecLayoutSingleFile: | ||
if r.Filename == "" { | ||
return fmt.Errorf("filename must be specified when using single-file layout") | ||
} | ||
if !strings.HasSuffix(r.Filename, ".go") { | ||
return fmt.Errorf("filename should be path to a go source file when using single-file layout") | ||
} | ||
r.Filename = abs(r.Filename) | ||
case ExecLayoutFollowSchema: | ||
if r.DirName == "" { | ||
return fmt.Errorf("dir must be specified when using follow-schema layout") | ||
} | ||
r.DirName = abs(r.DirName) | ||
default: | ||
return fmt.Errorf("invalid layout %s", r.Layout) | ||
} | ||
|
||
if strings.ContainsAny(r.Package, "./\\") { | ||
return fmt.Errorf("package should be the output package name only, do not include the output filename") | ||
} | ||
|
||
if r.Package == "" && r.Dir() != "" { | ||
r.Package = code.NameForDir(r.Dir()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *ExecConfig) ImportPath() string { | ||
if r.Dir() == "" { | ||
return "" | ||
} | ||
return code.ImportPathForDir(r.Dir()) | ||
} | ||
|
||
func (r *ExecConfig) Dir() string { | ||
switch r.Layout { | ||
case ExecLayoutSingleFile: | ||
if r.Filename == "" { | ||
return "" | ||
} | ||
return filepath.Dir(r.Filename) | ||
case ExecLayoutFollowSchema: | ||
return abs(r.DirName) | ||
default: | ||
panic("invalid layout " + r.Layout) | ||
} | ||
} | ||
|
||
func (r *ExecConfig) Pkg() *types.Package { | ||
if r.Dir() == "" { | ||
return nil | ||
} | ||
return types.NewPackage(r.ImportPath(), r.Package) | ||
} | ||
|
||
func (r *ExecConfig) IsDefined() bool { | ||
return r.Filename != "" || r.DirName != "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package codegen | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/99designs/gqlgen/codegen/config" | ||
"github.com/vektah/gqlparser/v2/ast" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestData_Directives(t *testing.T) { | ||
d := Data{ | ||
Config: &config.Config{ | ||
Sources: []*ast.Source{ | ||
{ | ||
Name: "schema.graphql", | ||
}, | ||
}, | ||
}, | ||
AllDirectives: DirectiveList{ | ||
"includeDirective": { | ||
DirectiveDefinition: &ast.DirectiveDefinition{ | ||
Name: "includeDirective", | ||
Position: &ast.Position{ | ||
Src: &ast.Source{ | ||
Name: "schema.graphql", | ||
}, | ||
}, | ||
}, | ||
Name: "includeDirective", | ||
Args: nil, | ||
Builtin: false, | ||
}, | ||
"excludeDirective": { | ||
DirectiveDefinition: &ast.DirectiveDefinition{ | ||
Name: "excludeDirective", | ||
Position: &ast.Position{ | ||
Src: &ast.Source{ | ||
Name: "anothersource.graphql", | ||
}, | ||
}, | ||
}, | ||
Name: "excludeDirective", | ||
Args: nil, | ||
Builtin: false, | ||
}, | ||
}, | ||
} | ||
|
||
expected := DirectiveList{ | ||
"includeDirective": { | ||
DirectiveDefinition: &ast.DirectiveDefinition{ | ||
Name: "includeDirective", | ||
Position: &ast.Position{ | ||
Src: &ast.Source{ | ||
Name: "schema.graphql", | ||
}, | ||
}, | ||
}, | ||
Name: "includeDirective", | ||
Args: nil, | ||
Builtin: false, | ||
}, | ||
} | ||
|
||
assert.Equal(t, expected, d.Directives()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.