Introduction

In this section, we will learn how to connect Ent to GraphQL. If you’re not familiar with GraphQL, it’s recommended to go over its introduction guide before going over this tutorial.

Clone the code (optional)

The code for this tutorial is available under github.com/a8m/ent-graphql-example, 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 checkout v0.1.0 as follows:

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

Basic Skeleton

gqlgen is a framework for easily generating GraphQL servers in Go. In this tutorial, we will review Ent’s official integration with it.

This tutorial begins where the previous one ended (with a working Todo-list schema). We start by creating a simple GraphQL schema for our todo list, then install the 99designs/gqlgen package and configure it. Let’s create a file named todo.graphql and paste the following:

  1. # Maps a Time GraphQL scalar to a Go time.Time struct.
  2. scalar Time
  3. # Define an enumeration type and map it later to Ent enum (Go type).
  4. # https://graphql.org/learn/schema/#enumeration-types
  5. enum Status {
  6. IN_PROGRESS
  7. COMPLETED
  8. }
  9. # Define an object type and map it later to the generated Ent model.
  10. # https://graphql.org/learn/schema/#object-types-and-fields
  11. type Todo {
  12. id: ID!
  13. createdAt: Time
  14. status: Status!
  15. priority: Int!
  16. text: String!
  17. parent: Todo
  18. children: [Todo!]
  19. }
  20. # Define an input type for the mutation below.
  21. # https://graphql.org/learn/schema/#input-types
  22. input TodoInput {
  23. status: Status! = IN_PROGRESS
  24. priority: Int
  25. text: String!
  26. parent: ID
  27. }
  28. # Define a mutation for creating todos.
  29. # https://graphql.org/learn/queries/#mutations
  30. type Mutation {
  31. createTodo(todo: TodoInput!): Todo!
  32. }
  33. # Define a query for getting all todos.
  34. type Query {
  35. todos: [Todo!]
  36. }

Install 99designs/gqlgen:

  1. go get github.com/99designs/gqlgen

The gqlgen package can be configured using a gqlgen.yml file that it automatically loads from the current directory. Let’s add this file. Follow the comments in this file to understand what each config directive means:

  1. # schema tells gqlgen where the GraphQL schema is located.
  2. schema:
  3. - todo.graphql
  4. # resolver reports where the resolver implementations go.
  5. resolver:
  6. layout: follow-schema
  7. dir: .
  8. # gqlgen will search for any type names in the schema in these go packages
  9. # if they match it will use them, otherwise it will generate them.
  10. # autobind tells gqlgen to search for any type names in the GraphQL schema in the
  11. # provided Go package. If they match it will use them, otherwise it will generate new ones.
  12. autobind:
  13. - todo/ent
  14. # This section declares type mapping between the GraphQL and Go type systems.
  15. models:
  16. # Defines the ID field as Go 'int'.
  17. ID:
  18. model:
  19. - github.com/99designs/gqlgen/graphql.IntID
  20. # Map the Status type that was defined in the schema
  21. Status:
  22. model:
  23. - todo/ent/todo.Status

Now, we’re ready to run gqlgen code generation. Execute this command from the root of the project:

  1. go run github.com/99designs/gqlgen

The command above will execute the gqlgen code-generator, and if that finished successfully, your project directory should look like this:

  1. tree -L 1
  2. .
  3. ├── ent
  4. ├── example_test.go
  5. ├── generated.go
  6. ├── go.mod
  7. ├── go.sum
  8. ├── gqlgen.yml
  9. ├── models_gen.go
  10. ├── resolver.go
  11. ├── todo.graphql
  12. └── todo.resolvers.go
  13. 1 directories, 9 files

Connect Ent to GQL

After the gqlgen assets were generated, we’re ready to connect Ent to gqlgen and start running our server. This section contains 5 steps, follow them carefully :).

1. Install the GraphQL extension for Ent

  1. go get entgo.io/contrib/entgql

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

  1. // +build ignore
  2. package main
  3. import (
  4. "log"
  5. "entgo.io/ent/entc"
  6. "entgo.io/ent/entc/gen"
  7. "entgo.io/contrib/entgql"
  8. )
  9. func main() {
  10. ex, err := entgql.NewExtension()
  11. if err != nil {
  12. log.Fatalf("creating entgql extension: %v", err)
  13. }
  14. opts := []entc.Option{
  15. entc.Extensions(ex),
  16. }
  17. if err := entc.Generate("./schema", &gen.Config{}, opts...); err != nil {
  18. log.Fatalf("running ent codegen: %v", err)
  19. }
  20. }

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

  1. package ent
  2. //go:generate go run 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.

4. In order to execute gqlgen through go generate, we create a new generate.go file (in the root of the project) with the following:

  1. package todo
  2. //go:generate go run github.com/99designs/gqlgen

Now, running go generate ./... from the root of the project, triggers both Ent and gqlgen code generation.

  1. go generate ./...

5. gqlgen allows changing the generated Resolver and add additional dependencies to it. Let’s add the ent.Client as a dependency by pasting the following in resolver.go:

  1. package todo
  2. import (
  3. "todo/ent"
  4. "github.com/99designs/gqlgen/graphql"
  5. )
  6. // Resolver is the resolver root.
  7. type Resolver struct{ client *ent.Client }
  8. // NewSchema creates a graphql executable schema.
  9. func NewSchema(client *ent.Client) graphql.ExecutableSchema {
  10. return NewExecutableSchema(Config{
  11. Resolvers: &Resolver{client},
  12. })
  13. }

Run the server

We create a new directory cmd/todo and a main.go file with the following code to create the GraphQL server:

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "net/http"
  6. "todo"
  7. "todo/ent"
  8. "todo/ent/migrate"
  9. "entgo.io/ent/dialect"
  10. "github.com/99designs/gqlgen/graphql/handler"
  11. "github.com/99designs/gqlgen/graphql/playground"
  12. _ "github.com/mattn/go-sqlite3"
  13. )
  14. func main() {
  15. // Create ent.Client and run the schema migration.
  16. client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
  17. if err != nil {
  18. log.Fatal("opening ent client", err)
  19. }
  20. if err := client.Schema.Create(
  21. context.Background(),
  22. migrate.WithGlobalUniqueID(true),
  23. ); err != nil {
  24. log.Fatal("opening ent client", err)
  25. }
  26. // Configure the server and start listening on :8081.
  27. srv := handler.NewDefaultServer(todo.NewSchema(client))
  28. http.Handle("/",
  29. playground.Handler("Todo", "/query"),
  30. )
  31. http.Handle("/query", srv)
  32. log.Println("listening on :8081")
  33. if err := http.ListenAndServe(":8081", nil); err != nil {
  34. log.Fatal("http server terminated", err)
  35. }
  36. }

Run the server using the command below, and open localhost:8081:

  1. go run ./cmd/todo

You should see the interactive playground:

tutorial-todo-playground

If you’re having troubles with getting the playground to run, go to first section and clone the example repository.

Query Todos

If we try to query our todo list, we’ll get an error as the resolver method is not yet implemented. Let’s implement the resolver by replacing the Todos implementation in the query resolver:

  1. func (r *queryResolver) Todos(ctx context.Context) ([]*ent.Todo, error) {
  2. - panic(fmt.Errorf("not implemented"))
  3. + return r.client.Todo.Query().All(ctx)
  4. }

Then, running this GraphQL query should return an empty todo list:

  1. query AllTodos {
  2. todos {
  3. id
  4. }
  5. }
  6. # Output: { "data": { "todos": [] } }

Create a Todo

Same as before, if we try to create a todo item in GraphQL, we’ll get an error as the resolver is not yet implemented. Let’s implement the resolver by changing the CreateTodo implementation in the mutation resolver:

  1. func (r *mutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (*ent.Todo, error) {
  2. return r.client.Todo.Create().
  3. SetText(todo.Text).
  4. SetStatus(todo.Status).
  5. SetNillablePriority(todo.Priority). // Set the "priority" field if provided.
  6. SetNillableParentID(todo.Parent). // Set the "parent_id" field if provided.
  7. Save(ctx)
  8. }

Now, creating a todo item should work:

  1. mutation CreateTodo($todo: TodoInput!) {
  2. createTodo(todo: $todo) {
  3. id
  4. text
  5. createdAt
  6. priority
  7. parent {
  8. id
  9. }
  10. }
  11. }
  12. # Query Variables: { "todo": { "text": "Create GraphQL Example", "status": "IN_PROGRESS", "priority": 1 } }
  13. # Output: { "data": { "createTodo": { "id": "2", "text": "Create GraphQL Example", "createdAt": "2021-03-10T15:02:18+02:00", "priority": 1, "parent": null } } }

If you’re having troubles with getting this example to work, go to first section and clone the example repository.


Please continue to the next section where we explain how to implement the Relay Node Interface and learn how Ent automatically supports this.