diff --git a/README.md b/README.md index ebed257..63405f7 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/config/config.go b/config/config.go index b5c4a60..9562a64 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/generator/generator.go b/generator/generator.go index 85c9033..05731ca 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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" ) @@ -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") + } + } + if err := cfg.LoadSchema(ctx); err != nil { return fmt.Errorf("failed to load schema: %w", err) } diff --git a/go.mod b/go.mod index 5144186..b513b52 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index 76d38ca..dabc58a 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=