Skip to content

Commit 95c9192

Browse files
committed
Add new function for plugin
1 parent 88a613e commit 95c9192

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

clientgenv2/client.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/99designs/gqlgen/codegen/config"
77
"github.com/99designs/gqlgen/plugin"
88
gqlgencConfig "github.com/Yamashou/gqlgenc/config"
9+
"github.com/Yamashou/gqlgenc/parsequery"
10+
"github.com/Yamashou/gqlgenc/querydocument"
911
"github.com/vektah/gqlparser/v2/ast"
1012
)
1113

@@ -29,15 +31,45 @@ func New(queryFilePaths []string, queryDocument *ast.QueryDocument, operationQue
2931
}
3032
}
3133

34+
func NewWithQueryDocument(queryFilePaths []string, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *Plugin {
35+
return &Plugin{
36+
queryFilePaths: queryFilePaths,
37+
Client: client,
38+
GenerateConfig: generateConfig,
39+
}
40+
}
41+
3242
func (p *Plugin) Name() string {
3343
return "clientgen"
3444
}
3545

3646
func (p *Plugin) MutateConfig(cfg *config.Config) error {
47+
queryDocument := p.queryDocument
48+
if queryDocument == nil {
49+
querySources, err := parsequery.LoadQuerySources(p.queryFilePaths)
50+
if err != nil {
51+
return fmt.Errorf("load query sources failed: %w", err)
52+
}
53+
54+
queryDocument, err = parsequery.ParseQueryDocuments(cfg.Schema, querySources)
55+
if err != nil {
56+
return fmt.Errorf(": %w", err)
57+
}
58+
}
59+
60+
var err error
61+
operationQueryDocuments := p.operationQueryDocuments
62+
if operationQueryDocuments == nil {
63+
operationQueryDocuments, err = querydocument.QueryDocumentsByOperations(cfg.Schema, queryDocument.Operations)
64+
if err != nil {
65+
return fmt.Errorf(": %w", err)
66+
}
67+
}
68+
3769
// テンプレートと情報ソースを元にコード生成
3870
// Generate code from template and document source
3971
sourceGenerator := NewSourceGenerator(cfg, p.Client, p.GenerateConfig)
40-
source := NewSource(cfg.Schema, p.queryDocument, sourceGenerator, p.GenerateConfig)
72+
source := NewSource(cfg.Schema, queryDocument, sourceGenerator, p.GenerateConfig)
4173

4274
fragments, err := source.Fragments()
4375
if err != nil {
@@ -49,7 +81,7 @@ func (p *Plugin) MutateConfig(cfg *config.Config) error {
4981
return fmt.Errorf("generating operation response failed: %w", err)
5082
}
5183

52-
operations, err := source.Operations(p.operationQueryDocuments)
84+
operations, err := source.Operations(operationQueryDocuments)
5385
if err != nil {
5486
return fmt.Errorf("generating operation failed: %w", err)
5587
}

clientgenv2/source_generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func NewStructGenerator(responseFieldList ResponseFieldList) *StructGenerator {
124124
}
125125
}
126126

127-
func mergeFieldsRecursively(targetFields ResponseFieldList, sourceFields ResponseFieldList, preMerged, postMerged []*StructSource) (ResponseFieldList, []*StructSource, []*StructSource) {
127+
func mergeFieldsRecursively(targetFields, sourceFields ResponseFieldList, preMerged, postMerged []*StructSource) (ResponseFieldList, []*StructSource, []*StructSource) {
128128
responseFieldList := make(ResponseFieldList, 0)
129129
targetFieldsMap := targetFields.MapByName()
130130
newPreMerged := preMerged

clientv2/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
10181018
enableOmitemptyTag: true,
10191019
want: map[string]any{
10201020
"name": "John",
1021-
"email2": nil,
1021+
"email2": nil,
10221022
"address": map[string]any{"city": "Tokyo"},
10231023
},
10241024
},

generator/generator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,14 @@ func Generate(ctx context.Context, cfg *config.Config) error {
116116
return fmt.Errorf(": %w", err)
117117
}
118118

119-
usedTypes := querydocument.CollectTypesFromQueryDocuments(cfg.GQLConfig.Schema, operationQueryDocuments)
120-
121119
var clientGen api.Option
122120
if cfg.Generate != nil {
123-
clientGen = api.AddPlugin(clientgenv2.New(cfg.Query, queryDocument, operationQueryDocuments, cfg.Client, cfg.Generate))
121+
clientGen = api.AddPlugin(clientgenv2.New(queryDocument, operationQueryDocuments, cfg.Client, cfg.Generate))
124122
}
125123

126124
var plugins []plugin.Plugin
127125
if cfg.Model.IsDefined() {
126+
usedTypes := querydocument.CollectTypesFromQueryDocuments(cfg.GQLConfig.Schema, operationQueryDocuments)
128127
p := &modelgen.Plugin{
129128
MutateHook: mutateHook(cfg, usedTypes),
130129
FieldHook: modelgen.DefaultFieldMutateHook,

generator/generator_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package generator_test
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
59
"github.com/Yamashou/gqlgenc/config"
610
"github.com/Yamashou/gqlgenc/generator"
711
"github.com/stretchr/testify/suite"
812
"golang.org/x/tools/go/packages"
9-
"os"
10-
"path/filepath"
11-
"testing"
1213
)
1314

1415
type Suite struct {
@@ -25,12 +26,10 @@ const (
2526
)
2627

2728
func (s *Suite) TestGenerator_withTestData() {
28-
2929
dirs := s.getTestDirs()
3030

3131
for _, dir := range dirs {
3232
s.Run(dir, func() {
33-
3433
// temporary change working directory
3534
s.useDirForTest(filepath.Join("testdata", dir))
3635

@@ -64,8 +63,8 @@ func (s *Suite) TestGenerator_withTestData() {
6463
for path, content := range actualFiles {
6564
s.T().Logf("resetting expected file %s", path)
6665
expectedPath := filepath.Join(expected, path)
67-
_ = os.MkdirAll(filepath.Dir(expectedPath), 0700)
68-
err = os.WriteFile(expectedPath, []byte(content), 0644)
66+
_ = os.MkdirAll(filepath.Dir(expectedPath), 0o700)
67+
err = os.WriteFile(expectedPath, []byte(content), 0o644)
6968
s.Require().NoError(err)
7069
}
7170
return
@@ -115,7 +114,6 @@ func (s *Suite) loadFiles(dir string) map[string]string {
115114
rel, err := filepath.Rel(dir, path)
116115
if err != nil {
117116
return err
118-
119117
}
120118
files[rel] = string(content)
121119
return nil

0 commit comments

Comments
 (0)