v0.17.56
v0.17.56 v0.17.55 v0.17.54 v0.17.53 v0.17.52 v0.17.51 v0.17.50 v0.17.49 v0.17.48 v0.17.47 v0.17.46 v0.17.45 v0.17.44 v0.17.43 v0.17.42 v0.17.41 v0.17.40 v0.17.39 v0.17.38 v0.17.37 master

Generated Model Extra Fields

Generation of additional extra fields for internal use
[edit]

Extra fields allows you to generate additional fields for your models. These fields can be used at runtime when implementing field resolvers.

Extending your models

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.

Inline config with directive

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
}