Skip to content

Commit

Permalink
Update federation plugin (#2876)
Browse files Browse the repository at this point in the history
* update 2.7 impl, add missing 2.6 support

* address feedback

* go fmt
  • Loading branch information
trevor-scheer committed Jan 30, 2024
1 parent 5524a39 commit 4d8b6ed
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 12 deletions.
11 changes: 7 additions & 4 deletions api/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ func Generate(cfg *config.Config, option ...Option) error {
if cfg.Federation.IsDefined() {
if cfg.Federation.Version == 0 { // default to using the user's choice of version, but if unset, try to sort out which federation version to use
urlRegex := regexp.MustCompile(`(?s)@link.*\(.*url:.*?"(.*?)"[^)]+\)`) // regex to grab the url of a link directive, should it exist

versionRegex := regexp.MustCompile(`v(\d+).(\d+)$`) // regex to grab the version number from a url
// check the sources, and if one is marked as federation v2, we mark the entirety to be generated using that format
for _, v := range cfg.Sources {
cfg.Federation.Version = 1
urlString := urlRegex.FindStringSubmatch(v.Input)
if urlString != nil && urlString[1] == "https://specs.apollo.dev/federation/v2.0" {
cfg.Federation.Version = 2
break
if urlString != nil && urlString[1] == "https://specs.apollo.dev/federation/v2.7" {
matches := versionRegex.FindStringSubmatch(urlString[1])
if matches[1] == "2" {
cfg.Federation.Version = 2
break
}
}
}
}
Expand Down
88 changes: 87 additions & 1 deletion api/testdata/federation2/graph/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/testdata/federation2/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# https://gqlgen.com/getting-started/
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.0",
@link(url: "https://specs.apollo.dev/federation/v2.7",
import: ["@key", "@shareable", "@provides", "@external", "@tag", "@extends", "@override", "@inaccessible"])

type Todo {
Expand Down
13 changes: 12 additions & 1 deletion plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (f *federation) MutateConfig(cfg *config.Config) error {
"federation__Scope": {
Model: config.StringList{"github.com/99designs/gqlgen/graphql.String"},
},
"federation__Policy": {
Model: config.StringList{"github.com/99designs/gqlgen/graphql.String"},
},
}

for typeName, entry := range builtins {
Expand All @@ -85,6 +88,7 @@ func (f *federation) MutateConfig(cfg *config.Config) error {
cfg.Directives["inaccessible"] = config.DirectiveConfig{SkipRuntime: true}
cfg.Directives["authenticated"] = config.DirectiveConfig{SkipRuntime: true}
cfg.Directives["requiresScopes"] = config.DirectiveConfig{SkipRuntime: true}
cfg.Directives["policy"] = config.DirectiveConfig{SkipRuntime: true}
cfg.Directives["interfaceObject"] = config.DirectiveConfig{SkipRuntime: true}
cfg.Directives["composeDirective"] = config.DirectiveConfig{SkipRuntime: true}
}
Expand Down Expand Up @@ -126,7 +130,13 @@ func (f *federation) InjectSourceEarly() *ast.Source {
| UNION
directive @interfaceObject on OBJECT
directive @link(import: [String!], url: String!) repeatable on SCHEMA
directive @override(from: String!) on FIELD_DEFINITION
directive @override(from: String!, label: String) on FIELD_DEFINITION
directive @policy(policies: [[federation__Policy!]!]!) on
| FIELD_DEFINITION
| OBJECT
| INTERFACE
| SCALAR
| ENUM
directive @provides(fields: FieldSet!) on FIELD_DEFINITION
directive @requires(fields: FieldSet!) on FIELD_DEFINITION
directive @requiresScopes(scopes: [[federation__Scope!]!]!) on
Expand All @@ -149,6 +159,7 @@ func (f *federation) InjectSourceEarly() *ast.Source {
| UNION
scalar _Any
scalar FieldSet
scalar federation__Policy
scalar federation__Scope
`
}
Expand Down
8 changes: 4 additions & 4 deletions plugin/federation/testdata/federation2/federation2.graphql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3",
import: ["@key", "@shareable", "@provides", "@external", "@tag", "@extends", "@override", "@inaccessible", "@interfaceObject"])
@link(url: "https://specs.apollo.dev/federation/v2.7",
import: ["@key", "@shareable", "@provides", "@external", "@tag", "@extends", "@override", "@inaccessible", "@interfaceObject", "@policy"])

schema {
query: CustomQuery
}

type Hello @key(fields:"name", resolvable: false) {
name: String!
name: String! @override(from: "old-service", label: "percent(5)")
}

type World @key(fields: "foo bar", resolvable: false) {
Expand All @@ -21,5 +21,5 @@ extend type ExternalExtension @key(fields: " upc ") {
}

type CustomQuery {
hello: Hello!
hello: Hello!
}
2 changes: 1 addition & 1 deletion plugin/federation/testdata/multi/multi.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3",
@link(url: "https://specs.apollo.dev/federation/v2.7",
import: ["@key"])

directive @entityResolver(multi: Boolean) on OBJECT
Expand Down

0 comments on commit 4d8b6ed

Please sign in to comment.