Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions doc/md/tutorial-grpc-service-generation-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
id: grpc-service-generation-options
title: Configuring Service Method Generation
sidebar_label: Service Generation Options
---
By default, entproto will generate CRUD service methods for an `ent.Schema` annotated with `ent.Service()`. Method generation can be customized by including the argument `entproto.Methods()` in the `entproto.Service()` annotation. `entproto.Methods()` accepts bit flags to determine what service methods should be generated. The flags include:
```go
// Generates a Create gRPC service method for the entproto.Service.
entproto.MethodCreate

// Generates a Get gRPC service method for the entproto.Service.
entproto.MethodGet

// Generates an Update gRPC service method for the entproto.Service.
entproto.MethodUpdate

// Generates a Delete gRPC service method for the entproto.Service.
entproto.MethodDelete

// Generates all service methods for the entproto.Service.
// This is the same behavior as not including entproto.Methods.
entproto.MethodAll
```
To generate a service with multiple methods, bitwise OR the flags.


To see this in action, we can modify our ent schema. Let's say we wanted to prevent our gRPC client from mutating entries. We can accomplish this by modifying `ent/schema/user.go`:
```go {5-8}
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
entproto.Message(),
entproto.Service(
entproto.Methods(
entproto.MethodCreate |
entproto.MethodGet
),
),
}
}
```

Re-running `go generate ./...` will give us the following service definition in `entpb.proto`:
```protobuf
service UserService {
rpc Create ( CreateUserRequest ) returns ( User );

rpc Get ( GetUserRequest ) returns ( User );
}
```

Notice that the service no longer includes `Update` and `Delete` methods. Perfect!
1 change: 1 addition & 0 deletions doc/website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = {
'grpc-server-and-client',
'grpc-edges',
'grpc-optional-fields',
'grpc-service-generation-options',
]
}
]
Expand Down