How to Build an API with Echo Golang and GraphQL using gqlgen (Part I)

Jesús Zubieta Segundo
4 min readMay 11, 2021
Image by : https://github.com/graph-gophers/

This article is made to serve as a reference for building APIs using Golang’s Echo framework and gqlen as GraphQL library trying to use the best architecture and software development practices.

Prerequisites

Step 1: Build your API template

  • For the development of the APIwe will take as a basis this project that contains the basic implementation of a REST API with the use of a layered architecture, injection of dependencies, unit tests,go mods
Basic api structure

Step 2: Initialise your Go project as gqlgen project

For the next step we need to go to the directory in which our API is located and run the next commands to initialize it as a gqlgen project which will generate the serve code

go get github.com/99designs/gqlgen
go run github.com/99designs/gqlgen init --verbose
command output

You will see that the files containing the graphQL functionality have been added to the project

api structure with gqlgen auto gen files

As you can see, gqlgen generates an implementation of a server to be able to run the API at server.go. This causes conflicts within the api because we have a main method already implemented, up to this point it would not be possible to run our API due to these conflicts

server code autogen by gqlgen

Then our next step would be to couple the gqlgen-generated auto server implementation with the one we have with Echo, let’s start by preserving a single line of code from the server.go file and then deleting it

srv:=handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

now we will modify the container.go file, adding a method that injects the graphql schema to the server as below :

Step 3 Start Our Implementation :D

Now we generate the server code for our schema, for practical purposes we will reuse the schema that gqlgen autogenerated, which is found in /graph/schema.graphqlwhich already contains the definition of querys and mutations. Now run the gqlgen generate command to generate Go models, resolvers and a dummy server implementation for our schema.

go run github.com/99designs/gqlgen generate --verbose

Gqlgen allows us to control and customize the code that is going to be generated. From changing the location of the schema, bind our existing models with those defined in the schema to avoid their auto-generation, enable Apollo Federation and a host of other things.

Server implementation from Gqlgen

Now the next step.Following the principles of layered architecture, we will continue with the creation of some more files; this to begin to segregate responsibilities.We will add our repository layer in which the database request, APIs Rest, GRPC or any other data source, will be added. In this case it will be a mock. We will also add our service layer which will be in charge of orchestrating the business logic and will be responsible for calling and managing our data layer. So we add an services folder to include ITodoService.go ,todoService.go files , and ITodoRepository.go todoRepository.go at repositories folder.

ITodoRepository.go
todoRepository.go
ITodoService.go
TodoService.go

Now we are going to edit the file reolver.goto add the DI

And then we go to our schema.resolvers.gofile to modify it and add our implementation of the resolver

we add our new methods for DI to the file container.go and we are going to eliminate the method to use the DI of the resolver

Now as the last step we are going to register the graphQL route in our server generating the executable schema from the resolver generated by the DI

routes.go

It is necessary to wrap the handlerQL as an echo Handler func, so that it recognizes it as a valid handler function

Step 4 Let´s Try !!

So now we can run our API by executing the command go run main.go

Let´s do a request using GraphiQL client and we can see the schema at the Docs explorer

Next Steps

How to get echo context to use with gqlgen, integration of Grpc as data source …. etc etc :D

--

--