Skip to content

Commit

Permalink
Merge pull request #261 from Yamashou/newfunction
Browse files Browse the repository at this point in the history
Add new function for plugin
  • Loading branch information
Yamashou authored Dec 25, 2024
2 parents 88a613e + 0a2b512 commit 94cb8fc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
39 changes: 35 additions & 4 deletions clientgenv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin"
gqlgencConfig "github.com/Yamashou/gqlgenc/config"
"github.com/Yamashou/gqlgenc/parsequery"
"github.com/Yamashou/gqlgenc/querydocument"
"github.com/vektah/gqlparser/v2/ast"
)

Expand All @@ -19,25 +21,54 @@ type Plugin struct {
GenerateConfig *gqlgencConfig.GenerateConfig
}

func New(queryFilePaths []string, queryDocument *ast.QueryDocument, operationQueryDocuments []*ast.QueryDocument, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *Plugin {
func New(queryDocument *ast.QueryDocument, operationQueryDocuments []*ast.QueryDocument, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *Plugin {
return &Plugin{
queryFilePaths: queryFilePaths,
queryDocument: queryDocument,
operationQueryDocuments: operationQueryDocuments,
Client: client,
GenerateConfig: generateConfig,
}
}

func NewWithQueryDocument(queryFilePaths []string, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *Plugin {
return &Plugin{
queryFilePaths: queryFilePaths,
Client: client,
GenerateConfig: generateConfig,
}
}

func (p *Plugin) Name() string {
return "clientgen"
}

func (p *Plugin) MutateConfig(cfg *config.Config) error {
queryDocument := p.queryDocument
if queryDocument == nil {
querySources, err := parsequery.LoadQuerySources(p.queryFilePaths)
if err != nil {
return fmt.Errorf("load query sources failed: %w", err)
}

queryDocument, err = parsequery.ParseQueryDocuments(cfg.Schema, querySources)
if err != nil {
return fmt.Errorf(": %w", err)
}
}

var err error
operationQueryDocuments := p.operationQueryDocuments
if operationQueryDocuments == nil {
operationQueryDocuments, err = querydocument.QueryDocumentsByOperations(cfg.Schema, queryDocument.Operations)
if err != nil {
return fmt.Errorf(": %w", err)
}
}

// テンプレートと情報ソースを元にコード生成
// Generate code from template and document source
sourceGenerator := NewSourceGenerator(cfg, p.Client, p.GenerateConfig)
source := NewSource(cfg.Schema, p.queryDocument, sourceGenerator, p.GenerateConfig)
source := NewSource(cfg.Schema, queryDocument, sourceGenerator, p.GenerateConfig)

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

operations, err := source.Operations(p.operationQueryDocuments)
operations, err := source.Operations(operationQueryDocuments)
if err != nil {
return fmt.Errorf("generating operation failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion clientgenv2/source_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func NewStructGenerator(responseFieldList ResponseFieldList) *StructGenerator {
}
}

func mergeFieldsRecursively(targetFields ResponseFieldList, sourceFields ResponseFieldList, preMerged, postMerged []*StructSource) (ResponseFieldList, []*StructSource, []*StructSource) {
func mergeFieldsRecursively(targetFields, sourceFields ResponseFieldList, preMerged, postMerged []*StructSource) (ResponseFieldList, []*StructSource, []*StructSource) {
responseFieldList := make(ResponseFieldList, 0)
targetFieldsMap := targetFields.MapByName()
newPreMerged := preMerged
Expand Down
2 changes: 1 addition & 1 deletion clientv2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ func TestEncoder_encodeStruct(t *testing.T) {
enableOmitemptyTag: true,
want: map[string]any{
"name": "John",
"email2": nil,
"email2": nil,
"address": map[string]any{"city": "Tokyo"},
},
},
Expand Down
5 changes: 2 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ func Generate(ctx context.Context, cfg *config.Config) error {
return fmt.Errorf(": %w", err)
}

usedTypes := querydocument.CollectTypesFromQueryDocuments(cfg.GQLConfig.Schema, operationQueryDocuments)

var clientGen api.Option
if cfg.Generate != nil {
clientGen = api.AddPlugin(clientgenv2.New(cfg.Query, queryDocument, operationQueryDocuments, cfg.Client, cfg.Generate))
clientGen = api.AddPlugin(clientgenv2.New(queryDocument, operationQueryDocuments, cfg.Client, cfg.Generate))
}

var plugins []plugin.Plugin
if cfg.Model.IsDefined() {
usedTypes := querydocument.CollectTypesFromQueryDocuments(cfg.GQLConfig.Schema, operationQueryDocuments)
p := &modelgen.Plugin{
MutateHook: mutateHook(cfg, usedTypes),
FieldHook: modelgen.DefaultFieldMutateHook,
Expand Down
14 changes: 6 additions & 8 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package generator_test

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/Yamashou/gqlgenc/config"
"github.com/Yamashou/gqlgenc/generator"
"github.com/stretchr/testify/suite"
"golang.org/x/tools/go/packages"
"os"
"path/filepath"
"testing"
)

type Suite struct {
Expand All @@ -25,12 +26,10 @@ const (
)

func (s *Suite) TestGenerator_withTestData() {

dirs := s.getTestDirs()

for _, dir := range dirs {
s.Run(dir, func() {

// temporary change working directory
s.useDirForTest(filepath.Join("testdata", dir))

Expand Down Expand Up @@ -64,8 +63,8 @@ func (s *Suite) TestGenerator_withTestData() {
for path, content := range actualFiles {
s.T().Logf("resetting expected file %s", path)
expectedPath := filepath.Join(expected, path)
_ = os.MkdirAll(filepath.Dir(expectedPath), 0700)
err = os.WriteFile(expectedPath, []byte(content), 0644)
_ = os.MkdirAll(filepath.Dir(expectedPath), 0o700)
err = os.WriteFile(expectedPath, []byte(content), 0o644)
s.Require().NoError(err)
}
return
Expand Down Expand Up @@ -115,7 +114,6 @@ func (s *Suite) loadFiles(dir string) map[string]string {
rel, err := filepath.Rel(dir, path)
if err != nil {
return err

}
files[rel] = string(content)
return nil
Expand Down

0 comments on commit 94cb8fc

Please sign in to comment.