Generating a gRPC Service

Generating Protobuf structs generated from our ent.Schema can be useful, but what we’re really interested in is getting an actual server that can create, read, update, and delete entities from an actual database. To do that, we need to update just one line of code! When we annotate a schema with entproto.Service, we tell the entproto code-gen that we are interested in generating a gRPC service definition, from the protoc-gen-entgrpc will read our definition and generate a service implementation. Edit ent/schema/user.go and modify the schema’s Annotations:

  1. func (User) Annotations() []schema.Annotation {
  2. return []schema.Annotation{
  3. entproto.Message(),
  4. entproto.Service(), // <-- add this
  5. }
  6. }

Now re-run code-generation:

  1. go generate ./...

Observe some interesting changes in ent/proto/entpb:

  1. ent/proto/entpb
  2. ├── entpb.pb.go
  3. ├── entpb.proto
  4. ├── entpb_grpc.pb.go
  5. ├── entpb_user_service.go
  6. └── generate.go

First, entproto added a service definition to entpb.proto:

  1. service UserService {
  2. rpc Create ( CreateUserRequest ) returns ( User );
  3. rpc Get ( GetUserRequest ) returns ( User );
  4. rpc Update ( UpdateUserRequest ) returns ( User );
  5. rpc Delete ( DeleteUserRequest ) returns ( google.protobuf.Empty );
  6. }

In addition, two new files were created. The first, ent_grpc.pb.go, contains the gRPC client stub and the interface definition. If you open the file, you will find in it (among many other things):

  1. // UserServiceClient is the client API for UserService service.
  2. //
  3. // For semantics around ctx use and closing/ending streaming RPCs, please
  4. // refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
  5. type UserServiceClient interface {
  6. Create(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*User, error)
  7. Get(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*User, error)
  8. Update(ctx context.Context, in *UpdateUserRequest, opts ...grpc.CallOption) (*User, error)
  9. Delete(ctx context.Context, in *DeleteUserRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
  10. }

The second file, entpub_user_service.go contains a generated implementation for this interface. For example, an implementation for the Get method:

  1. // Get implements UserServiceServer.Get
  2. func (svc *UserService) Get(ctx context.Context, req *GetUserRequest) (*User, error) {
  3. var (
  4. err error
  5. get *ent.User
  6. )
  7. switch req.GetView() {
  8. case GetUserRequest_VIEW_UNSPECIFIED, GetUserRequest_BASIC:
  9. get, err = svc.client.User.Get(ctx, int(req.GetId()))
  10. case GetUserRequest_WITH_EDGE_IDS:
  11. get, err = svc.client.User.Query().
  12. Where(user.ID(int(req.GetId()))).
  13. Only(ctx)
  14. default:
  15. return nil, status.Errorf(codes.InvalidArgument, "invalid argument: unknown view")
  16. }
  17. switch {
  18. case err == nil:
  19. return toProtoUser(get), nil
  20. case ent.IsNotFound(err):
  21. return nil, status.Errorf(codes.NotFound, "not found: %s", err)
  22. default:
  23. return nil, status.Errorf(codes.Internal, "internal error: %s", err)
  24. }
  25. }

Not bad! Next, let’s create a gRPC server that can serve requests to our service.