Skip to content

feat: add support for aliases#1209

Merged
Noroth merged 4 commits intomasterfrom
ludwig/eng-7072-support-aliases
Jul 7, 2025
Merged

feat: add support for aliases#1209
Noroth merged 4 commits intomasterfrom
ludwig/eng-7072-support-aliases

Conversation

@Noroth
Copy link
Copy Markdown
Contributor

@Noroth Noroth commented Jul 4, 2025

Summary by CodeRabbit

  • New Features

    • Improved support for GraphQL field aliases in gRPC execution plans, ensuring aliases are correctly handled and reflected in responses, including nested objects, interfaces, unions, mutations, and enums.
  • Bug Fixes

    • Enhanced error handling during RPC compilation to prevent issues with missing or invalid message definitions.
  • Tests

    • Added comprehensive tests verifying correct handling of GraphQL aliases in execution plans and gRPC responses.
  • Chores

    • Updated internal mappings and configuration for mutation operations and field mappings.

Checklist

  • I have discussed my proposed changes in an issue and have received approval to proceed.
  • I have followed the coding standards of the project.
  • Tests or benchmarks have been added or updated.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 4, 2025

Walkthrough

The changes add explicit error handling in RPC compilation, introduce alias tracking in the RPC execution plan, and update response marshaling to use aliases as JSON keys. They include new methods and tests verifying alias handling in queries and mutations, and update RPC mapping configurations with renamed mutation RPCs and additional field mappings.

Changes

Files / Areas Changed Change Summary
v2/pkg/engine/datasource/grpc_datasource/compiler.go Added explicit error handling for missing input/output protobuf messages in RPCCompiler.Compile.
v2/pkg/engine/datasource/grpc_datasource/execution_plan.go Added AliasOrPath() method to RPCField struct to return the alias if set, otherwise the JSON path.
v2/pkg/engine/datasource/grpc_datasource/execution_plan_test.go Added TestProductExecutionPlanWithAliases with multiple test cases verifying execution plan generation for queries and mutations with field aliases, covering various levels of complexity and GraphQL type features.
v2/pkg/engine/datasource/grpc_datasource/execution_plan_visitor.go Set the Alias field in RPCField during field entry in execution plan visitor, using the operation's field alias string.
v2/pkg/engine/datasource/grpc_datasource/grpc_datasource.go Replaced all uses of field.JSONPath with field.AliasOrPath() in marshalResponseJSON to ensure JSON output uses aliases as keys when present.
v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go Added Test_DataSource_Load_WithAliases, a comprehensive test suite running real gRPC queries with aliases, validating correct alias handling in responses across query, mutation, nested, interface, union, and enum scenarios.
v2/pkg/engine/datasource/grpc_datasource/mapping_test_helper.go Updated mutation RPC config in testMapping to use "MutationCreateUser" naming; no changes to function signatures or logic.
v2/pkg/grpctest/mapping/mapping.go Updated mutation RPC config for "createUser" to use "MutationCreateUser" naming; added new field mappings for "SearchResult" and "ActionResult" types. No changes to exported function signatures.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go (1)

1294-1329: Consider refactoring duplicated test setup code.

The gRPC server setup code here duplicates logic from the existing setupTestGRPCServer function. Consider extracting this into a reusable helper to reduce code duplication and improve maintainability.

Apply this refactor to use the existing helper:

-	// Set up the bufconn listener
-	lis := bufconn.Listen(1024 * 1024)
-
-	// Create a new gRPC server
-	server := grpc.NewServer()
-
-	// Register our mock service implementation
-	mockService := &grpctest.MockService{}
-	productv1.RegisterProductServiceServer(server, mockService)
-
-	// Start the server in a goroutine
-	go func() {
-		if err := server.Serve(lis); err != nil {
-			t.Errorf("failed to serve: %v", err)
-		}
-	}()
-
-	// Clean up the server when the test completes
-	defer server.Stop()
-
-	// Create a buffer-based dialer
-	bufDialer := func(context.Context, string) (net.Conn, error) {
-		return lis.Dial()
-	}
-
-	// Connect using bufconn dialer
-	// see https://github.com/grpc/grpc-go/issues/7091
-	// nolint: staticcheck
-	conn, err := grpc.Dial(
-		"bufnet",
-		grpc.WithTransportCredentials(insecure.NewCredentials()),
-		grpc.WithContextDialer(bufDialer),
-		grpc.WithLocalDNSResolution(),
-	)
-	require.NoError(t, err)
-	defer conn.Close()
+	conn, cleanup := setupTestGRPCServer(t)
+	t.Cleanup(cleanup)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e9b9f19 and 2848e93.

