-
Notifications
You must be signed in to change notification settings - Fork 161
feat(grpc_datasource): introduce RPCTransport abstraction with gRPC implementation #1490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Noroth
merged 3 commits into
wundergraph:master
from
fengyuwusong:feat/grpc-datasource-transport-abstraction
May 21, 2026
+123
−34
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "google.golang.org/grpc" | ||
| protoref "google.golang.org/protobuf/reflect/protoreflect" | ||
| ) | ||
|
|
||
| // RPCTransport abstracts the transport protocol for RPC calls. | ||
| // Both gRPC and Connect protocol implement this interface. | ||
| type RPCTransport interface { | ||
| Invoke(ctx context.Context, methodFullName string, input, output protoref.Message) error | ||
| } | ||
|
|
||
| // grpcTransport wraps grpc.ClientConnInterface to implement RPCTransport. | ||
| type grpcTransport struct { | ||
| cc grpc.ClientConnInterface | ||
| } | ||
|
|
||
| // NewGRPCTransport creates an RPCTransport that delegates to a gRPC ClientConnInterface. | ||
| func NewGRPCTransport(cc grpc.ClientConnInterface) RPCTransport { | ||
| return &grpcTransport{cc: cc} | ||
| } | ||
|
|
||
| func (t *grpcTransport) Invoke(ctx context.Context, method string, input, output protoref.Message) error { | ||
| if t.cc == nil { | ||
| return errors.New("grpc transport: nil client connection") | ||
| } | ||
| // grpc.ClientConnInterface.Invoke accepts (ctx, method, args any, reply any, opts ...grpc.CallOption). | ||
| // protoref.Message satisfies the any constraint; variadic opts can be omitted. | ||
| // This wrapper intentionally does not forward grpc.CallOption, as RPCTransport | ||
| // is protocol-agnostic. The existing grpc_datasource code does not use any CallOption at the Invoke site. | ||
| return t.cc.Invoke(ctx, method, input, output) | ||
| } |
54 changes: 54 additions & 0 deletions
54
v2/pkg/engine/datasource/grpc_datasource/transport_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| protoref "google.golang.org/protobuf/reflect/protoreflect" | ||
| "google.golang.org/protobuf/types/dynamicpb" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/grpctest" | ||
| ) | ||
|
|
||
| // newTestCompiler builds an RPCCompiler bound to the grpctest fixture. | ||
| // It is shared by every transport-level test in this package. | ||
| func newTestCompiler(t *testing.T) *RPCCompiler { | ||
| t.Helper() | ||
| compiler, err := NewProtoCompiler(grpctest.MustProtoSchema(t), testMapping()) | ||
| require.NoError(t, err) | ||
| return compiler | ||
| } | ||
|
|
||
| // findMessageDesc resolves a fully-qualified message name from the compiled | ||
| // proto document. Used by tests to construct dynamicpb.Message instances | ||
| // for transport.Invoke calls without depending on the generated Go types. | ||
| func findMessageDesc(t *testing.T, compiler *RPCCompiler, fullName string) protoref.MessageDescriptor { | ||
| t.Helper() | ||
| for _, m := range compiler.doc.Messages { | ||
| if string(m.Desc.FullName()) == fullName { | ||
| return m.Desc | ||
| } | ||
| } | ||
| t.Fatalf("message %q not found in proto document", fullName) | ||
| return nil | ||
| } | ||
|
|
||
| // TestGRPCTransport_Invoke is a smoke test for the gRPC RPCTransport | ||
| // implementation; it goes through the data source's mockInterface (defined | ||
| // in grpc_datasource_test.go) so the assertion is just that Invoke returns | ||
| // no error for a well-formed request. | ||
| func TestGRPCTransport_Invoke(t *testing.T) { | ||
| mi := mockInterface{} | ||
| transport := NewGRPCTransport(mi) | ||
|
|
||
| compiler := newTestCompiler(t) | ||
| reqDesc := findMessageDesc(t, compiler, "productv1.QueryComplexFilterTypeRequest") | ||
| respDesc := findMessageDesc(t, compiler, "productv1.QueryComplexFilterTypeResponse") | ||
|
|
||
| inputMsg := dynamicpb.NewMessage(reqDesc) | ||
| outputMsg := dynamicpb.NewMessage(respDesc) | ||
|
|
||
| err := transport.Invoke(context.Background(), "/productv1.ProductService/QueryComplexFilterType", inputMsg, outputMsg) | ||
| require.NoError(t, err) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.