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:

ent/schema/user.go

  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:

ent/proto/entpb/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. rpc List ( ListUserRequest ) returns ( ListUserResponse );
  7. rpc BatchCreate ( BatchCreateUsersRequest ) returns ( BatchCreateUsersResponse );
  8. }

In addition, two new files were created. The first, entpb_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):

ent/proto/entpb/entpb_grpc.pb.go

  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. List(ctx context.Context, in *ListUserRequest, opts ...grpc.CallOption) (*ListUserResponse, error)
  11. BatchCreate(ctx context.Context, in *BatchCreateUsersRequest, opts ...grpc.CallOption) (*BatchCreateUsersResponse, error)
  12. }

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

ent/proto/entpb/entpb_user_service.go

  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. id := int(req.GetId())
  8. switch req.GetView() {
  9. case GetUserRequest_VIEW_UNSPECIFIED, GetUserRequest_BASIC:
  10. get, err = svc.client.User.Get(ctx, id)
  11. case GetUserRequest_WITH_EDGE_IDS:
  12. get, err = svc.client.User.Query().
  13. Where(user.ID(id)).
  14. Only(ctx)
  15. default:
  16. return nil, status.Error(codes.InvalidArgument, "invalid argument: unknown view")
  17. }
  18. switch {
  19. case err == nil:
  20. return toProtoUser(get)
  21. case ent.IsNotFound(err):
  22. return nil, status.Errorf(codes.NotFound, "not found: %s", err)
  23. default:
  24. return nil, status.Errorf(codes.Internal, "internal error: %s", err)
  25. }
  26. }

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