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:
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:
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:
Now, we're ready to run gqlgen code generation. Execute this command from the root of the project:
The command above will execute the gqlgen code-generator, and if that finished successfully, your project directory should look like this:
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
2. Create a new Go file named
ent/entc.go, and paste the following content:
3. Edit the
ent/generate.go file to execute the
ent/entc.go is ignored using a build tag, and it's executed by the go generate command through the
4. In order to execute
go generate, we create a new
generate.go file (in the root
of the project) with the following:
go generate ./... from the root of the project, triggers both Ent and gqlgen code generation.
gqlgen allows changing the generated
Resolver and add additional dependencies to it. Let's add
ent.Client as a dependency by pasting the following in
We create a new directory
cmd/todo and a
main.go file with the following code to create the GraphQL server:
Run the server using the command below, and open localhost:8081:
You should see the interactive playground:
If you're having troubles with getting the playground to run, go to first section and clone the example repository.
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:
Then, running this GraphQL query should return an empty todo list:
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:
Now, creating a todo item should work:
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.