📒 Files selected for processing (8)
  • v2/pkg/engine/datasource/grpc_datasource/compiler.go (1 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/execution_plan.go (1 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/execution_plan_test.go (1 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/execution_plan_visitor.go (1 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/grpc_datasource.go (4 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go (1 hunks)
  • v2/pkg/engine/datasource/grpc_datasource/mapping_test_helper.go (1 hunks)
  • v2/pkg/grpctest/mapping/mapping.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
v2/pkg/engine/datasource/grpc_datasource/execution_plan.go (1)
pkg/ast/ast_field_alias.go (1)
  • Alias (8-12)
v2/pkg/engine/datasource/grpc_datasource/execution_plan_visitor.go (1)
pkg/ast/ast_field_alias.go (1)
  • Alias (8-12)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build and test (go 1.23 / windows-latest)
  • GitHub Check: Build and test (go 1.23 / windows-latest)
🔇 Additional comments (9)
v2/pkg/engine/datasource/grpc_datasource/execution_plan.go (1)

169-176: LGTM! Clean alias fallback implementation.

The AliasOrPath() method provides a clean abstraction for accessing the effective field name, properly prioritizing aliases over default JSON paths. The implementation is straightforward and follows good practices.

v2/pkg/engine/datasource/grpc_datasource/execution_plan_visitor.go (1)

336-336: Excellent integration of alias support.

The alias assignment using r.operation.FieldAliasString(ref) is correctly placed within the RPCField construction and properly integrates with the new AliasOrPath() method for comprehensive alias support.

v2/pkg/engine/datasource/grpc_datasource/mapping_test_helper.go (1)

90-92: Good naming convention improvement.

The update to prefix mutation RPC names with "Mutation" improves consistency and clarity in the mapping configuration. This aligns well with the corresponding changes in the main mapping file.

v2/pkg/engine/datasource/grpc_datasource/grpc_datasource.go (1)

166-166: Comprehensive alias support in JSON marshaling.

The systematic replacement of field.JSONPath with field.AliasOrPath() throughout the marshalResponseJSON method ensures that GraphQL field aliases are properly reflected in the response JSON keys. This completes the alias support implementation by connecting the execution plan aliases to the final response output.

Also applies to: 172-172, 188-188, 193-193, 217-217, 232-232, 238-238

v2/pkg/grpctest/mapping/mapping.go (2)

92-94: Consistent RPC naming convention update.

The mutation RPC configuration update maintains consistency with the test helper mappings, properly standardizing the naming convention with "Mutation" prefix.


472-484: Well-structured field mappings for new types.

The added field mappings for SearchResult and ActionResult types properly handle GraphQL-to-gRPC field name conversions, following the established snake_case naming convention for gRPC fields.

v2/pkg/engine/datasource/grpc_datasource/compiler.go (1)

326-333: Excellent error handling improvement!

The explicit validation of input and output messages with descriptive error messages prevents compilation from proceeding with invalid message definitions. This enhances robustness and provides clear feedback when protobuf messages are missing from the document.

v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go (1)

1291-1574: Comprehensive alias test coverage with excellent validation!

This test thoroughly covers various GraphQL alias scenarios including root fields, nested objects, interfaces, unions, mutations, and enums. The validation logic properly ensures aliased fields appear correctly and original field names are absent, which is crucial for alias functionality verification.

v2/pkg/engine/datasource/grpc_datasource/execution_plan_test.go (1)

2514-3120: Excellent test coverage for GraphQL alias support!

The new TestProductExecutionPlanWithAliases function provides comprehensive coverage of alias functionality across all major GraphQL features including simple fields, nested objects, interfaces, unions, mutations, and complex input types. The test cases are well-structured and follow the established patterns in the file.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go (1)

1290-1539: Excellent comprehensive test coverage for alias functionality!

This test function provides thorough coverage of GraphQL alias scenarios including root fields, nested objects, interfaces, unions, mutations, and enum fields. The validation logic correctly verifies that:

  1. Aliased field names appear in the JSON response
  2. Original field names are absent from the response
  3. Data values are correctly preserved

The test cases align well with the PR objectives for adding alias support and demonstrate end-to-end functionality from query parsing to response marshaling.

Consider splitting into separate test functions for better organization.

While the comprehensive coverage is excellent, this single test function contains 8 different test cases which could make debugging more difficult when specific scenarios fail. Consider splitting this into separate focused test functions like:

  • Test_DataSource_Load_WithSimpleAliases
  • Test_DataSource_Load_WithNestedAliases
  • Test_DataSource_Load_WithInterfaceUnionAliases
  • Test_DataSource_Load_WithMutationAliases

This would make it easier to identify which specific alias scenario is failing and improve test maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2848e93 and 3887a3f.

📒 Files selected for processing (1)
  • v2/pkg/engine/datasource/grpc_datasource/grpc_datasource_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build and test (go 1.23 / windows-latest)
  • GitHub Check: Build and test (go 1.23 / windows-latest)

Copy link
Copy Markdown
Collaborator

@StarpTech StarpTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Noroth Noroth merged commit 9223351 into master Jul 7, 2025
10 checks passed
@Noroth Noroth deleted the ludwig/eng-7072-support-aliases branch July 7, 2025 07:22
devsergiy pushed a commit that referenced this pull request Jul 7, 2025
🤖 I have created a release *beep* *boop*
---


##
[2.0.0-rc.199](v2.0.0-rc.198...v2.0.0-rc.199)
(2025-07-07)


### Features

* add support for aliases
([#1209](#1209))
([9223351](9223351))


### Bug Fixes

* do not trim whitespaces around non-block strings
([#1211](#1211))
([6f5046b](6f5046b))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
  * Added support for aliases.

* **Bug Fixes**
* Resolved an issue to prevent trimming whitespaces around non-block
strings.

* **Documentation**
  * Updated the changelog with details for version 2.0.0-rc.199.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@coderabbitai coderabbitai Bot mentioned this pull request Jul 14, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants