Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums can't be imported #595

Closed
steebchen opened this issue Mar 7, 2019 · 2 comments · Fixed by #634
Closed

Enums can't be imported #595

steebchen opened this issue Mar 7, 2019 · 2 comments · Fixed by #634
Labels
BC break v0.8.2 Fixed in 0.8.2

Comments

@steebchen
Copy link
Contributor

steebchen commented Mar 7, 2019

Expected Behaviour

I want to import existing enums:

models:
  Scope:
    model: github.com/steebchen/api/prisma.Scope

Actual Behavior

Some weird errors appear at runtime, after generating the files:

$ go run .
# github.com/steebchen/api/gqlgen
gqlgen/gqlgen.go:2393:2: not enough arguments to return
gqlgen/gqlgen.go:2393:11: ec.unmarshalInputScope undefined (type *executionContext has no field or method unmarshalInputScope)
gqlgen/gqlgen.go:2397:11: ec._Scope undefined (type *executionContext has no field or method _Scope)

Minimal graphql.schema and models to reproduce

# prisma/prisma.go
type Scope string

const (
	ScopeCompany  Scope = "Company"
	ScopeBranch   Scope = "Branch"
	ScopeEmployee Scope = "Employee"
	ScopeCustomer Scope = "Customer"
)

gqlgen@v0.8.1

@steebchen
Copy link
Contributor Author

The error appears because I (or my other code generator) didn't implement the following functions on the enum:

func (e Scope) IsValid() bool {
	switch e {
	case ScopeCompany, ScopeBranch, ScopeEmployee, ScopeCustomer:
		return true
	}
	return false
}

func (e Scope) String() string {
	return string(e)
}

func (e *Scope) UnmarshalGQL(v interface{}) error {
	str, ok := v.(string)
	if !ok {
		return fmt.Errorf("enums must be strings")
	}

	*e = Scope(str)
	if !e.IsValid() {
		return fmt.Errorf("%s is not a valid Scope", str)
	}
	return nil
}

func (e Scope) MarshalGQL(w io.Writer) {
	fmt.Fprint(w, strconv.Quote(e.String()))
}

Maybe the error message could be improved for clarification.

@steebchen
Copy link
Contributor Author

Would it be maybe useful to just generate those functions if they don't exist on an enum yet? Not sure if that makes sense for everybody, but in my case I just have generated enums from Prisma, but it obviously only generates the enum and not all method gqlgen requires.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BC break v0.8.2 Fixed in 0.8.2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants