Golang. CRUD with gRPC (plus GraphQL transcoding).

Yuri Fenyuk
4 min readJun 27, 2023

The current chapter is the continuation of Golang related series started with the previous ones:

The input for the current chapter is here. The project has gRPC framework to support CRUD operations for simple Brand entity. The protobuf message from the same scheme are used to persist Brand repository into file system. Currently, the project exposes Brands via gRPC server and traditional REST API via gRPC-Gateway plugin to gRPC. The current chapter plan is to do a similar trick and expose GraphQL endpoint for Brand entity.

grpc-graphql-gateway is a protoc plugin that generates GraphQL execution code from protobuf schema. This approach is not only convenient to keep protobuf schema as a single source of truth for API definition, but API implementation also exists in one version. Please read through readme section of plugin (yes, the approach is really similar to gRPC-Gateway plugin which has been used to support REST API access to gRPC server in one of my previous chapter).

Diagram from the official documentation

grpc-graphql-gateway generates a proxy GraphQL server which can be run side-by-side with the original gRPC server and reverse proxy all GraphQL queries or mutations to it.

Without further ado, let’s follow the installation section to get binary locally:

and also copy plugin’s main Protobuf schema file graphql.proto locally as it will be imported in this project’s schema.

Following amendments need to be done to brand.proto (please notice how these changes mimic what has been done for REST API):

#2: import plugin’s Protobuf definitions.

#5..#8: state that gRPC service needs GraphQL reverse proxy, which will be talking to gRPC server on port 6001 (indeed, this schema harcoding is not perfect, but this is plugin limitation) and be insecure.

#15..#18: generate GraphQL reverse-proxy operation for Create mutation with the same name.
#24..#27, #33..#36, #43..#46,#52..#57: generate GraphQL reverse-proxy operations for other CRUD parts.

As with other plugins so far, protoc command should have a new line for the new plugin:

#4: requires generating GraphQL reverse-proxy code to route calls to main gRPC server.

Generated Go-wrappers

New file has the name crud_brand.graphql.go and contains GraphQL related Go code. It has everything to run GraphQL server and it is coded in main.go:

New code in main.go

#3: importing plugin static part.

#6..#14: following plugin’s documentation to instantiate GraphQL server on port 6002 (set in config.json).

Let’s run the updated project:

go run

Main gRPC server is run on port 6001, GraphQL server is run on port 6002 (it proxies all queries and mutations to gRPC server on port 6001). Another proxy REST API server (created in previous chapters) is run on port 6000 (again, it proxies all queries and mutations to gRPC server on port 6001).

And testing different CRUD operations by calling different protocols and servers proves that everything works in sync, having gRPC server as the central and only data repository:

gRPC GetList shows 4 brands
GraphQL GetList shows same 4 brands
GraphQL Create stires new brand and assign ID = 5 to it
GetOne with ID = 5 returns just created brand
gRPC GetList shows now 5 brands

The final version of the project is here.

grpc-graphql-gateway is one of plugin to extend core functionality on gRPC. It helps in the situation when different protocols need to be still supported and the developer does not want to maintain too much duplication code and considers proto schema file as a single source of truth.