-
Notifications
You must be signed in to change notification settings - Fork 162
feat(grpc_datasource): add ConnectRPC transport implementation #1509
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
Open
fengyuwusong
wants to merge
5
commits into
wundergraph:master
Choose a base branch
from
fengyuwusong:feat/connectrpc-datasource-impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f17973
chore(grpctest): generate Connect handlers via buf
fengyuwusong 3cbbc6e
feat(grpc_datasource): add ConnectRPC transport via connect-go client
fengyuwusong 3b92945
test(grpc_datasource): exercise data source over Connect transport
fengyuwusong b2bf327
test(grpc_datasource): guard against empty result slice in Connect e2e
fengyuwusong 0723be5
Merge branch 'master' into feat/connectrpc-datasource-impl
fengyuwusong 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
183 changes: 183 additions & 0 deletions
183
v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_connect_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,183 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/net/http2" | ||
| "golang.org/x/net/http2/h2c" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astparser" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/grpctest" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/grpctest/productv1/productv1connect" | ||
| ) | ||
|
|
||
| // setupTestConnectServer starts an httptest server backed by the | ||
| // MockServiceConnect adapter (the gRPC MockService wrapped onto the | ||
| // ConnectRPC handler interface). The server speaks Connect, gRPC, and | ||
| // gRPC-Web on the same H2C endpoint, but for these tests we drive it via | ||
| // the Connect transport. | ||
| // | ||
| // Returns a base URL that can be passed to NewConnectTransport. | ||
| func setupTestConnectServer(t testing.TB) (baseURL string, cleanup func()) { | ||
| t.Helper() | ||
|
|
||
| mock := &grpctest.MockService{} | ||
| connectImpl := grpctest.NewMockServiceConnect(mock) | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle(productv1connect.NewProductServiceHandler(connectImpl)) | ||
|
|
||
| srv := httptest.NewUnstartedServer(h2c.NewHandler(mux, &http2.Server{})) | ||
| srv.EnableHTTP2 = true | ||
| srv.Start() | ||
|
|
||
| cleanup = srv.Close | ||
| return srv.URL, cleanup | ||
| } | ||
|
|
||
| // Test_DataSource_Load_WithMockServiceConnect mirrors the gRPC end-to-end | ||
| // happy path (Test_DataSource_Load_WithMockService) but routes the call | ||
| // through the Connect transport instead of the gRPC client connection. | ||
| // It proves that the data source pipeline (compiler -> JSON builder -> | ||
| // transport -> response unmarshal) works for the Connect protocol against | ||
| // the same MockService implementation. | ||
| func Test_DataSource_Load_WithMockServiceConnect(t *testing.T) { | ||
| baseURL, cleanup := setupTestConnectServer(t) | ||
| t.Cleanup(cleanup) | ||
|
|
||
| query := `query ComplexFilterTypeQuery($filter: ComplexFilterTypeInput!) { complexFilterType(filter: $filter) { id name } }` | ||
| variables := `{"variables":{"filter":{"filter":{"name":"Test Product","filterField1":"filterField1","filterField2":"filterField2"}}}}` | ||
|
|
||
| schemaDoc := grpctest.MustGraphQLSchema(t) | ||
| queryDoc, report := astparser.ParseGraphqlDocumentString(query) | ||
| if report.HasErrors() { | ||
| t.Fatalf("failed to parse query: %s", report.Error()) | ||
| } | ||
|
|
||
| compiler, err := NewProtoCompiler(grpctest.MustProtoSchema(t), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| transport := NewConnectTransport(ConnectTransportConfig{ | ||
| BaseURL: baseURL, | ||
| Encoding: ConnectEncodingProtobuf, | ||
| }) | ||
|
|
||
| ds, err := NewDataSource(transport, DataSourceConfig{ | ||
| Operation: &queryDoc, | ||
| Definition: &schemaDoc, | ||
| SubgraphName: "Products", | ||
| Compiler: compiler, | ||
| Mapping: &GRPCMapping{ | ||
| Service: "Products", | ||
| QueryRPCs: RPCConfigMap[RPCConfig]{ | ||
| "complexFilterType": { | ||
| RPC: "QueryComplexFilterType", | ||
| Request: "QueryComplexFilterTypeRequest", | ||
| Response: "QueryComplexFilterTypeResponse", | ||
| }, | ||
| }, | ||
| Fields: map[string]FieldMap{ | ||
| "Query": { | ||
| "complexFilterType": { | ||
| TargetName: "complex_filter_type", | ||
| ArgumentMappings: map[string]string{ | ||
| "filter": "filter", | ||
| }, | ||
| }, | ||
| }, | ||
| "FilterType": { | ||
| "name": {TargetName: "name"}, | ||
| "filterField1": {TargetName: "filter_field_1"}, | ||
| "filterField2": {TargetName: "filter_field_2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| output, err := ds.Load(context.Background(), nil, []byte(`{"query":"`+query+`","body":`+variables+`}`)) | ||
| require.NoError(t, err) | ||
|
|
||
| type response struct { | ||
| Data struct { | ||
| ComplexFilterType []struct { | ||
| Id string `json:"id"` | ||
| Name string `json:"name"` | ||
| } `json:"complexFilterType"` | ||
| } `json:"data"` | ||
| } | ||
| var resp response | ||
| require.NoError(t, json.Unmarshal(output, &resp)) | ||
| require.NotEmpty(t, resp.Data.ComplexFilterType, "response should contain at least one item; empty slice would otherwise panic on index below") | ||
| require.Equal(t, "test-id-123", resp.Data.ComplexFilterType[0].Id) | ||
| require.Equal(t, "Test Product", resp.Data.ComplexFilterType[0].Name) | ||
| } | ||
|
|
||
| // Test_DataSource_Load_WithMockServiceConnect_JSON re-runs the same | ||
| // happy-path query with JSON encoding instead of Protobuf. Both wire | ||
| // formats must yield identical decoded responses. | ||
| func Test_DataSource_Load_WithMockServiceConnect_JSON(t *testing.T) { | ||
| baseURL, cleanup := setupTestConnectServer(t) | ||
| t.Cleanup(cleanup) | ||
|
|
||
| query := `query ComplexFilterTypeQuery($filter: ComplexFilterTypeInput!) { complexFilterType(filter: $filter) { id name } }` | ||
| variables := `{"variables":{"filter":{"filter":{"name":"Test Product","filterField1":"a","filterField2":"b"}}}}` | ||
|
|
||
| schemaDoc := grpctest.MustGraphQLSchema(t) | ||
| queryDoc, report := astparser.ParseGraphqlDocumentString(query) | ||
| if report.HasErrors() { | ||
| t.Fatalf("failed to parse query: %s", report.Error()) | ||
| } | ||
|
|
||
| compiler, err := NewProtoCompiler(grpctest.MustProtoSchema(t), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| transport := NewConnectTransport(ConnectTransportConfig{ | ||
| BaseURL: baseURL, | ||
| Encoding: ConnectEncodingJSON, | ||
| }) | ||
|
|
||
| ds, err := NewDataSource(transport, DataSourceConfig{ | ||
| Operation: &queryDoc, | ||
| Definition: &schemaDoc, | ||
| SubgraphName: "Products", | ||
| Compiler: compiler, | ||
| Mapping: &GRPCMapping{ | ||
| Service: "Products", | ||
| QueryRPCs: RPCConfigMap[RPCConfig]{ | ||
| "complexFilterType": { | ||
| RPC: "QueryComplexFilterType", | ||
| Request: "QueryComplexFilterTypeRequest", | ||
| Response: "QueryComplexFilterTypeResponse", | ||
| }, | ||
| }, | ||
| Fields: map[string]FieldMap{ | ||
| "Query": { | ||
| "complexFilterType": { | ||
| TargetName: "complex_filter_type", | ||
| ArgumentMappings: map[string]string{ | ||
| "filter": "filter", | ||
| }, | ||
| }, | ||
| }, | ||
| "FilterType": { | ||
| "name": {TargetName: "name"}, | ||
| "filterField1": {TargetName: "filter_field_1"}, | ||
| "filterField2": {TargetName: "filter_field_2"}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| output, err := ds.Load(context.Background(), nil, []byte(`{"query":"`+query+`","body":`+variables+`}`)) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Contains(t, string(output), `"id":"test-id-123"`) | ||
| require.Contains(t, string(output), `"name":"Test Product"`) | ||
| } | ||
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
Oops, something went wrong.
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.