Skip to content

Commit

Permalink
Add config option to omit root objects from models
Browse files Browse the repository at this point in the history
  • Loading branch information
ghjm committed Jan 9, 2024
1 parent 15c05a7 commit 00d1c06
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 15 deletions.
1 change: 1 addition & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
OmitComplexity bool `yaml:"omit_complexity,omitempty"`
OmitGQLGenFileNotice bool `yaml:"omit_gqlgen_file_notice,omitempty"`
OmitGQLGenVersionInFileNotice bool `yaml:"omit_gqlgen_version_in_file_notice,omitempty"`
OmitRootModels bool `yaml:"omit_root_models,omitempty"`
StructFieldsAlwaysPointers bool `yaml:"struct_fields_always_pointers,omitempty"`
ReturnPointersInUmarshalInput bool `yaml:"return_pointers_in_unmarshalinput,omitempty"`
ResolversAlwaysReturnPointers bool `yaml:"resolvers_always_return_pointers,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ resolver:
# Optional: turn on to exclude the gqlgen version in the generated file notice. No effect if `omit_gqlgen_file_notice` is true.
# omit_gqlgen_version_in_file_notice: false

# Optional: turn on to exclude root models such as Query and Mutation from the generated models file.
# omit_root_models: false

# Optional: turn off to make struct-type struct fields not use pointers
# e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing }
# struct_fields_always_pointers: true
Expand Down
10 changes: 6 additions & 4 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
b.Interfaces = append(b.Interfaces, it)
case ast.Object, ast.InputObject:
if cfg.IsRoot(schemaType) {
b.Models = append(b.Models, &Object{
Description: schemaType.Description,
Name: schemaType.Name,
})
if !cfg.OmitRootModels {
b.Models = append(b.Models, &Object{
Description: schemaType.Description,
Name: schemaType.Name,
})
}
continue
}

Expand Down
17 changes: 17 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ func TestModelGeneration(t *testing.T) {
})
}

func TestModelGenerationOmitRootModels(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_omit_root_models.yml")
require.NoError(t, err)
require.NoError(t, cfg.Init())
p := Plugin{
MutateHook: mutateHook,
FieldHook: DefaultFieldMutateHook,
}
//cfg.OmitRootModels = true
require.NoError(t, p.MutateConfig(cfg))
require.NoError(t, goBuild(t, "./out/"))
generated, err := os.ReadFile("./out/generated_omit_root_models.go")
require.NoError(t, err)
require.NotContains(t, string(generated), "type Mutation struct")
require.NotContains(t, string(generated), "type Query struct")
}

func TestModelGenerationStructFieldPointers(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_struct_field_pointers.yml")
require.NoError(t, err)
Expand Down
11 changes: 0 additions & 11 deletions plugin/modelgen/out/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions plugin/modelgen/out/generated_omit_root_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions plugin/modelgen/testdata/gqlgen_omit_root_models.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema:
- "testdata/schema_omit_root_models.graphql"

exec:
filename: out/ignored.go
model:
filename: out/generated_omit_root_models.go

omit_root_models: true
14 changes: 14 additions & 0 deletions plugin/modelgen/testdata/schema_omit_root_models.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Query {
thisShoudlntGetGenerated: Boolean
}

type Mutation {
thisShoudlntGetGenerated: Boolean
}

enum SomeContent {
This
Is
A
Test
}

0 comments on commit 00d1c06

Please sign in to comment.