Skip to content

Commit

Permalink
rename call_grpc_method to call on sdk && and improe caching
Browse files Browse the repository at this point in the history
  • Loading branch information
j03-dev committed Sep 12, 2024
1 parent 5111c73 commit 4136fe5
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 45 deletions.
16 changes: 8 additions & 8 deletions docs/metatype.dev/docs/concepts/features-overview/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ We're taking any and all kinds of feature requests, suggestions and recommendati
link: "/docs/reference/typegate",
body: (
<>
Expose your typegraph through a gRPC API, enabling high-performance,
bi-directional communication between services. This allows you to call
gRPC methods directly from the typegraph, providing more flexibility
and better integration with existing gRPC-based microservices.
This approach supports a broader range of real-time use cases .
Expose your gRPC API, enabling high-performance, bi-directional
communication between services. This allows you to call gRPC methods
directly from the typegraph, providing more flexibility and better
integration with existing gRPC-based microservices. This approach
supports a broader range of real-time use cases .
</>
),
},
Expand Down Expand Up @@ -314,7 +314,7 @@ We're taking any and all kinds of feature requests, suggestions and recommendati
Includes easy access to ESM and libraries through standard Deno features.
</>
),
},
},
],
[
<TGExample
Expand All @@ -333,7 +333,7 @@ We're taking any and all kinds of feature requests, suggestions and recommendati
Implement functions that execute python functions in code snippets or on disk modules.
</>
),
},
},
],
[
<CodeBlock
Expand Down Expand Up @@ -543,7 +543,7 @@ Access a myriad of databases directly from your typegraph. Run queries, mange it
Bundles the typegate within it making the CLI all one needs to get started. (And a text editor, of course.)
</>
),
},
},
],
[
<CodeBlock
Expand Down
6 changes: 4 additions & 2 deletions docs/metatype.dev/docs/reference/runtimes/grpc/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ The `proto_file` is the path to the `.proto` file that defines the gRPC service.

The `endpoint` is the address of the gRPC server that the Typegraph will communicate with. It uses the format `tcp://<host>:<port>`, and is specified to point to the correct server and port where the gRPC service is running.

### `call_grpc_method`
## Method

This method is used to call a specific method on the gRPC service. It accepts the full path to the gRPC method, usually in the form `/package_name.service_name/method_name`. The **package_name** refers to the package defined in the `.proto` file, and it must be included when calling the method. In the example below, `greet` will call the `SayHello` method of the `Greeter` service within the `helloworld` package, as defined in the `helloworld.proto` file.
### `call`

This method creates a typegraph function for gRPC method calls. It accepts the full path to the gRPC method, usually in the form `/package_name.service_name/method_name`. The **package_name** refers to the package defined in the `.proto` file, and it must be included when calling the method. In the example below, `greet` will call the `SayHello` method of the `Greeter` service within the `helloworld` package, as defined in the `helloworld.proto` file.

## Example

Expand Down
2 changes: 1 addition & 1 deletion examples/typegraphs/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def grpc(g: Graph):

g.expose(
Policy.public(),
greet=grpc_runtime.call_grpc_method("/helloworld.Greeter/SayHello"),
greet=grpc_runtime.call("/helloworld.Greeter/SayHello"),
)
14 changes: 7 additions & 7 deletions examples/typegraphs/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export const tg = await typegraph(

const proto_file = `${__dirname}proto/helloworld.proto`;
// highlight-next-line
const grpc_runtime = new GrpcRuntime(
proto_file,
endpoint,
);
const grpc_runtime = new GrpcRuntime(proto_file, endpoint);

g.expose({
greet: grpc_runtime.call_grpc_method("/helloworld.Greeter/SayHello"),
}, Policy.public());
g.expose(
{
greet: grpc_runtime.call("/helloworld.Greeter/SayHello"),
},
Policy.public(),
);
},
);
2 changes: 1 addition & 1 deletion src/typegate/engine/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type MetaNS = {

grpc: {
register: (inp: GrpcRegisterInput) => Promise<void>;
unregister: (client_id: string) => void;
unregister: (client_id: string) => Promise<void>;
callGrpcMethod: (inp: CallGrpcMethodInput) => Promise<string>;
};
};
Expand Down
16 changes: 10 additions & 6 deletions src/typegraph/core/src/runtimes/grpc/type_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ fn convert_proto_fields_to_type_id(

for field in fields {
let field_name = field.name().to_string();
let type_name = field.proto().type_();
let type_name = field.proto().type_name();

if let Some(cached_type_id) = cache.get(&field_name) {
let cache_key = format!("{}/{}", type_name, field_name);

// Check if the type is already cached
if let Some(cached_type_id) = cache.get(&cache_key) {
r#type.prop(&field_name, *cached_type_id);
continue;
}

let mut type_id = match type_name {
let mut type_id = match field.proto().type_() {
Type::TYPE_STRING => t::string().build()?,
Type::TYPE_INT32 | Type::TYPE_INT64 => t::integer().build()?,
Type::TYPE_FLOAT => t::float().build()?,
Type::TYPE_BOOL => t::boolean().build()?,
Type::TYPE_MESSAGE => {
let nested_message_type = field.proto().type_name().to_string();
let nested_message_type = field.proto().type_name();
let nested_fields =
get_message_field_descriptor(file_descriptor, &nested_message_type)?;
get_message_field_descriptor(file_descriptor, nested_message_type)?;
convert_proto_fields_to_type_id(file_descriptor, nested_fields, cache)?
}
_ => bail!(
Expand All @@ -80,7 +83,8 @@ fn convert_proto_fields_to_type_id(
type_id = t::optional(type_id).build()?;
}

cache.insert(field_name.clone(), type_id);
// Cache the type_id using the unique cache key
cache.insert(cache_key, type_id);

r#type.prop(&field_name, type_id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/deno/src/runtimes/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class GrpcRuntime extends Runtime {
super(id);
}

call_grpc_method(method: string) {
call(method: string) {
const funcData = runtimes.callGrpcMethod(this._id, { method: method });
return Func.fromTypeFunc(funcData);
}
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/python/typegraph/runtimes/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, proto_file: str, endpoint: str):

super().__init__(runtime_id.value)

def call_grpc_method(self, method: str):
def call(self, method: str):
data = GrpcData(method)
func_data = runtimes.call_grpc_method(store, self.id, data)

Expand Down
6 changes: 3 additions & 3 deletions tests/runtimes/grpc/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
def geography(g: Graph):
endpoint = "tcp://localhost:4770"

geography = BASE_DIR.joinpath("proto/geography.proto")
geography_grpc = GrpcRuntime(geography, endpoint)
proto_file = BASE_DIR.joinpath("proto/geography.proto")
grpc_runtime = GrpcRuntime(proto_file, endpoint)

g.expose(
Policy.public(),
dem=geography_grpc.call_grpc_method("/geography.Demography/Country"),
dem=grpc_runtime.call("/geography.Demography/Country"),
)
6 changes: 3 additions & 3 deletions tests/runtimes/grpc/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
def helloworld(g: Graph):
endpoint = "tcp://localhost:4770"

helloworld = BASE_DIR.joinpath("proto/helloworld.proto")
helloworld_grpc = GrpcRuntime(helloworld, endpoint)
proto_file = BASE_DIR.joinpath("proto/helloworld.proto")
grpc_runtime = GrpcRuntime(proto_file, endpoint)

g.expose(
Policy.public(),
greet=helloworld_grpc.call_grpc_method("/helloworld.Greeter/SayHello"),
greet=grpc_runtime.call("/helloworld.Greeter/SayHello"),
)
16 changes: 8 additions & 8 deletions tests/runtimes/grpc/helloworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const __dirname = new URL(".", import.meta.url).pathname;

export const tg = await typegraph("helloworld", (g) => {
const endpoint = "tcp://localhost:4770";
const helloworld = `${__dirname}proto/helloworld.proto`;
const proto_file = `${__dirname}proto/helloworld.proto`;

const helloworld_grpc = new GrpcRuntime(
helloworld,
endpoint,
);
const grpc_runtime = new GrpcRuntime(proto_file, endpoint);

g.expose({
greet: helloworld_grpc.call_grpc_method("/helloworld.Greeter/SayHello"),
}, Policy.public());
g.expose(
{
greet: grpc_runtime.call("/helloworld.Greeter/SayHello"),
},
Policy.public(),
);
});
8 changes: 4 additions & 4 deletions tests/runtimes/grpc/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
def maths(g: Graph):
endpoint = "tcp://localhost:4770"

maths = BASE_DIR.joinpath("proto/maths.proto")
maths_grpc = GrpcRuntime(maths, endpoint)
proto_file = BASE_DIR.joinpath("proto/maths.proto")
grpc_runtime = GrpcRuntime(proto_file, endpoint)

g.expose(
Policy.public(),
sum=maths_grpc.call_grpc_method("/maths.Calculator/Sum"),
prime=maths_grpc.call_grpc_method("/maths.Calculator/IsPrime"),
sum=grpc_runtime.call("/maths.Calculator/Sum"),
prime=grpc_runtime.call("/maths.Calculator/IsPrime"),
)

0 comments on commit 4136fe5

Please sign in to comment.