weixin_39710249
weixin_39710249
2021-01-12 18:49

Generated models do not implement user defined interfaces

What happened?

Generated models do not fulfill user defined interfaces provided through models configuration.

go
// models/generated.go
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.

package models

type MyObect struct {
    ID string `json:"id"`
}

func (MyObect) IsMyInterface() {}

What did you expect?

An error preventing me from doing this, OR the generated models need to fulfill the user defined interface, i.e.

go
// models/generated.go
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.

package models

type MyObect struct {
    ID string `json:"id"`
}

func (MyObect) Baz() {}

Minimal graphql.schema and models to reproduce

yml
# .gqlgen.yml
exec:
  filename: generated/exec.go
model:
  filename: models/generated.go

models:
  MyInterface:
    model: github.com/99designs/gqlgen/example/interface_failure/models.MyInterface

graphql
# schema.graphql
interface MyInterface  {
    id: ID!
}

type MyObect implements MyInterface {
    id: ID!
}

type Query {
    myObjects: MyObect!
}
go
// models/my_interface.go
package models

type MyInterface interface {
    Baz()
}

versions

  • gqlgen version? dev, from master with test case added to the example/ directory
  • go version? go version go1.12.4 linux/amd64
  • dep or go modules? master branch go.mod

该提问来源于开源项目:99designs/gqlgen

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39809168 weixin_39809168 4月前

    I also found this working through https://gqlgen.com/getting-started/

    See https://github.com/ii/org/blob/master/research/gqlgen.org#errors

    Where I was writing up an org file for it and ran into:

    
    -*- mode: compilation; default-directory: "~/gqlgen-todos/" -*-
    Compilation started at Sun May 19 07:11:45
    
    go run server/server.go 
    # github.com/hh/gqlgen-todos
    ./resolver.go:17:24: cannot use &queryResolver literal (type *queryResolver) as type QueryResolver in return argument:
        *queryResolver does not implement QueryResolver (wrong type for Todos method)
            have Todos(context.Context) ([]Todo, error)
            want Todos(context.Context) ([]*Todo, error)
    
    Compilation exited abnormally with code 2 at Sun May 19 07:11:45
    
    点赞 评论 复制链接分享
  • weixin_39809168 weixin_39809168 4月前

    I added my environment:

    https://github.com/ii/org/blob/master/research/gqlgen.org#my-environment

    environ

    shell
    go version
    
    go version go1.12.3 linux/amd64
    

    go env

    shell
    go env
    
    GOARCH="amd64"
    GOBIN=""
    GOCACHE="/home/hippie/.cache/go-build"
    GOEXE=""
    GOFLAGS=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/hippie/go"
    GOPROXY=""
    GORACE=""
    GOROOT="/usr/local/go"
    GOTMPDIR=""
    GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
    GCCGO="gccgo"
    CC="gcc"
    CXX="g++"
    CGO_ENABLED="1"
    GOMOD="/home/hippie/gqlgen-todos/go.mod"
    CGO_CFLAGS="-g -O2"
    CGO_CPPFLAGS=""
    CGO_CXXFLAGS="-g -O2"
    CGO_FFLAGS="-g -O2"
    CGO_LDFLAGS="-g -O2"
    PKG_CONFIG="pkg-config"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build045453696=/tmp/go-build -gno-record-gcc-switches"
    
    点赞 评论 复制链接分享
  • weixin_39809168 weixin_39809168 4月前

    I updated the documentation to fix #714 in #718.

    点赞 评论 复制链接分享
  • weixin_39875842 weixin_39875842 4月前

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

    点赞 评论 复制链接分享

相关推荐