Upgrade to go 1.19 and update dependencies (#3069)

* Update to go 1.19
* Update dependencies
* Update cross-compile script
* Add missing targets to cross-compile-all
* Update cache action to remove warning
This commit is contained in:
WithoutPants
2022-11-04 13:41:26 +11:00
committed by GitHub
parent f25881a3bf
commit bba7c23957
939 changed files with 101336 additions and 43819 deletions

View File

@@ -44,9 +44,13 @@ linters:
- varnamelen
- nilnil
- structcheck
- exhaustruct
- nonamedreturns
fast: false
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# Test
- path: _test\.go
@@ -57,6 +61,7 @@ issues:
linters:
- unused
- structcheck
- forcetypeassert
- path: introspection/type.go
linters:
- structcheck # These types fits IntrospectionQuery

View File

@@ -48,6 +48,9 @@ endpoint:
Authorization: "Bearer ${ANNICT_KEY}" # support environment variables
query:
- "./query/*.graphql" # Where are all the query files located?
generate:
clientV2: true # Generate a Client that provides a new signature
clientInterfaceName: "GithubGraphQLClient" # Determine the name of the generated client interface
```
Load a schema from a local file:
@@ -68,6 +71,9 @@ schema:
- "schema/**/*.graphql" # Where are all the schema files located?
query:
- "./query/*.graphql" # Where are all the query files located?
generate:
clientV2: true # Generate a Client that provides a new signature
clientInterfaceName: "GithubGraphQLClient" # Determine the name of the generated client interface
```
Execute the following command on same directory for .gqlgenc.yml

View File

@@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"github.com/Yamashou/gqlgenc/graphqljson"
@@ -55,6 +55,9 @@ func (c *Client) newRequest(ctx context.Context, operationName, query string, va
return nil, fmt.Errorf("create request struct failed: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
for _, httpRequestOption := range c.HTTPRequestOptions {
httpRequestOption(req)
}
@@ -109,8 +112,6 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData
if err != nil {
return fmt.Errorf("don't create request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
resp, err := c.Client.Do(req)
if err != nil {
@@ -118,7 +119,7 @@ func (c *Client) Post(ctx context.Context, operationName, query string, respData
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}

View File

@@ -24,7 +24,6 @@ package clientgen
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@@ -94,7 +93,7 @@ func LoadQuerySources(queryFileNames []string) ([]*ast.Source, error) {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
schemaRaw, err = os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to open schema: %w", err)
}

View File

@@ -19,7 +19,7 @@ func RenderTemplate(cfg *config.Config, query *Query, mutation *Mutation, fragme
"Operation": operations,
"OperationResponse": operationResponses,
"GenerateClient": generateCfg.ShouldGenerateClient(),
"ClientInterfaceName": generateCfg.ClientInterfaceName,
"ClientInterfaceName": generateCfg.GetClientInterfaceName(),
},
Packages: cfg.Packages,
PackageDoc: "// Code generated by github.com/Yamashou/gqlgenc, DO NOT EDIT.\n",

View File

@@ -4,7 +4,6 @@
{{ reserveImport "encoding/json" }}
{{ reserveImport "fmt" }}
{{ reserveImport "io" }}
{{ reserveImport "io/ioutil" }}
{{ reserveImport "net/http" }}
{{ reserveImport "net/url" }}
{{ reserveImport "path" }}

View File

@@ -24,7 +24,6 @@ package clientgenv2
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@@ -94,7 +93,7 @@ func LoadQuerySources(queryFileNames []string) ([]*ast.Source, error) {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
schemaRaw, err = os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to open schema: %w", err)
}

View File

@@ -4,7 +4,6 @@
{{ reserveImport "encoding/json" }}
{{ reserveImport "fmt" }}
{{ reserveImport "io" }}
{{ reserveImport "io/ioutil" }}
{{ reserveImport "net/http" }}
{{ reserveImport "net/url" }}
{{ reserveImport "path" }}

View File

@@ -3,7 +3,6 @@ package config
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -116,7 +115,7 @@ var path2regex = strings.NewReplacer(
// LoadConfig loads and parses the config gqlgenc config
func LoadConfig(filename string) (*Config, error) {
var cfg Config
b, err := ioutil.ReadFile(filename)
b, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to read config: %w", err)
}
@@ -188,7 +187,7 @@ func LoadConfig(filename string) (*Config, error) {
filename = filepath.ToSlash(filename)
var err error
var schemaRaw []byte
schemaRaw, err = ioutil.ReadFile(filename)
schemaRaw, err = os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("unable to open schema: %w", err)
}
@@ -269,7 +268,7 @@ func (c *Config) loadRemoteSchema(ctx context.Context) (*ast.Schema, error) {
func (c *Config) loadLocalSchema() (*ast.Schema, error) {
schema, err := gqlparser.LoadSchema(c.GQLConfig.Sources...)
if err != nil {
return nil, err
return nil, fmt.Errorf("loadLocalSchema: %w", err)
}
return schema, nil

View File

@@ -5,33 +5,43 @@ import (
"fmt"
"github.com/99designs/gqlgen/api"
codegenconfig "github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin"
"github.com/99designs/gqlgen/plugin/modelgen"
"github.com/Yamashou/gqlgenc/config"
)
// mutateHook adds the "omitempty" option to nilable fields.
// mutateHook adds the "omitempty" option to optional field from input type model as defined in graphql schema
// For more info see https://github.com/99designs/gqlgen/blob/master/docs/content/recipes/modelgen-hook.md
func mutateHook(build *modelgen.ModelBuild) *modelgen.ModelBuild {
for _, model := range build.Models {
for _, field := range model.Fields {
field.Tag = `json:"` + field.Name
if codegenconfig.IsNilable(field.Type) {
field.Tag += ",omitempty"
}
field.Tag += `"`
}
}
func mutateHook(cfg *config.Config) func(b *modelgen.ModelBuild) *modelgen.ModelBuild {
return func(build *modelgen.ModelBuild) *modelgen.ModelBuild {
for _, model := range build.Models {
// only handle input type model
if schemaModel, ok := cfg.GQLConfig.Schema.Types[model.Name]; ok && schemaModel.IsInputType() {
for _, field := range model.Fields {
// find field in graphql schema
for _, def := range schemaModel.Fields {
if def.Name == field.Name {
// only add 'omitempty' on optional field as defined in graphql schema
if !def.Type.NonNull {
field.Tag = `json:"` + field.Name + `,omitempty"`
}
return build
break
}
}
}
}
}
return build
}
}
func Generate(ctx context.Context, cfg *config.Config, option ...api.Option) error {
var plugins []plugin.Plugin
if cfg.Model.IsDefined() {
p := modelgen.Plugin{
MutateHook: mutateHook,
MutateHook: mutateHook(cfg),
}
plugins = append(plugins, &p)
}

View File

@@ -146,12 +146,17 @@ func (d *Decoder) decode() error {
// We've just consumed the current token, which was the key.
// Read the next token, which should be the value.
// If it's of json.RawMessage type, decode the value.
if matchingFieldValue.Type() == reflect.TypeOf(json.RawMessage{}) {
// If it's of json.RawMessage or map type, decode the value.
switch matchingFieldValue.Type() {
case reflect.TypeOf(json.RawMessage{}):
var data json.RawMessage
err = d.jsonDecoder.Decode(&data)
tok = data
} else {
case reflect.TypeOf(map[string]interface{}{}):
var data map[string]interface{}
err = d.jsonDecoder.Decode(&data)
tok = data
default:
tok, err = d.jsonDecoder.Token()
}
@@ -183,7 +188,7 @@ func (d *Decoder) decode() error {
}
switch tok := tok.(type) {
case string, json.Number, bool, nil, json.RawMessage:
case string, json.Number, bool, nil, json.RawMessage, map[string]interface{}:
// Value.
for i := range d.vs {

View File

@@ -2,6 +2,7 @@ package introspection
import (
"fmt"
"strings"
"github.com/vektah/gqlparser/v2/ast"
)
@@ -12,6 +13,7 @@ func ParseIntrospectionQuery(url string, query Query) *ast.SchemaDocument {
Name: "remote",
BuiltIn: false,
}},
typeMap: query.Schema.Types.NameMap(),
}
if url != "" {
@@ -22,23 +24,27 @@ func ParseIntrospectionQuery(url string, query Query) *ast.SchemaDocument {
}
type parser struct {
sharedPosition *ast.Position
sharedPosition *ast.Position
typeMap map[string]*FullType
deprecatedDirectiveDefinition *ast.DirectiveDefinition
}
func (p parser) parseIntrospectionQuery(query Query) *ast.SchemaDocument {
var doc ast.SchemaDocument
typeMap := query.Schema.Types.NameMap()
doc.Schema = append(doc.Schema, p.parseSchemaDefinition(query, typeMap))
doc.Schema = append(doc.Schema, p.parseSchemaDefinition(query, p.typeMap))
doc.Position = p.sharedPosition
for _, typeVale := range typeMap {
doc.Definitions = append(doc.Definitions, p.parseTypeSystemDefinition(typeVale))
}
// parseDirectiveDefinition before parseTypeSystemDefinition
// Because SystemDefinition depends on DirectiveDefinition
for _, directiveValue := range query.Schema.Directives {
doc.Directives = append(doc.Directives, p.parseDirectiveDefinition(directiveValue))
}
p.deprecatedDirectiveDefinition = doc.Directives.ForName("deprecated")
for _, typeVale := range p.typeMap {
doc.Definitions = append(doc.Definitions, p.parseTypeSystemDefinition(typeVale))
}
return &doc
}
@@ -116,6 +122,7 @@ func (p parser) parseObjectFields(typeVale *FullType) ast.FieldList {
Arguments: args,
Type: typ,
Position: p.sharedPosition,
Directives: p.buildDeprecatedDirective(field),
}
fieldList = append(fieldList, fieldDefinition)
}
@@ -164,7 +171,7 @@ func (p parser) parseObjectTypeDefinition(typeVale *FullType) *ast.Definition {
Fields: fieldList,
EnumValues: enums,
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: builtInObject(typeVale),
}
}
@@ -182,7 +189,7 @@ func (p parser) parseInterfaceTypeDefinition(typeVale *FullType) *ast.Definition
Interfaces: interfaces,
Fields: fieldList,
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: false,
}
}
@@ -200,7 +207,7 @@ func (p parser) parseInputObjectTypeDefinition(typeVale *FullType) *ast.Definiti
Interfaces: interfaces,
Fields: fieldList,
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: false,
}
}
@@ -216,7 +223,7 @@ func (p parser) parseUnionTypeDefinition(typeVale *FullType) *ast.Definition {
Name: pointerString(typeVale.Name),
Types: unions,
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: false,
}
}
@@ -237,7 +244,7 @@ func (p parser) parseEnumTypeDefinition(typeVale *FullType) *ast.Definition {
Name: pointerString(typeVale.Name),
EnumValues: enums,
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: builtInEnum(typeVale),
}
}
@@ -247,7 +254,7 @@ func (p parser) parseScalarTypeExtension(typeVale *FullType) *ast.Definition {
Description: pointerString(typeVale.Description),
Name: pointerString(typeVale.Name),
Position: p.sharedPosition,
BuiltIn: true,
BuiltIn: builtInScalar(typeVale),
}
}
@@ -279,7 +286,7 @@ func (p parser) buildInputValue(input *InputValue) *ast.ArgumentDefinition {
if input.DefaultValue != nil {
defaultValue = &ast.Value{
Raw: pointerString(input.DefaultValue),
Kind: ast.Variable,
Kind: p.parseValueKind(typ),
Position: p.sharedPosition,
}
}
@@ -317,6 +324,67 @@ func (p parser) getType(typeRef *TypeRef) *ast.Type {
return ast.NamedType(pointerString(typeRef.Name), p.sharedPosition)
}
func (p parser) buildDeprecatedDirective(field *FieldValue) ast.DirectiveList {
var directives ast.DirectiveList
if field.IsDeprecated {
var arguments ast.ArgumentList
if field.DeprecationReason != nil {
arguments = append(arguments, &ast.Argument{
Name: "reason",
Value: &ast.Value{
Raw: *field.DeprecationReason,
Kind: ast.StringValue,
Position: p.sharedPosition,
},
Position: p.sharedPosition,
})
}
deprecatedDirective := &ast.Directive{
Name: "deprecated",
Arguments: arguments,
Position: p.sharedPosition,
ParentDefinition: nil,
Definition: p.deprecatedDirectiveDefinition,
Location: ast.LocationVariableDefinition,
}
directives = append(directives, deprecatedDirective)
}
return directives
}
func (p parser) parseValueKind(typ *ast.Type) ast.ValueKind {
typName := typ.Name()
if fullType, ok := p.typeMap[typName]; ok {
switch fullType.Kind {
case TypeKindEnum:
return ast.EnumValue
case TypeKindInputObject, TypeKindObject, TypeKindUnion, TypeKindInterface:
return ast.ObjectValue
case TypeKindList:
return ast.ListValue
case TypeKindNonNull:
panic(fmt.Sprintf("parseValueKind not match Type Name: %s", typ.Name()))
case TypeKindScalar:
switch typName {
case "Int":
return ast.IntValue
case "Float":
return ast.FloatValue
case "Boolean":
return ast.BooleanValue
case "String", "ID":
return ast.StringValue
default:
return ast.StringValue
}
}
}
panic(fmt.Sprintf("parseValueKind not match Type Name: %s", typ.Name()))
}
func pointerString(s *string) string {
if s == nil {
return ""
@@ -324,3 +392,28 @@ func pointerString(s *string) string {
return *s
}
func builtInScalar(fullType *FullType) bool {
name := pointerString(fullType.Name)
if strings.HasPrefix(name, "__") {
return true
}
switch name {
case "String", "Int", "Float", "Boolean", "ID":
return true
}
return false
}
func builtInEnum(fullType *FullType) bool {
name := pointerString(fullType.Name)
return strings.HasPrefix(name, "__")
}
func builtInObject(fullType *FullType) bool {
name := pointerString(fullType.Name)
return strings.HasPrefix(name, "__")
}