Support Go 1.18: Upgrade gqlgen to v0.17.2 (#2443)

* Upgrade gqlgen to v0.17.2

This enables builds on Go 1.18. github.com/vektah/gqlparser is upgraded
to the newest version too.

Getting this to work is a bit of a hazzle. I had to first remove
vendoring from the repository, perform the upgrade and then re-introduce
the vendor directory. I think gqlgens analysis went wrong for some
reason on the upgrade. It would seem a clean-room installation fixed it.

* Bump project to 1.18

* Update all packages, address gqlgenc breaking changes

* Let `go mod tidy` handle the go.mod file

* Upgrade linter to 1.45.2

* Introduce v1.45.2 of the linter

The linter now correctly warns on `strings.Title` because it isn't
unicode-aware. Fix this by using the suggested fix from x/text/cases
to produce unicode-aware strings.

The mapping isn't entirely 1-1 as this new approach has a larger iface:
it spans all of unicode rather than just ASCII. It coincides for ASCII
however, so things should be largely the same.

* Ready ourselves for errchkjson and contextcheck.

* Revert dockerfile golang version changes for now

Co-authored-by: Kermie <kermie@isinthe.house>
Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
SmallCoccinelle
2022-04-02 09:08:14 +02:00
committed by GitHub
parent e5c4241180
commit 45f700d6ea
516 changed files with 43496 additions and 10015 deletions

View File

@@ -1,6 +1,7 @@
package codegen
import (
"errors"
"fmt"
"go/types"
"log"
@@ -10,7 +11,6 @@ import (
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/v2/ast"
)
@@ -25,6 +25,7 @@ type Field struct {
Args []*FieldArgument // A list of arguments to be passed to this field
MethodHasContext bool // If this is bound to a go method, does the method also take a context
NoErr bool // If this is bound to a go method, does that method have an error as the second argument
VOkFunc bool // If this is bound to a go method, is it of shape (interface{}, bool)
Object *Object // A link back to the parent object
Default interface{} // The default value
Stream bool // does this field return a channel?
@@ -50,7 +51,7 @@ func (b *builder) buildField(obj *Object, field *ast.FieldDefinition) (*Field, e
var err error
f.Default, err = field.DefaultValue.Value(nil)
if err != nil {
return nil, errors.Errorf("default value %s is not valid: %s", field.Name, err.Error())
return nil, fmt.Errorf("default value %s is not valid: %w", field.Name, err)
}
}
@@ -64,6 +65,9 @@ func (b *builder) buildField(obj *Object, field *ast.FieldDefinition) (*Field, e
if err = b.bindField(obj, &f); err != nil {
f.IsResolver = true
if errors.Is(err, config.ErrTypeNotFound) {
return nil, err
}
log.Println(err.Error())
}
@@ -88,6 +92,11 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
if err != nil {
errret = err
}
for _, dir := range obj.Directives {
if dir.IsLocation(ast.LocationInputObject) {
dirs = append(dirs, dir)
}
}
f.Directives = append(dirs, f.Directives...)
}
}()
@@ -110,6 +119,7 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
f.GoReceiverName = "ec"
f.GoFieldName = "__resolve_entities"
f.MethodHasContext = true
f.NoErr = true
return nil
case f.Name == "_service":
f.GoFieldType = GoFieldMethod
@@ -152,6 +162,8 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
sig := target.Type().(*types.Signature)
if sig.Results().Len() == 1 {
f.NoErr = true
} else if s := sig.Results(); s.Len() == 2 && s.At(1).Type().String() == "bool" {
f.VOkFunc = true
} else if sig.Results().Len() != 2 {
return fmt.Errorf("method has wrong number of args")
}
@@ -167,10 +179,13 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
params = types.NewTuple(vars...)
}
if err = b.bindArgs(f, params); err != nil {
return errors.Wrapf(err, "%s:%d", pos.Filename, pos.Line)
// Try to match target function's arguments with GraphQL field arguments
newArgs, err := b.bindArgs(f, params)
if err != nil {
return fmt.Errorf("%s:%d: %w", pos.Filename, pos.Line, err)
}
// Try to match target function's return types with GraphQL field return type
result := sig.Results().At(0)
tr, err := b.Binder.TypeReference(f.Type, result.Type())
if err != nil {
@@ -181,6 +196,7 @@ func (b *builder) bindField(obj *Object, f *Field) (errret error) {
f.GoFieldType = GoFieldMethod
f.GoReceiverName = "obj"
f.GoFieldName = target.Name()
f.Args = newArgs
f.TypeReference = tr
return nil
@@ -237,7 +253,7 @@ func (b *builder) findBindTarget(t types.Type, name string) (types.Object, error
return foundField, nil
case foundField != nil && foundMethod != nil:
// Error
return nil, errors.Errorf("found more than one way to bind for %s", name)
return nil, fmt.Errorf("found more than one way to bind for %s", name)
}
// Search embeds
@@ -262,7 +278,7 @@ func (b *builder) findBindStructTagTarget(in types.Type, name string) (types.Obj
tags := reflect.StructTag(t.Tag(i))
if val, ok := tags.Lookup(b.Config.StructTag); ok && equalFieldName(val, name) {
if found != nil {
return nil, errors.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", b.Config.StructTag, val)
return nil, fmt.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", b.Config.StructTag, val)
}
found = field
@@ -300,7 +316,7 @@ func (b *builder) findBindMethoderTarget(methodFunc func(i int) *types.Func, met
}
if found != nil {
return nil, errors.Errorf("found more than one matching method to bind for %s", name)
return nil, fmt.Errorf("found more than one matching method to bind for %s", name)
}
found = method
@@ -322,7 +338,7 @@ func (b *builder) findBindFieldTarget(in types.Type, name string) (types.Object,
}
if found != nil {
return nil, errors.Errorf("found more than one matching field to bind for %s", name)
return nil, fmt.Errorf("found more than one matching field to bind for %s", name)
}
found = field
@@ -366,7 +382,7 @@ func (b *builder) findBindStructEmbedsTarget(strukt *types.Struct, name string)
}
if f != nil && found != nil {
return nil, errors.Errorf("found more than one way to bind for %s", name)
return nil, fmt.Errorf("found more than one way to bind for %s", name)
}
if f != nil {
@@ -388,7 +404,7 @@ func (b *builder) findBindInterfaceEmbedsTarget(iface *types.Interface, name str
}
if f != nil && found != nil {
return nil, errors.Errorf("found more than one way to bind for %s", name)
return nil, fmt.Errorf("found more than one way to bind for %s", name)
}
if f != nil {
@@ -417,7 +433,8 @@ func (f *Field) ImplDirectives() []*Directive {
loc = ast.LocationInputFieldDefinition
}
for i := range f.Directives {
if !f.Directives[i].Builtin && f.Directives[i].IsLocation(loc, ast.LocationObject) {
if !f.Directives[i].Builtin &&
(f.Directives[i].IsLocation(loc, ast.LocationObject) || f.Directives[i].IsLocation(loc, ast.LocationInputObject)) {
d = append(d, f.Directives[i])
}
}
@@ -452,7 +469,10 @@ func (f *Field) GoNameUnexported() string {
}
func (f *Field) ShortInvocation() string {
return fmt.Sprintf("%s().%s(%s)", f.Object.Definition.Name, f.GoFieldName, f.CallArgs())
if f.Object.Kind == ast.InputObject {
return fmt.Sprintf("%s().%s(ctx, &it, data)", strings.Title(f.Object.Definition.Name), f.GoFieldName)
}
return fmt.Sprintf("%s().%s(%s)", strings.Title(f.Object.Definition.Name), f.GoFieldName, f.CallArgs())
}
func (f *Field) ArgsFunc() string {
@@ -472,6 +492,13 @@ func (f *Field) ResolverType() string {
}
func (f *Field) ShortResolverDeclaration() string {
if f.Object.Kind == ast.InputObject {
return fmt.Sprintf("(ctx context.Context, obj %s, data %s) error",
templates.CurrentImports.LookupType(f.Object.Reference()),
templates.CurrentImports.LookupType(f.TypeReference.GO),
)
}
res := "(ctx context.Context"
if !f.Object.Root {