Generated Model Extra Fields
Generation of additional extra fields for internal use
[edit]
master
branch. The latest version is v0.17.49.
Extra fields allows you to generate additional fields for your models. These fields can be used at runtime when implementing field resolvers.
Imagine you have a model named User and you want to extend a generated struct with additional data used in your service.
The schema is:
type User {
id: ID!
name: String!
}
Extra fields can be defined in gqlgen.yaml configuration:
models:
User:
extraFields:
Session:
description: "A Session used by this user"
type: "github.com/author/mypkg.Session"
overrideTags: 'xml:"session"'
The generated code would look like:
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.
type User struct {
ID string
Name string
// A Session used by this user.
Session mypkg.Session `xml:"session"`
}
After these steps you have an extra field for your server implementation and the field is not being exposed to a caller.
To start using it you first need to define it:
directive @goExtraField(
name: String
type: String!
overrideTags: String
description: String
) repeatable on OBJECT | INPUT_OBJECT
Now you can use these directive when defining types in your schema:
type User
@goExtraField(
name: "Session"
type: "github.com/author/mypkg.Session"
description: "A Session used by this user"
overrideTags: "xml:\"session\""
)
@goExtraField(name: "Activated", type: "bool")
@goExtraField(
type: "time.Time"
description: "type without name will be embedded"
) {
id: ID
name: String
}
The generated code would look like:
type User struct {
ID string
Name string
// A Session used by this user.
Session mypkg.Session `xml:"session"`
Activated bool
time.Time
}