Skip to content

Commit

Permalink
omit_resolver_fields also respects gqlgen.yml's config
Browse files Browse the repository at this point in the history
  • Loading branch information
aereal committed May 6, 2024
1 parent 769632a commit 0044392
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,13 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
f = mf
}

if f.IsResolver && cfg.OmitResolverFields {
var shouldGenerateResolver bool
if models := cfg.Models; len(models) > 0 {
if fields := models[schemaType.Name].Fields; len(fields) > 0 {
shouldGenerateResolver = fields[field.Name].Resolver
}
}
if (f.IsResolver || shouldGenerateResolver) && cfg.OmitResolverFields {
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 @@ -332,6 +332,23 @@ func TestModelGenerationOmitResolverFields(t *testing.T) {
require.NotContains(t, string(generated), "ResolverField")
}

func TestModelGenerationOmitResolverFieldsWithExplicitResolver(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_omit_resolver_fields_with_explicit_resolver.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_omit_resolver_fields_with_explicit_resolver/"))
generated, err := os.ReadFile("./out_omit_resolver_fields_with_explicit_resolver/generated.go")
require.NoError(t, err)
require.Contains(t, string(generated), "type Base struct")
require.Contains(t, string(generated), "StandardField")
require.NotContains(t, string(generated), "ResolverField")
}

func TestModelGenerationStructFieldPointers(t *testing.T) {
cfg, err := config.LoadConfig("testdata/gqlgen_struct_field_pointers.yml")
require.NoError(t, err)
Expand Down

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema:
- "testdata/schema_omit_resolver_fields_with_explicit_resolver.graphql"

exec:
filename: out_omit_resolver_fields_with_explict_resolver/ignored.go
model:
filename: out_omit_resolver_fields_with_explicit_resolver/generated.go

omit_resolver_fields: true
models:
Base:
fields:
ResolverField:
resolver: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Base {
StandardField: String!
ResolverField: String!
}

0 comments on commit 0044392

Please sign in to comment.