Using a custom codec: graphql

117 views
Skip to first unread message

Francis Chuang

unread,
Jan 9, 2017, 9:11:49 PM1/9/17
to grpc.io
I am looking into using grpc for communication between microservices using the grpc-go library, because of the reasons outlined here: http://www.grpc.io/docs/guides/concepts.html

However, rather than using protobuf as the codec, I want to use graphql as callers can define the shape of the response and only fetch the things they need.

I have thought of 2 options:
1. Use protobufs to wrap graphql/json. Requests will contain the graphql query, variables and the queryname. Responses will just contain a result field containing serialized JSON. This method allows me to use existing tooling and code generation, but the double encoding bothers me.

2. Create a custom codec (https://godoc.org/google.golang.org/grpc#Codec) to decode and encode requests and responses in JSON. This seems like the best and cleanest way, but no code generation.

What are your thoughts?

Cheers,
Francis

Carl Mastrangelo

unread,
Jan 10, 2017, 4:40:13 PM1/10/17
to grpc.io
2 sounds better to me, and will be faster most likely.  

Francis Chuang

unread,
Jan 10, 2017, 4:50:58 PM1/10/17
to grpc.io
Yea, that currently seems to be the best way, and creating a JSON codec is pretty simple too.

However, my concern is that by doing so, there is no more code generation. I've only followed the tutorials, but it seems the grpc code generation generates a lot of things such as interfaces for implementing the grpc server, types and all the stuff to work with streams which is a huge productivity boon.

Another problem is that Server.RegisterService() takes an interface{}: https://godoc.org/google.golang.org/grpc#Server.RegisterService without the code generation, I am not sure what ss should look like.

Carl Mastrangelo

unread,
Jan 10, 2017, 4:54:13 PM1/10/17
to grpc.io
Personally I would generate the code once and then just modify it to work with my custom codec.   Having the autogenerated code is nice, but since you don't need most of it, I think you'll find you pay the costs without any of the benefits.

Francis Chuang

unread,
Jan 10, 2017, 4:59:22 PM1/10/17
to grpc.io
Good point! :)

I'll give that a go.
Reply all
Reply to author
Forward
0 new messages