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+
3242func (p * Plugin ) Name () string {
3343 return "clientgen"
3444}
3545
3646func (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 }
0 commit comments