master
master

Enum binding

Extended enum to model binding tips
[edit]
You are looking at the docs for the unreleased master branch. The latest version is master.

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