ent/entc.go 会忽略build tag，并通过
go generate 命令. 完整的示例可以在 ent/contrib 仓库 中找到。
3. 在你的项目中运行 codegen 命令:
在运行 codegen 后，以下附加组件将被添加到你的项目。
A new file named
ent/gql_node.go was created that implements the Relay Node interface.
如果您在schema migration中使用 Universal IDs 选项， NodeType 来源于id的值，可以按照下面的方式使用：
分页模板根据 Relay Cursor Connections Spec 添加了分页支持。 更多关于Relay Spec的信息 可以在其 网站 中找到。
The ordering option allows us to apply an ordering on the edges returned from a connection.
- The generated types will be
autobinded to GraphQL types if a naming convention is preserved (see example below).
- Ordering can only be defined on ent fields (no edges).
- Ordering fields should normally be indexed to avoid full table DB scan.
- Pagination queries can be sorted by a single field (no order by ... then by ... semantics).
Let's go over the steps needed in order to add ordering to an existing GraphQL type. The code example is based on a todo-app that can be found in ent/contrib/entql/todo.
Ordering can be defined on any comparable field of ent by annotating it with
entgql.Annotation. Note that the given
OrderField name must match its enum value in graphql schema.
That's all the schema changes required, make sure to run
go generate to apply them.
Next we need to define the ordering types in graphql schema:
Note that the naming must take the form of
autobinding to the generated ent types. Alternatively @goModel directive can be used for manual type binding.
That's all for the GraphQL schema changes, let's run
gqlgen code generation.
Head over to the Todo resolver and update it to pass
orderBy argument to
The collection template adds support for automatic GraphQL fields collection for ent-edges using eager-loading. That means, if a query asks for nodes and their edges, entgql will add automatically
With<E> steps to the root query, and as a result, the client will execute constant number of queries to the database - and it works recursively.
For example, given this GraphQL query:
The client will execute 1 query for getting the users, 1 for getting the photos, and another 2 for getting the posts, and their comments (4 in total). This logic works both for root queries/resolvers and for the node(s) API.
In order to configure this option to specific edges, use the
entgql.Annotation as follows:
The GraphQL extension generates also edge-resolvers for the nodes under the
gql_edge.go file as follows:
However, if you need to explicitly write these resolvers by hand, you can add the
forceResolver option to your GraphQL schema:
Then, you can implement it on your type resolver.
The enum template implements the MarshalGQL/UnmarshalGQL methods for enums generated by ent.
entgql.Transactioner handler executes each GraphQL mutation in a transaction. The injected client for the resolver is a transactional
ent.Client. Hence, code that uses
ent.Client won't need to be changed. In order to use it, follow these steps:
1. In the GraphQL server initialization, use the
entgql.Transactioner handler as follows:
2. Then, in the GraphQL mutations, use the client from context as follows:
The ent/contrib contains several examples at the moment:
- A complete GraphQL server with a simple Todo App with numeric ID field
- The same Todo App in 1, but with UUID type for the ID field
- The same Todo App in 1 and 2, but with a prefixed ULID or
PULIDas the ID field. This example supports the Relay Node API by prefixing IDs with the entity type rather than employing the ID space partitioning in Universal IDs.