Filter Inputs

GraphQL

Ent

  1. client.Todo.
  2. Query().
  3. Where(
  4. todo.HasParent(),
  5. todo.HasChildrenWith(
  6. todo.StatusEQ(todo.StatusInProgress),
  7. ),
  8. ).
  9. 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:

  1. git clone git@github.com:a8m/ent-graphql-example.git
  2. cd ent-graphql-example
  3. go run ./cmd/todo/

Go to your ent/entc.go file, and add the 3 highlighted lines (extension options):

ent/entc.go

  1. func main() {
  2. ex, err := entgql.NewExtension(
  3. entgql.WithWhereFilters(true),
  4. entgql.WithConfigPath("../gqlgen.yml"),
  5. entgql.WithSchemaPath("../ent.graphql"),
  6. )
  7. if err != nil {
  8. log.Fatalf("creating entgql extension: %v", err)
  9. }
  10. opts := []entc.Option{
  11. entc.Extensions(ex),
  12. entc.TemplateDir("./template"),
  13. }
  14. if err := entc.Generate("./schema", &gen.Config{}, opts...); err != nil {
  15. log.Fatalf("running ent codegen: %v", err)
  16. }
  17. }

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.

  1. 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

  1. TodoWhereInput is used for filtering Todo objects.
  2. Input was generated by ent.
  3. """
  4. input TodoWhereInput {
  5. not: TodoWhereInput
  6. and: [TodoWhereInput!]
  7. or: [TodoWhereInput!]
  8. """created_at field predicates"""
  9. createdAt: Time
  10. createdAtNEQ: Time
  11. createdAtIn: [Time!]
  12. createdAtNotIn: [Time!]
  13. createdAtGT: Time
  14. createdAtGTE: Time
  15. createdAtLT: Time
  16. createdAtLTE: Time
  17. """status field predicates"""
  18. status: Status
  19. statusNEQ: Status
  20. statusIn: [Status!]
  21. statusNotIn: [Status!]
  22. # .. truncated ..
  23. }
info" class="reference-link">Filter Inputs - 图2info

If your project contains more than 1 GraphQL schema (e.g. todo.graphql and ent.graphql), you should configure gqlgen.yml file as follows:

  1. schema:
  2. - todo.graphql
  3. # The ent.graphql schema was generated by Ent.
  4. - 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:

  1. type Query {
  2. todos(
  3. after: Cursor,
  4. first: Int,
  5. before: Cursor,
  6. last: Int,
  7. orderBy: TodoOrder,
  8. ): TodoConnection
  1. 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) {
  2. return r.client.Todo.Query().
  3. Paginate(ctx, after, first, before, last,
  4. ent.WithTodoOrder(orderBy),
  5. ent.WithTodoFilter(where.Filter),
  6. )
  7. }

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.

  1. {
  2. status: COMPLETED,
  3. textHasPrefix: "GraphQL",
  4. }

The above query will produce the following Ent query:

  1. client.Todo.
  2. Query().
  3. Where(
  4. todo.And(
  5. todo.StatusEQ(todo.StatusCompleted),
  6. todo.TextHasPrefix("GraphQL"),
  7. )
  8. ).
  9. All(ctx)

Edge/Relation filters

can be expressed in the same Ent syntax:

  1. {
  2. hasParent: true,
  3. hasChildrenWith: {
  4. status: IN_PROGRESS,
  5. }
  6. }

The above query will produce the following Ent query:

  1. client.Todo.
  2. Query().
  3. Where(
  4. todo.HasParent(),
  5. todo.HasChildrenWith(
  6. todo.StatusEQ(todo.StatusInProgress),
  7. ),
  8. ).
  9. All(ctx)