From 32bdbdd18c1afb496158525490bce0cb1629d5e2 Mon Sep 17 00:00:00 2001 From: Graham Mainwaring Date: Fri, 12 Jan 2024 08:20:11 -0500 Subject: [PATCH] Add config option to omit root objects from models (#2878) --- codegen/config/config.go | 1 + docs/content/config.md | 3 ++ plugin/modelgen/models.go | 10 ++-- plugin/modelgen/models_test.go | 17 ++++++ plugin/modelgen/out/generated.go | 11 ---- .../out/generated_omit_root_models.go | 54 +++++++++++++++++++ .../testdata/gqlgen_omit_root_models.yml | 9 ++++ .../testdata/schema_omit_root_models.graphql | 18 +++++++ 8 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 plugin/modelgen/out/generated_omit_root_models.go create mode 100644 plugin/modelgen/testdata/gqlgen_omit_root_models.yml create mode 100644 plugin/modelgen/testdata/schema_omit_root_models.graphql diff --git a/codegen/config/config.go b/codegen/config/config.go index 6238b7702a7..5cbc6e24965 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -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"` diff --git a/docs/content/config.md b/docs/content/config.md index f464359aca8..c69c4202af1 100644 --- a/docs/content/config.md +++ b/docs/content/config.md @@ -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 diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index c7a27988d4c..fc37c7e2ea2 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -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 } diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index a9182d02f02..164dcee3144 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -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, + } + 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") + require.NotContains(t, string(generated), "type Subscription struct") +} + func TestModelGenerationStructFieldPointers(t *testing.T) { cfg, err := config.LoadConfig("testdata/gqlgen_struct_field_pointers.yml") require.NoError(t, err) diff --git a/plugin/modelgen/out/generated.go b/plugin/modelgen/out/generated.go index 4d51c32d915..f90f8b974a1 100644 --- a/plugin/modelgen/out/generated.go +++ b/plugin/modelgen/out/generated.go @@ -11,8 +11,6 @@ import ( "github.com/99designs/gqlgen/plugin/modelgen/internal/extrafields" ) -// Add any new functions or any additional code/template functionality here - type A interface { IsA() GetA() string @@ -196,9 +194,6 @@ func (MissingTypeNullable) IsMissingUnion() {} func (MissingTypeNullable) IsExistingUnion() {} -type Mutation struct { -} - type NotCyclicalA struct { FieldOne string `json:"FieldOne" database:"NotCyclicalAFieldOne"` FieldTwo int `json:"FieldTwo" database:"NotCyclicalAFieldTwo"` @@ -214,9 +209,6 @@ type OmitEmptyJSONTagTest struct { Value *string `json:"Value,omitempty" database:"OmitEmptyJsonTagTestValue"` } -type Query struct { -} - type Recursive struct { FieldOne *Recursive `json:"FieldOne" database:"RecursiveFieldOne"` FieldTwo *Recursive `json:"FieldTwo" database:"RecursiveFieldTwo"` @@ -229,9 +221,6 @@ type RenameFieldTest struct { OtherField string `json:"otherField" database:"RenameFieldTestotherField"` } -type Subscription struct { -} - // TypeWithDescription is a type with a description type TypeWithDescription struct { Name *string `json:"name,omitempty" database:"TypeWithDescriptionname"` diff --git a/plugin/modelgen/out/generated_omit_root_models.go b/plugin/modelgen/out/generated_omit_root_models.go new file mode 100644 index 00000000000..0150b18b5a4 --- /dev/null +++ b/plugin/modelgen/out/generated_omit_root_models.go @@ -0,0 +1,54 @@ +// Code generated by github.com/99designs/gqlgen, DO NOT EDIT. + +package out + +import ( + "fmt" + "io" + "strconv" +) + +type SomeContent string + +const ( + SomeContentThis SomeContent = "This" + SomeContentIs SomeContent = "Is" + SomeContentA SomeContent = "A" + SomeContentTest SomeContent = "Test" +) + +var AllSomeContent = []SomeContent{ + SomeContentThis, + SomeContentIs, + SomeContentA, + SomeContentTest, +} + +func (e SomeContent) IsValid() bool { + switch e { + case SomeContentThis, SomeContentIs, SomeContentA, SomeContentTest: + return true + } + return false +} + +func (e SomeContent) String() string { + return string(e) +} + +func (e *SomeContent) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = SomeContent(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid SomeContent", str) + } + return nil +} + +func (e SomeContent) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) +} diff --git a/plugin/modelgen/testdata/gqlgen_omit_root_models.yml b/plugin/modelgen/testdata/gqlgen_omit_root_models.yml new file mode 100644 index 00000000000..63720084d3e --- /dev/null +++ b/plugin/modelgen/testdata/gqlgen_omit_root_models.yml @@ -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 diff --git a/plugin/modelgen/testdata/schema_omit_root_models.graphql b/plugin/modelgen/testdata/schema_omit_root_models.graphql new file mode 100644 index 00000000000..800020b1c04 --- /dev/null +++ b/plugin/modelgen/testdata/schema_omit_root_models.graphql @@ -0,0 +1,18 @@ +type Query { + thisShoudlntGetGenerated: Boolean +} + +type Mutation { + thisShoudlntGetGenerated: Boolean +} + +type Subscription { + thisShoudlntGetGenerated: Boolean +} + +enum SomeContent { + This + Is + A + Test +}