Extensions

    Defining a New Extension

    All extension’s must implement the Extension interface:

    To simplify the development of new extensions, developers can embed to create extensions without implementing all methods:

    1. package hello
    2. // GreetExtension implements entc.Extension.
    3. type GreetExtension struct {
    4. entc.DefaultExtension
    5. }

    Ent supports adding that will be rendered during code generation. To bundle such external templates on an extension, implement the Templates method:

    templates/greet.tmpl

    1. {{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
    2. {{/* gotype: entgo.io/ent/entc/gen.Graph */}}
    3. {{ define "greet" }}
    4. {{/* Add the base header for the generated file */}}
    5. {{ $pkg := base $.Config.Package }}
    6. {{ template "header" $ }}
    7. {{ range $n := $.Nodes }}
    8. {{ $receiver := $n.Receiver }}
    9. func ({{ $receiver }} *{{ $n.Name }}) Greet() string {
    10. return "Hello, {{ $n.Name }}"
    11. }
    12. {{ end }}
    13. {{ end }}

    Adding Global Annotations

    1. type GreetingWord string
    2. // Name of the annotation. Used by the codegen templates.
    3. func (GreetingWord) Name() string {
    4. return "GreetingWord"
    5. }

    Then add it to the GreetExtension struct:

    1. type GreetExtension struct {
    2. entc.DefaultExtension
    3. word GreetingWord
    4. }

    Next, implement the Annotations method:

    Now, from within your templates you can access the GreetingWord annotation:

    1. func ({{ $receiver }} *{{ $n.Name }}) Greet() string {
    2. return "{{ $.Annotations.GreetingWord }}, {{ $n.Name }}"
    3. }

    The entc package provides an option to add a list of hooks (middlewares) to the code-generation phase. This option is ideal for adding custom validators for the schema, or for generating additional assets using the graph schema. To bundle code generation hooks with your extension, implement the Hooks method:

    1. return []gen.Hook{
    2. DisallowTypeName("Shalom"),
    3. }
    4. }
    5. // DisallowTypeName ensures there is no ent.Schema with the given name in the graph.
    6. func DisallowTypeName(name string) gen.Hook {
    7. return func(next gen.Generator) gen.Generator {
    8. return gen.GenerateFunc(func(g *gen.Graph) error {
    9. if node.Name == name {
    10. return fmt.Errorf("entc: validation failed, type named %q not allowed", name)
    11. }
    12. }
    13. return next.Generate(g)
    14. })
    15. }
    16. }

    Using an Extension in Code Generation

    ent/entc.go


    • elk is an extension that generates RESTful API endpoints from Ent schemas. The extension generates HTTP CRUD handlers from the Ent schema, as well as an OpenAPI JSON file. By using it, you can easily build a RESTful HTTP server for your application. Please note, that elk has been discontinued in favor of entoas. An implementation generator is in the works.
      Read this blog post on how to work with elk, and on how to generate an OpenAPI Specification.

    • entoas is an extension that originates from elk and was ported into its own extension and is now the official generator for and opinionated OpenAPI Specification document. You can use this to rapidly develop and document a RESTful HTTP server. There will be a new extension released soon providing a generated implementation integrating for the document provided by entoas using ent.

    • entgql
      This extension helps users build servers from Ent schemas. entgql integrates with gqlgen, a popular, schema-first Go library for building GraphQL servers. The extension includes the generation of type-safe GraphQL filters, which enable users to effortlessly map GraphQL queries to Ent queries.
      Follow to get started.