Skip to main content

GraphQL Integration

The Ent framework supports GraphQL using the 99designs/gqlgen library and provides various integrations, such as:

  1. Generating a GraphQL schema for nodes and edges defined in an Ent schema.
  2. Auto-generated Query and Mutation resolvers and provide seamless integration with the Relay framework.
  3. Filtering, pagination (including nested) and compliant support with the Relay Cursor Connections Spec.
  4. Efficient field collection to overcome the N+1 problem without requiring data loaders.
  5. Transactional mutations to ensure consistency in case of failures.

Check out the website's GraphQL tutorial for more information.

Quick Introduction​

In order to enable the entgql extension to your project, you need to use the entc (ent codegen) package as described here. Follow these 3 steps to enable it to your project:

1. Create a new Go file named ent/entc.go, and paste the following content:

ent/entc.go
// +build ignore

package main

import (
"log"

"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
"entgo.io/contrib/entgql"
)

func main() {
ex, err := entgql.NewExtension()
if err != nil {
log.Fatalf("creating entgql extension: %v", err)
}
if err := entc.Generate("./schema", &gen.Config{}, entc.Extensions(ex)); err != nil {
log.Fatalf("running ent codegen: %v", err)
}
}

2. Edit the ent/generate.go file to execute the ent/entc.go file:

ent/generate.go
package ent

//go:generate go run -mod=mod entc.go

Note that ent/entc.go is ignored using a build tag, and it's executed by the go generate command through the generate.go file. The full example can be found in the ent/contrib repository.

3. Run codegen for your ent project:

go generate ./...

After running codegen, the following add-ons will be added to your project.

Node API​

A new file named ent/gql_node.go was created that implements the Relay Node interface.

In order to use the new generated ent.Noder interface in the GraphQL resolver, add the Node method to the query resolver, and look at the configuration section to understand how to use it.

If you are using the Universal IDs option in the schema migration, the NodeType is derived from the id value and can be used as follows:

func (r *queryResolver) Node(ctx context.Context, id int) (ent.Noder, error) {
return r.client.Noder(ctx, id)
}

However, if you use a custom format for the global unique identifiers, you can control the NodeType as follows:

func (r *queryResolver) Node(ctx context.Context, guid string) (ent.Noder, error) {
typ, id := parseGUID(guid)
return r.client.Noder(ctx, id, ent.WithFixedNodeType(typ))
}

GQL Configuration​

Here's a configuration example for a todo app as exists in ent/contrib/entgql/todo.

schema:
- todo.graphql

resolver:
# Tell gqlgen to generate resolvers next to the schema file.
layout: follow-schema
dir: .

# gqlgen will search for any type names in the schema in the generated
# ent package. If they match it will use them, otherwise it will new ones.
autobind:
- entgo.io/contrib/entgql/internal/todo/ent

models:
ID:
model:
- github.com/99designs/gqlgen/graphql.IntID
Node:
model:
# ent.Noder is the new interface generated by the Node template.
- entgo.io/contrib/entgql/internal/todo/ent.Noder

Pagination​

The pagination template adds a pagination support according to the Relay Cursor Connections Spec. More info about the Relay Spec can be found in its website.

Connection Ordering​

The ordering option allows us to apply an ordering on the edges returned from a connection.

Usage Notes​

  • The generated types will be autobinded to GraphQL types if a naming convention is preserved (see example below).
  • 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).

Example​

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.

Defining order fields in ent/schema​

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.

func (Todo) Fields() []ent.Field {
return []ent.Field{
field.Time("created_at").
Default(time.Now).
Immutable().
Annotations(
entgql.OrderField("CREATED_AT"),
),
field.Enum("status").
NamedValues(
"InProgress", "IN_PROGRESS",
"Completed", "COMPLETED",
).
Annotations(
entgql.OrderField("STATUS"),
),
field.Int("priority").
Default(0).
Annotations(
entgql.OrderField("PRIORITY"),
),
field.Text("text").
NotEmpty().
Annotations(
entgql.OrderField("TEXT"),
),
}
}

That's all the schema changes required, make sure to run go generate to apply them.

Define ordering types in GraphQL schema​

Next we need to define the ordering types in graphql schema:

enum OrderDirection {
ASC
DESC
}

enum TodoOrderField {
CREATED_AT
PRIORITY
STATUS
TEXT
}

input TodoOrder {
direction: OrderDirection!
field: TodoOrderField
}

Note that the naming must take the form of <T>OrderField / <T>Order for autobinding to the generated ent types. Alternatively @goModel directive can be used for manual type binding.

Adding orderBy argument to the pagination query​

type Query {
todos(
after: Cursor
first: Int
before: Cursor
last: Int
orderBy: TodoOrder
): TodoConnection!
}

That's all for the GraphQL schema changes, let's run gqlgen code generation.

Update the underlying resolver​

Head over to the Todo resolver and update it to pass orderBy argument to .Paginate() call:

func (r *queryResolver) Todos(ctx context.Context, after *ent.Cursor, first *int, before *ent.Cursor, last *int, orderBy *ent.TodoOrder) (*ent.TodoConnection, error) {
return r.client.Todo.Query().
Paginate(ctx, after, first, before, last,
ent.WithTodoOrder(orderBy),
)
}

Use in GraphQL​

query {
todos(first: 3, orderBy: {direction: DESC, field: TEXT}) {
edges {
node {
text
}
}
}
}

Fields Collection​

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:

query {
users(first: 100) {
edges {
node {
photos {
link
}
posts {
content
comments {
content
}
}
}
}
}
}

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.

Schema configuration​

In order to configure this option to specific edges, use the entgql.Annotation as follows:

func (Todo) Edges()