Filter Inputs
GraphQL
Ent
client.Todo.
Query().
Where(
todo.HasParent(),
todo.HasChildrenWith(
todo.StatusEQ(todo.StatusInProgress),
),
).
All(ctx)
Clone the code (optional)
The code for this tutorial is available under , and tagged (using Git) in each step. If you want to skip the basic setup and start with the initial version of the GraphQL server, you can clone the repository and run the program as follows:
git clone git@github.com:a8m/ent-graphql-example.git
cd ent-graphql-example
go run ./cmd/todo/
Go to your ent/entc.go
file, and add the 3 highlighted lines (extension options):
ent/entc.go
func main() {
ex, err := entgql.NewExtension(
entgql.WithWhereFilters(true),
entgql.WithConfigPath("../gqlgen.yml"),
entgql.WithSchemaPath("../ent.graphql"),
)
if err != nil {
log.Fatalf("creating entgql extension: %v", err)
}
opts := []entc.Option{
entc.Extensions(ex),
entc.TemplateDir("./template"),
}
if err := entc.Generate("./schema", &gen.Config{}, opts...); err != nil {
log.Fatalf("running ent codegen: %v", err)
}
}
The WithWhereFilters
option enables the filter generation, the WithConfigPath
configures the path to the gqlgen
config file, which allows the extension to more accurately map GraphQL to Ent types. The last option WithSchemaPath
, configures a path to a new, or an existing GraphQL schema to write the generated filters to.
go generate ./ent/...
Observe that Ent has generated <T>WhereInput
for each type in your schema in a file named ent/where_input.go
. Ent also generates a GraphQL schema as well (ent.graphql
), so you don’t need to autobind
them to gqlgen
manually. For example:
ent/where_input.go
ent.graphql
TodoWhereInput is used for filtering Todo objects.
Input was generated by ent.
"""
input TodoWhereInput {
not: TodoWhereInput
and: [TodoWhereInput!]
or: [TodoWhereInput!]
"""created_at field predicates"""
createdAt: Time
createdAtNEQ: Time
createdAtIn: [Time!]
createdAtNotIn: [Time!]
createdAtGT: Time
createdAtGTE: Time
createdAtLT: Time
createdAtLTE: Time
"""status field predicates"""
status: Status
statusNEQ: Status
statusIn: [Status!]
statusNotIn: [Status!]
# .. truncated ..
}
info" class="reference-link">
info
If your project contains more than 1 GraphQL schema (e.g. todo.graphql
and ent.graphql
), you should configure gqlgen.yml
file as follows:
schema:
- todo.graphql
# The ent.graphql schema was generated by Ent.
- ent.graphql
After running the code generation, we’re ready to complete the integration and expose the filtering capabilities in GraphQL:
1. Edit the GraphQL schema to accept the new filter types:
type Query {
todos(
after: Cursor,
first: Int,
before: Cursor,
last: Int,
orderBy: TodoOrder,
): TodoConnection
func (r *queryResolver) Todos(ctx context.Context, after *ent.Cursor, first *int, before *ent.Cursor, last *int, orderBy *ent.TodoOrder, where *ent.TodoWhereInput) (*ent.TodoConnection, error) {
return r.client.Todo.Query().
Paginate(ctx, after, first, before, last,
ent.WithTodoOrder(orderBy),
ent.WithTodoFilter(where.Filter),
)
}
As mentioned above, with the new GraphQL filter types, you can express the same Ent filters you use in your Go code.
Conjunction, disjunction and negation
The Not
, And
and Or
operators can be added using the not
, and
and or
fields. For example:
When multiple filter fields are provided, Ent implicitly adds the And
operator.
{
status: COMPLETED,
textHasPrefix: "GraphQL",
}
The above query will produce the following Ent query:
client.Todo.
Query().
Where(
todo.And(
todo.StatusEQ(todo.StatusCompleted),
todo.TextHasPrefix("GraphQL"),
)
).
All(ctx)
Edge/Relation filters
can be expressed in the same Ent syntax:
{
hasParent: true,
hasChildrenWith: {
status: IN_PROGRESS,
}
}
The above query will produce the following Ent query:
client.Todo.
Query().
Where(
todo.HasParent(),
todo.HasChildrenWith(
todo.StatusEQ(todo.StatusInProgress),
),
).
All(ctx)