Skip to content
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

Make gqlgenc recognize Apollo Federation related directives #197

Merged
merged 1 commit into from
Feb 1, 2024

Conversation

110y
Copy link
Contributor

@110y 110y commented Jan 29, 2024

What

  • Make gqlgenc recognize Apollo Federation related GraphQL directives.

Why

Current gqlgenc raises an Undefined directive error when a target GraphQL schema includes Apollo Federation related directives. This is just because gqlgenc does not recognize unknown directives not declared in a schema.
Regarding the original gqlgen, it has special treatments for Apollo Federation, so we don't have to explicitly declare Apollo Federation related directives in our schema.
Given that, I think it is better to make gqlgenc recognize Apollo Federation related directives like the original gqlgen.

Summary by CodeRabbit

  • New Features
    • Introduced support for Apollo Federation with a configurable version, enhancing schema definition capabilities for distributed GraphQL architectures.

Copy link

coderabbitai bot commented Jan 29, 2024

Walkthrough

The update introduces a new federation field in the configuration, specifically for Apollo Federation with a version: 2 directive. It enhances the GraphQL schema to support federation by adding necessary federation directives, conditional on the federation version being set. This allows for more flexible and scalable GraphQL architecture by enabling services to be composed into a single federated schema.

Changes

Files Change Summary
README.md
config/config.go
Added the federation field with the version: 2 directive under the models section in the configuration file, affecting the schema definition for Apollo Federation and included a Federation field of type config.PackageConfig to the Config struct.
generator/generator.go Modified to include the import of the federation package from github.com/99designs/gqlgen/plugin/federation and added a conditional block to inject federation directives into the GraphQL schema if the federation version is not 0.

🐇✨
In the land of code and schema, where the data rivers flow,
A rabbit hopped, with updates in tow,
"Federation!" it cheered, with a leap so high,
"For a united graph, under one sky."
🌌🚀

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

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>.
    • Generate unit-tests for this file.
  • 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 generate unit tests for this file.
    • @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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 863b724 and a3445b5.
Files ignored due to path filters (2)
  • go.mod is excluded by: !**/*.mod
  • go.sum is excluded by: !**/*.sum
Files selected for processing (3)
  • README.md (2 hunks)
  • config/config.go (1 hunks)
  • generator/generator.go (2 hunks)
Additional comments: 6
generator/generator.go (1)
  • 11-11: The import of the federation package from github.com/99designs/gqlgen/plugin/federation is correctly added to support Apollo Federation directives.
README.md (3)
  • 45-46: The addition of the federation field with the version: 2 directive under the models section in the configuration file is correctly documented. This change effectively communicates how to enable Apollo Federation support.
  • 72-73: The documentation for adding Apollo Federation directives in a local schema configuration is clear and follows the same pattern as the remote configuration. This consistency helps users understand how to configure federation support regardless of schema source.
  • 42-49: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-100]

The README.md updates provide clear instructions on how to configure Apollo Federation support in gqlgenc. Ensure that all new configuration options are also documented in the official documentation or wiki for comprehensive coverage.

config/config.go (2)
  • 27-27: The addition of the Federation field of type config.PackageConfig to the Config struct is correctly implemented. This change aligns with the PR objectives to support Apollo Federation by adding necessary configuration options.
  • 24-30: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-100]

The Config struct now includes the Federation field, which is a significant addition for supporting Apollo Federation. Ensure that all functions and methods that manipulate or access the Config struct are updated to consider this new field appropriately.

Comment on lines +56 to +62
if cfg.Federation.Version != 0 {
if fed, ok := federation.New(cfg.Federation.Version).(plugin.EarlySourceInjector); ok {
if source := fed.InjectSourceEarly(); source != nil {
cfg.GQLConfig.Sources = append(cfg.GQLConfig.Sources, source)
}
} else {
return errors.New("failed to inject federation directives")
}
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search for validation of the federation version in the codebase only returned references in generator/generator.go, specifically in the conditional block we are reviewing and the instantiation of a federation object. There are no explicit checks elsewhere in the code to ensure that cfg.Federation.Version is a positive integer beyond the check that it is not 0.

Given this context, it appears that there is no further validation of the federation version beyond ensuring it is not 0. This could potentially allow for invalid federation version values that are negative, assuming the type allows for such values, which is not explicitly verified but can be inferred based on common Go practices and the nature of versioning (typically non-negative).

Since the verification script did not find any additional validation logic for the federation version, and based on the information provided, it seems that the initial review comment stands correct in its advice but also reveals a potential oversight in validation logic within the codebase.

Analysis chain

The conditional logic to inject federation directives into the GraphQL schema only if the federation version is not 0 is correctly implemented. However, ensure that the federation version is validated elsewhere in the code to prevent invalid values.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that federation version is validated to be a positive integer elsewhere in the code.
rg "cfg.Federation.Version" --type go

Length of output: 207

Comment on lines +56 to +63
if cfg.Federation.Version != 0 {
if fed, ok := federation.New(cfg.Federation.Version).(plugin.EarlySourceInjector); ok {
if source := fed.InjectSourceEarly(); source != nil {
cfg.GQLConfig.Sources = append(cfg.GQLConfig.Sources, source)
}
} else {
return errors.New("failed to inject federation directives")
}
}
Copy link

Choose a reason for hiding this comment

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

The implementation for injecting federation directives is clear and follows the intended logic. However, consider adding a log or debug statement before returning an error in line 62 for better debugging and operational visibility.

+ log.Printf("Federation version: %d is not supported for EarlySourceInjector", cfg.Federation.Version)
return errors.New("failed to inject federation directives")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if cfg.Federation.Version != 0 {
if fed, ok := federation.New(cfg.Federation.Version).(plugin.EarlySourceInjector); ok {
if source := fed.InjectSourceEarly(); source != nil {
cfg.GQLConfig.Sources = append(cfg.GQLConfig.Sources, source)
}
} else {
return errors.New("failed to inject federation directives")
}
}
if cfg.Federation.Version != 0 {
if fed, ok := federation.New(cfg.Federation.Version).(plugin.EarlySourceInjector); ok {
if source := fed.InjectSourceEarly(); source != nil {
cfg.GQLConfig.Sources = append(cfg.GQLConfig.Sources, source)
}
} else {
log.Printf("Federation version: %d is not supported for EarlySourceInjector", cfg.Federation.Version)
return errors.New("failed to inject federation directives")
}
}

"github.com/99designs/gqlgen/plugin/modelgen"

Copy link
Owner

Choose a reason for hiding this comment

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

Please run make fmt as it seems the code does not adhere to the gci format.

Copy link
Owner

@Yamashou Yamashou left a comment

Choose a reason for hiding this comment

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

@110y Everything looks good aside from the formatting!

Copy link

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 863b724 and a88dcd6.
Files ignored due to path filters (2)
  • go.mod is excluded by: !**/*.mod
  • go.sum is excluded by: !**/*.sum
Files selected for processing (3)
  • README.md (2 hunks)
  • config/config.go (1 hunks)
  • generator/generator.go (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • README.md
  • config/config.go
  • generator/generator.go

@110y
Copy link
Contributor Author

110y commented Jan 31, 2024

@Yamashou

Ah sorry, fixed it! I appreciate it if you could take another look, thanks 🙏

@Yamashou Yamashou merged commit 48be91a into Yamashou:master Feb 1, 2024
1 check passed
@110y 110y deleted the federation-directives branch February 1, 2024 00:29
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