The Ent Extension API facilitates the creation of code-generation extensions that bundle together codegen hooks, templates and annotations to create reusable components that add new rich functionality to Ent's core. For example, Ent's entgql plugin exposes an
Extension that automatically generates GraphQL servers from an Ent schema.
All extension's must implement the Extension interface:
To simplify the development of new extensions, developers can embed entc.DefaultExtension to create extensions without implementing all methods:
Ent supports adding external templates that will be rendered during code generation. To bundle such external templates on an extension, implement the
Annotations are a convenient way to supply users of our extension with an API to modify the behavior of code generation. To add annotations to our extension, implement the
Annotations method. Let's say in our
GreetExtension we want to provide users with the ability to configure the greeting word in the generated code:
Then add it to the
Next, implement the
Now, from within your templates you can access the
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
To use an extension in our code-generation configuration, use
entc.Extensions, a helper method that returns an
entc.Option that applies our chosen extensions:
elkis 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.
Read this blog post on how to work with
elk, and this blog post on how to generate an OpenAPI Specification.
This extension helps users build GraphQL servers from Ent schemas.
entgqlintegrates 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 this tutorial to get started.
entprotogenerates Protobuf message definitions and gRPC service definitions from Ent schemas. The project also includes
protoc(Protobuf compiler) plugin that is used to generate a working implementation of the gRPC service definition generated by Entproto. In this manner, we can easily create a gRPC server that can serve requests to our service without writing any code (aside from defining the Ent schema)!
To learn how to use and set up
entproto, read this tutorial. For more background you can read this blog post, or this blog post discussing more
entvizis an extension that generates visual diagrams from Ent schemas. These diagrams visualize the schema in a web browser, and stay updated as we continue coding.
entvizcan be configured in such a way that every time we regenerate the schema, the diagram is automatically updated, making it easy to view the changes being made.
Learn how to integrate
entvizin your project in this blog post.