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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ models:
model: github.com/99designs/gqlgen/graphql.Int64
Date:
model: github.com/99designs/gqlgen/graphql.Time
federation: # Add this if your schema includes Apollo Federation related directives
version: 2
endpoint:
url: https://api.annict.com/graphql # Where do you want to send your request?
headers: # If you need header for getting introspection query, set it
Expand All @@ -67,6 +69,8 @@ models:
model: github.com/99designs/gqlgen/graphql.Int64
Date:
model: github.com/99designs/gqlgen/graphql.Time
federation: # Add this if your schema includes Apollo Federation related directives
version: 2
schema:
- "schema/**/*.graphql" # Where are all the schema files located?
query:
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
SchemaFilename StringList `yaml:"schema,omitempty"`
Model config.PackageConfig `yaml:"model,omitempty"`
Client config.PackageConfig `yaml:"client,omitempty"`
Federation config.PackageConfig `yaml:"federation,omitempty"`
Models config.TypeMap `yaml:"models,omitempty"`
Endpoint *EndPointConfig `yaml:"endpoint,omitempty"`
Generate *GenerateConfig `yaml:"generate,omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package generator

import (
"context"
"errors"
"fmt"
"sort"

"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/plugin"
"github.com/99designs/gqlgen/plugin/federation"
"github.com/99designs/gqlgen/plugin/modelgen"
"github.com/Yamashou/gqlgenc/config"
)
Expand Down Expand Up @@ -50,6 +52,16 @@ func Generate(ctx context.Context, cfg *config.Config, option ...api.Option) err
o(cfg.GQLConfig, &plugins)
}

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")
}
Comment on lines +55 to +62
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 +55 to +63
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")
}
}


if err := cfg.LoadSchema(ctx); err != nil {
return fmt.Errorf("failed to load schema: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
github.com/vektah/gqlparser/v2 v2.5.10
golang.org/x/text v0.13.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -21,7 +22,6 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 1 addition & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand All @@ -35,17 +33,11 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
Loading