2020-09-17 09:57:18 +00:00
|
|
|
package clientgen
|
|
|
|
|
|
|
|
import (
|
2022-04-02 07:08:14 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-17 09:57:18 +00:00
|
|
|
"github.com/99designs/gqlgen/codegen/config"
|
|
|
|
"github.com/99designs/gqlgen/plugin"
|
2022-04-02 07:08:14 +00:00
|
|
|
gqlgencConfig "github.com/Yamashou/gqlgenc/config"
|
2020-09-17 09:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ plugin.ConfigMutator = &Plugin{}
|
|
|
|
|
|
|
|
type Plugin struct {
|
|
|
|
queryFilePaths []string
|
|
|
|
Client config.PackageConfig
|
2022-04-02 07:08:14 +00:00
|
|
|
GenerateConfig *gqlgencConfig.GenerateConfig
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 07:08:14 +00:00
|
|
|
func New(queryFilePaths []string, client config.PackageConfig, generateConfig *gqlgencConfig.GenerateConfig) *Plugin {
|
2020-09-17 09:57:18 +00:00
|
|
|
return &Plugin{
|
|
|
|
queryFilePaths: queryFilePaths,
|
|
|
|
Client: client,
|
2022-04-02 07:08:14 +00:00
|
|
|
GenerateConfig: generateConfig,
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) Name() string {
|
|
|
|
return "clientgen"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) MutateConfig(cfg *config.Config) error {
|
|
|
|
querySources, err := LoadQuerySources(p.queryFilePaths)
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("load query sources failed: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1. 全体のqueryDocumentを1度にparse
|
|
|
|
// 1. Parse document from source of query
|
2022-04-02 07:08:14 +00:00
|
|
|
queryDocument, err := ParseQueryDocuments(cfg.Schema, querySources, p.GenerateConfig)
|
2020-09-17 09:57:18 +00:00
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf(": %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. OperationごとのqueryDocumentを作成
|
|
|
|
// 2. Separate documents for each operation
|
|
|
|
queryDocuments, err := QueryDocumentsByOperations(cfg.Schema, queryDocument.Operations)
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("parse query document failed: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. テンプレートと情報ソースを元にコード生成
|
|
|
|
// 3. Generate code from template and document source
|
|
|
|
sourceGenerator := NewSourceGenerator(cfg, p.Client)
|
2022-04-02 07:08:14 +00:00
|
|
|
source := NewSource(cfg.Schema, queryDocument, sourceGenerator, p.GenerateConfig)
|
2020-09-17 09:57:18 +00:00
|
|
|
query, err := source.Query()
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("generating query object: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutation, err := source.Mutation()
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("generating mutation object: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fragments, err := source.Fragments()
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("generating fragment failed: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
operationResponses, err := source.OperationResponses()
|
|
|
|
if err != nil {
|
2022-04-02 07:08:14 +00:00
|
|
|
return fmt.Errorf("generating operation response failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
operations, err := source.Operations(queryDocuments)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("generating operation failed: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 07:08:14 +00:00
|
|
|
if err := RenderTemplate(cfg, query, mutation, fragments, operations, operationResponses, p.GenerateConfig, p.Client); err != nil {
|
|
|
|
return fmt.Errorf("template failed: %w", err)
|
2020-09-17 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|