Project Setup

Table of contents

No heading

No headings in the article.

This article is part of a tutorial series on building GraphQL API with Go and PostgresQL.

Since this is not a tutorial for a total beginner, I'll assume you have the necessary tools such as Go installed or you know how to get them installed.

The setup or architecture of this project may change since I'm also learning as I code and write this series. At the point of writing this article, I've already gone beyond the project set up but still I'll bring you up to speed pretty quick.

Screenshot from 2022-04-19 16-45-37.png

First step - initializing a new Go project. Create a folder (mine's named TechHub-GoGraph), open your terminal, cd into it and do.

go mod init github.com/[github_username]/[project_name]

Replace github_username and project_name with the right parameters. e.g github.com/DavidHODs/TechHUB-goGraph.

This project uses gqlgen, to build the GraphQL server. To install, while still in your root directory

go get github.com/99designs/gqlgen
printf '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > tools.go
go mod tidy

To initialize gqlgen config

go run github.com/99designs/gqlgen init

gqlgen ships with a Todo app graphql schema definition, so on running the init command, models and resolvers are generated for the Todo app.

NB - the following images are screen shots from gqlgen's tutorial site. The project structure should like this Screenshot from 2022-04-19 19-23-24.png

Todo schema definition Screenshot from 2022-04-19 19-23-59.png

To confirm everything has been set up correctly, run

go run server.go

In the GraphQL playground, you can implement the following query as a test Screenshot from 2022-04-19 19-25-19.png GraphQL should return an error for those queries because the body of the generated resolver functions have not been implemented Screenshot from 2022-04-19 19-24-23.png

Restructuring the Project

Since we don't know how big the project might become, to keep everything neat, in the folder graph where we have schema.graphqls create three files - posts.graphqls, users.graphqls and querymutation.graphqls.

Finally, delete schema.graphqls, rename schema.resolvers.go to querymutation.graphqls. Clear off the content of the renamed querymutation.graphqls leaving only line 1 (package graph).