Thursday, June 24, 2021

Gqlgen For My Backends

On Monday night after I spent the day building a REST API in Go I decided to spin up something small with gqlgen and try and connect that to the ORM I'd been using that day in GORM.

I followed this tutorial to get an app idea and here is the code as it stands. I do need to add some database methods that replicate Active Record like save. I'll do that some time later this week.

It's honestly really quite easy to spin something up with the code generation that's provided. It could be from this point onwards my go to tool for making backends that talk to a client and a DB.

Some of the things that I did notice were:

  1. You need to maintain both your GraphQL schema alongside models that correlate to whatever is in your DB. This simply can't be a one to one (as far as I know) and transforming data between GraphQL land and database is something that I think is one of the slightly annoying things you need to do when working in this space
  2. Inputs are strongly typed, validation is done via the GraphQL schema and not via models
  3. Go structs and GraphQL types so clearly emulate each other, probably one of the reasons why GraphQL and Go is such a popular stack