简体   繁体   中英

Golang gqlgen error trying to import model into resolver.go

Hello I was playing arround with Golang and wanted to try GraphQL instead of a RESTful approach. So I decided to use https://github.com/99designs/gqlgen to implement it.

I followed to getting started guide and everything went fine until I pasted the resolver.go code.

My Folder structure:

├── go.mod
├── go.sum
├── gqlgen.yml               
├── graph
│   ├── generated            
│   │   └── generated.go
│   ├── model                
│   │   └── models_gen.go
│   ├── resolver.go          
│   ├── schema.graphqls      
│   └── schema.resolvers.go  
└── server.go                

My resolver.go

package graph


import (
    "context"
    "fmt"
    "math/rand"
)

type Resolver struct {
    todos []*model.Todo
}

func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
    todo := &model.Todo{
        Text: input.Text,
        ID:   fmt.Sprintf("T%d", rand.Int()),
        User: &model.User{ID: input.UserID, Name: "user " + input.UserID},
    }
    r.todos = append(r.todos, todo)
    return todo, nil
}

func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) {
    return r.todos, nil
}

Obviously it would throw me an error since I did not import the model.

So I tried to import it like that "[MODULENAME]/graph/model" This resulted in:

graph/schema.resolvers.go:14:28: (*mutationResolver).CreateTodo redeclared in this block
        previous declaration at graph/resolver.go:18:6
graph/schema.resolvers.go:18:25: (*queryResolver).Todos redeclared in this block
        previous declaration at graph/resolver.go:28:6

If I try [MODULENAME]/model

It throws me this error:

cannot find module providing package [MODULENAME]/model: unrecognized import path "[MODULENAME]/model": https fetch: Get "[MODULENAME]/model?go-get=1": dial tcp [MYSERVER IP]: connect: connection refused

I am new to Golang and to Modules in generl. I tried to solve but somehow I am super confused. I woul really appreciate any help.

Kind regards

Your issue is that you declared the (*mutationResolver).CreateTodo and (*queryResolver).Todos functions multiple times. First you defined them in graph/resolver.go file, and the second time in the graph/schema.resolvers.go file which is generated by gqlgen.

The graph/schema.resolvers.go file will be regenerated each time, and will contain all the resolver implementations that couldn't be matched from your "state", which is your Resolver struct, or from the model folder.

Solutions :

  • You can just easily remove those declarations from the graph/schema.resolvers.go file
  • Or just run the gqlgen generate command again, since it will find your implementations and remove those lines from the newly generated file

A note on modules and import paths :

Your first import path was great, if you have your go.mod file in the root, you can import all the packages inside them using the yourmodulename/path/to/folder format. In this case, the yourmodulename/graph/model is your import path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM