Skip to content

Commit 70bc5cd

Browse files
committed
Unify error package to xerrors
1 parent 675a673 commit 70bc5cd

File tree

8 files changed

+48
-33
lines changed

8 files changed

+48
-33
lines changed

client/client.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"net/http"
98

109
"github.com/Yamashou/gqlgenc/graphqljson"
@@ -43,12 +42,12 @@ func (c *Client) newRequest(ctx context.Context, query string, vars map[string]i
4342

4443
requestBody, err := json.Marshal(r)
4544
if err != nil {
46-
return nil, fmt.Errorf("encode: %s", err.Error())
45+
return nil, xerrors.Errorf("encode: %w", err)
4746
}
4847

4948
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL, bytes.NewBuffer(requestBody))
5049
if err != nil {
51-
return nil, xerrors.Errorf(": %w", err)
50+
return nil, xerrors.Errorf("create request struct failed: %w", err)
5251
}
5352

5453
for _, httpRequestOption := range c.HTTPRequestOptions {
@@ -66,18 +65,18 @@ func (c *Client) newRequest(ctx context.Context, query string, vars map[string]i
6665
func (c *Client) Post(ctx context.Context, query string, respData interface{}, vars map[string]interface{}, httpRequestOptions ...HTTPRequestOption) error {
6766
req, err := c.newRequest(ctx, query, vars, httpRequestOptions)
6867
if err != nil {
69-
return xerrors.Errorf(": %w", err)
68+
return xerrors.Errorf("don't create request: %w", err)
7069
}
7170
req.Header.Add("Content-Type", "application/json")
7271

7372
resp, err := c.Client.Do(req)
7473
if err != nil {
75-
return xerrors.Errorf(": %w", err)
74+
return xerrors.Errorf("request failed: %w", err)
7675
}
7776
defer resp.Body.Close()
7877

7978
if err := graphqljson.Unmarshal(resp.Body, respData); err != nil {
80-
return xerrors.Errorf(": %w", err)
79+
return xerrors.Errorf("response mapping failed: %w", err)
8180
}
8281

8382
if resp.StatusCode < 200 || 299 < resp.StatusCode {

clientgen/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (p *Plugin) Name() string {
2727
func (p *Plugin) MutateConfig(cfg *config.Config) error {
2828
querySources, err := LoadQuerySources(p.queryFilePaths)
2929
if err != nil {
30-
return xerrors.Errorf(": %w", err)
30+
return xerrors.Errorf("load query sources failed: %w", err)
3131
}
3232

3333
// 1. 全体のqueryDocumentを1度にparse
@@ -39,7 +39,7 @@ func (p *Plugin) MutateConfig(cfg *config.Config) error {
3939
// 2. OperationごとのqueryDocumentを作成
4040
queryDocuments, err := QueryDocumentsByOperations(cfg.Schema, queryDocument.Operations)
4141
if err != nil {
42-
return xerrors.Errorf(": %w", err)
42+
return xerrors.Errorf("parse query document failed: %w", err)
4343
}
4444

4545
// 3. テンプレートと情報ソースを元にコード生成
@@ -48,7 +48,7 @@ func (p *Plugin) MutateConfig(cfg *config.Config) error {
4848
fragments := source.fragments()
4949
operationResponses := source.operationResponses()
5050
if err := RenderTemplate(cfg, fragments, source.operations(queryDocuments), operationResponses, p.Client); err != nil {
51-
return xerrors.Errorf(": %w", err)
51+
return xerrors.Errorf("template failed: %w", err)
5252
}
5353

5454
return nil

clientgen/source_generator.go

-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ func (r *SourceGenerator) NewResponseField(selection ast.Selection) *ResponseFie
149149
}
150150
}
151151

152-
// ここに来たらバグ
153152
panic("unexpected selection type")
154153
}
155154

clientgen/template.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package clientgen
33
import (
44
"github.com/99designs/gqlgen/codegen/config"
55
"github.com/99designs/gqlgen/codegen/templates"
6+
"golang.org/x/xerrors"
67
)
78

89
func RenderTemplate(cfg *config.Config, fragments []*Fragment, operations []*Operation, operationResponses []*OperationResponse, client config.PackageConfig) error {
@@ -17,7 +18,7 @@ func RenderTemplate(cfg *config.Config, fragments []*Fragment, operations []*Ope
1718
Packages: cfg.Packages,
1819
PackageDoc: "// Code generated by github.com/Yamashou/gqlgenc, DO NOT EDIT.\n",
1920
}); err != nil {
20-
return err
21+
return xerrors.Errorf("%s generating failed: %w", client.Filename, err)
2122
}
2223

2324
return nil

config/config.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config
22

33
import (
44
"context"
5-
"fmt"
65
"io/ioutil"
76
"net/http"
87
"os"
@@ -26,6 +25,7 @@ type Config struct {
2625
Endpoint EndPointConfig `yaml:"endpoint"`
2726
Query []string `yaml:"query"`
2827

28+
// gqlgen config struct
2929
GQLConfig *config.Config `yaml:"-"`
3030
}
3131

@@ -111,24 +111,12 @@ func (c *Config) LoadSchema(ctx context.Context) error {
111111
func LoadRemoteSchema(ctx context.Context, gqlclient *client.Client) (*ast.Schema, error) {
112112
var res introspection.IntrospectionQuery
113113
if err := gqlclient.Post(ctx, introspection.Introspection, &res, nil); err != nil {
114-
fmt.Println(err)
115114
return nil, err
116115
}
117116

118-
var doc ast.SchemaDocument
119-
typeMap := make(map[string]*introspection.FullType)
120-
for _, typ := range res.Schema.Types {
121-
typeMap[*typ.Name] = typ
122-
}
123-
for _, typeVale := range typeMap {
124-
doc.Definitions = append(doc.Definitions, introspection.ParseTypeSystemDefinition(typeVale))
125-
}
126-
127-
for _, directiveValue := range res.Schema.Directives {
128-
doc.Directives = append(doc.Directives, introspection.ParseDirectiveDefinition(directiveValue))
129-
}
117+
doc := introspection.ParseIntrospectionQuery(res)
130118

131-
schema, err := validator.ValidateSchemaDocument(&doc)
119+
schema, err := validator.ValidateSchemaDocument(doc)
132120
if err != nil {
133121
return nil, err
134122
}

generator/generater.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package generator
33
import (
44
"context"
55

6+
"golang.org/x/xerrors"
7+
68
"github.com/99designs/gqlgen/api"
79
"github.com/99designs/gqlgen/plugin"
810
"github.com/99designs/gqlgen/plugin/modelgen"
911
"github.com/Yamashou/gqlgenc/config"
10-
"github.com/pkg/errors"
1112
)
1213

1314
func Generate(ctx context.Context, cfg *config.Config, option ...api.Option) error {
@@ -20,18 +21,19 @@ func Generate(ctx context.Context, cfg *config.Config, option ...api.Option) err
2021
}
2122

2223
if err := cfg.LoadSchema(ctx); err != nil {
23-
return errors.Wrap(err, "failed to load schema")
24+
return xerrors.Errorf("failed to load schema: %w", err)
2425
}
2526

2627
if err := cfg.GQLConfig.Init(); err != nil {
27-
return errors.Wrap(err, "generating core failed")
28+
return xerrors.Errorf("generating core failed: %w", err)
2829
}
2930

3031
for _, p := range plugins {
3132
if mut, ok := p.(plugin.ConfigMutator); ok {
3233
err := mut.MutateConfig(cfg.GQLConfig)
3334
if err != nil {
34-
return errors.Wrap(err, p.Name())
35+
// return errors.Wrap(err, p.Name())
36+
return xerrors.Errorf("%s failed: %w", p.Name(), err)
3537
}
3638
}
3739
}

introspection/parse.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@ import (
66
"github.com/vektah/gqlparser/v2/ast"
77
)
88

9-
func ParseDirectiveDefinition(directiveValue *DirectiveType) *ast.DirectiveDefinition {
9+
func ParseIntrospectionQuery(query IntrospectionQuery) *ast.SchemaDocument {
10+
var doc ast.SchemaDocument
11+
typeMap := query.Schema.Types.NameMap()
12+
13+
for _, typeVale := range typeMap {
14+
doc.Definitions = append(doc.Definitions, parseTypeSystemDefinition(typeVale))
15+
}
16+
17+
for _, directiveValue := range query.Schema.Directives {
18+
doc.Directives = append(doc.Directives, parseDirectiveDefinition(directiveValue))
19+
}
20+
21+
return &doc
22+
}
23+
24+
func parseDirectiveDefinition(directiveValue *DirectiveType) *ast.DirectiveDefinition {
1025
args := make(ast.ArgumentDefinitionList, 0, len(directiveValue.Args))
1126
for _, arg := range directiveValue.Args {
1227
argumentDefinition := buildInputValue(arg)
@@ -172,7 +187,7 @@ func parseScalarTypeExtension(typeVale *FullType) *ast.Definition {
172187
}
173188
}
174189

175-
func ParseTypeSystemDefinition(typeVale *FullType) *ast.Definition {
190+
func parseTypeSystemDefinition(typeVale *FullType) *ast.Definition {
176191
switch typeVale.Kind {
177192
case TypeKindScalar:
178193
return parseScalarTypeExtension(typeVale)

introspection/type.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ const (
1313
TypeKindNonNull TypeKind = "NON_NULL"
1414
)
1515

16+
type FullTypes []*FullType
17+
18+
func (fs FullTypes) NameMap() map[string]*FullType {
19+
typeMap := make(map[string]*FullType)
20+
for _, typ := range fs {
21+
typeMap[*typ.Name] = typ
22+
}
23+
24+
return typeMap
25+
}
26+
1627
type FullType struct {
1728
Kind TypeKind
1829
Name *string
@@ -56,7 +67,7 @@ type IntrospectionQuery struct {
5667
QueryType struct{ Name *string }
5768
MutationType *struct{ Name *string }
5869
SubscriptionType *struct{ Name *string }
59-
Types []*FullType
70+
Types FullTypes
6071
Directives []*DirectiveType
6172
} `graphql:"__schema"`
6273
}

0 commit comments

Comments
 (0)