v0.17.50
v0.17.50 v0.17.61 v0.17.60 v0.17.59 v0.17.58 v0.17.57 v0.17.56 v0.17.55 v0.17.54 v0.17.53 v0.17.52 v0.17.51 v0.17.49 v0.17.48 v0.17.47 v0.17.46 v0.17.45 v0.17.44 v0.17.43 v0.17.42 master

Enum binding

Extended enum to model binding tips
[edit]
You are looking at the docs for an older version (v0.17.50). The latest version is v0.17.61.

Using the following recipe you can bind enum values to specific const or variable. Both typed and untyped binding are supported.

More examples can be found in _examples/enum.

Binding target go model enums:

package model

type EnumTyped int

const (
	EnumTypedOne EnumTyped = iota + 1
	EnumTypedTwo
)

const (
	EnumUntypedOne = iota + 1
	EnumUntypedTwo
)

Binding using @goModel and @goEnum directives:

directive @goModel(
    model: String
    models: [String!]
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION

directive @goEnum(
    value: String
) on ENUM_VALUE

type Query {
    example(arg: EnumUntyped): EnumTyped
}

enum EnumTyped @goModel(model: "./model.EnumTyped") {
    ONE @goEnum(value: "./model.EnumTypedOne")
    TWO @goEnum(value: "./model.EnumTypedTwo")
}

enum EnumUntyped @goModel(model: "github.com/99designs/gqlgen/graphql.Int") {
    ONE @goEnum(value: "./model.EnumUntypedOne")
    TWO @goEnum(value: "./model.EnumUntypedTwo")
}

The same result can be achieved using the config:

models:
  EnumTyped:
    model: ./model.EnumTyped
    enum_values:
      ONE:
        value: ./model.EnumTypedOne
      TWO:
        value: ./model.EnumTypedTwo
  EnumUntyped:
    model: github.com/99designs/gqlgen/graphql.Int
    enum_values:
      ONE:
        value: ./model.EnumUntypedOne
      TWO:
        value: ./model.EnumUntypedTwo