fieldgen

package
v0.14.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

FieldGen

A graphql plugin to conditionally add fields to schema(s) which are not part of the existing model.

Overview

This plugin is for users of the ent project who use ent in conjunction with GraphQL. When you add additional fields with ent using their model/fields/additional, ex:

{{ define "model/fields/additional" }}
        // CreatedByMeow includes the meow details about the user or service that created the object
        CreatedByMeow string `json:"createdByMeow,omitempty"`
        // UpdatedByMeow includes the meow details about the user or service that last updated the object
        UpdatedByMeow string `json:"updatedByMeow,omitempty"`
    {{- end }}
{{ end }}

You can manually extend your graphql schema, ex:

extend type Meowzers {
    CreatedByMeow: String
    UpdatedByMeow: String
}

However, manually managing these fields with many scehmas (and ensuring future schemas are extended similarly) becomes difficult and unwieldy, which is where the fieldgen plugin comes into play.

Usage

Once you've added your additional fields (in whatever manner you want) you can use this plugin to add those fields to your graphql schema.

In the below example, we want a new field on every existing schema that has the createdByID and updatedByID field:

var extraFields = []fieldgen.AdditionalField{
	{
		Name:                         "createdByMeow",
		Type:                         "String",
		NonNull:                      true,
		Description:                  "The cat who created the object",
		AddToSchemaWithExistingField: "createdByID",
	},
	{
		Name:                         "updatedByMeow",
		Type:                         "String",
		NonNull:                      true,
		Description:                  "The cat who last updated the object",
		AddToSchemaWithExistingField: "updatedByID",
	},
}

Add the plugin to your generate function:

	if err := api.Generate(cfg,
		api.AddPlugin(fieldgen.NewExtraFieldsGen(extraFields)), // add the fieldgen plugin
	); err != nil {
		log.Fatal().Err(err).Msg("failed to generate gqlgen server")
	}

This uses the MutateConfig plugin option so the fields are added before the code is generated.

After running generate, you should see the additional fields in your model and graphql schema.

CustomTypes

If you are using a custom type, you can either manually add the scalar to your schema or you can use the CustomType field instead of Type. This will automatically add the type to the respective sources.

Documentation

Overview

Package fieldgen adds additional fields to the graphql schema

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalField

type AdditionalField struct {
	// Name of the field to add
	Name string
	// Type of the field to add
	Type string
	// CustomType is an non-standard go type to use for the field, if set will override the Type field
	// If the Scalar has already been defined manually, add it to Type instead, this will
	// programmatically add the scalar to the schema
	CustomType string
	// NonNull indicates if the field is required
	NonNull bool
	// Description of the field
	Description string
	// AddToSchemaWithName is the name of the schema to add the field to, if empty will add to all schemas
	// unless AddToSchemaWithExistingField is set
	AddToSchemaWithNames []string
	// AddToSchemaWithExistingField will add to any schema with the existing field, if empty will add to all schemas
	// unless AddToSchemaWithName is set
	AddToSchemaWithExistingField string
}

AdditionalField is a struct to with details about the additional field to add to the schema

type ExtraFields

type ExtraFields struct {
	// FieldDefs is a list of additional fields to add to the schema
	FieldDefs []AdditionalField
}

ExtraFields is a struct to hold the additional fields to add to the schema

func NewExtraFieldsGen

func NewExtraFieldsGen(fields []AdditionalField) *ExtraFields

NewExtraFieldsGen returns a new ExtraFields plugin

func (*ExtraFields) MutateConfig

func (f *ExtraFields) MutateConfig(cfg *config.Config) error

MutateConfig satisfies the plugin interface

func (*ExtraFields) Name

func (f *ExtraFields) Name() string

Name returns the plugin name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL