99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.94k stars 1.16k forks source link

WithStack not declared by package errors #2489

Open bynov opened 1 year ago

bynov commented 1 year ago

What happened?

I generated models & resolvers and implemented several resolver's methods. In these methods I used errors.WithStack(err) from github.com/pkg/errors package.

When I run generate one more time, it automatically replaces github.com/pkg/errors with errors and after that complaining that method not declared.

What did you expect?

I expect that it won't change import for github.com/pkg/errors package.

Minimal graphql.schema and models to reproduce

Schema:

type Query {
  myUser(email: String!): User!
}

type User {
  id: String!
  email: String!
}

Resolvers with implementation

package graph

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.22

import (
    "context"
    "github.com/pkg/errors"

    "github.com/bynov/test-graphql/graph/model"
)

// MyUser is the resolver for the myUser field.
func (r *queryResolver) MyUser(ctx context.Context, email string) (*model.User, error) {
    if email == "1" {
        return nil, errors.WithStack(errors.New("test"))
    }

    return &model.User{
        ID:    "1",
        Email: email,
    }, nil
}

// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }

type queryResolver struct{ *Resolver }

gclgen.yml

# Where are all the schema files located? globs are supported eg  src/**/*.graphqls
schema:
  - graph/*.graphqls

# Where should the generated server code go?
exec:
  filename: graph/generated.go
  package: graph

# Uncomment to enable federation
# federation:
#   filename: graph/federation.go
#   package: graph

# Where should any generated models go?
model:
  filename: graph/model/models_gen.go
  package: model

# Where should the resolver implementations go?
resolver:
  layout: follow-schema
  dir: graph
  package: graph

# Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
# struct_tag: json

# Optional: turn on to use []Thing instead of []*Thing
# omit_slice_element_pointers: false

# Optional: turn off to make struct-type struct fields not use pointers
# e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing }
# struct_fields_always_pointers: true

# Optional: turn off to make resolvers return values instead of pointers for structs
# resolvers_always_return_pointers: true

# Optional: set to speed up generation time by not performing a final validation pass.
# skip_validation: true

# gqlgen will search for any type names in the schema in these go packages
# if they match it will use them, otherwise it will generate them.
autobind:
#  - "github.com/bynov/test-graphql/graph/model"

# This section declares type mapping between the GraphQL and go type systems
#
# The first line in each type will be used as defaults for resolver arguments and
# modelgen, the others will be allowed when binding to fields. Configure them to
# your liking
models:
  ID:
    model:
      - github.com/99designs/gqlgen/graphql.ID
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32
  Int:
    model:
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32

Adding new type:

type NewUser {
  id: String!
}

Run generate go run github.com/99designs/gqlgen generate and got error: validation failed: packages.Load: /Users/bynov/go/src/github.com/bynov/test-graphql/graph/schema.resolvers.go:17:22: WithStack not declared by package errors

versions

razorness commented 1 year ago

Duplicate of #1171