Skip to content

Commit

Permalink
Fix union queries not working in @requires (#3347)
Browse files Browse the repository at this point in the history
* remove seamingly unneccessary type check

* implement logic to separate union/interface queries properly into their own 'field'

* add specific check to exclude union queries from typechecks

* add schema example to show it working

* fix feedback/linter issues

* include union @requires example in computed_requires

* go generate ./...

---------

Co-authored-by: Rick Bijkerk <rickbijkerk@bol.com>
  • Loading branch information
rickbijkerk and Rick Bijkerk authored Oct 30, 2024
1 parent 6cad444 commit 7b2e27b
Show file tree
Hide file tree
Showing 14 changed files with 1,369 additions and 3 deletions.
14 changes: 13 additions & 1 deletion plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,13 @@ func (f *Federation) GenerateCode(data *codegen.Data) error {
typeString := strings.Split(obj.Type.String(), ".")
requiresImports[strings.Join(typeString[:len(typeString)-1], ".")] = true

if containsUnionField(reqField) {
continue
}

cgField := reqField.Field.TypeReference(obj, data.Objects)
reqField.Type = cgField.TypeReference
}

// add type info to entity
e.Type = obj.Type
}
Expand Down Expand Up @@ -329,6 +332,15 @@ func (f *Federation) GenerateCode(data *codegen.Data) error {
})
}

func containsUnionField(reqField *Requires) bool {
for _, requireFields := range reqField.Field {
if strings.HasPrefix(requireFields, "... on") {
return true
}
}
return false
}

// Fill in types for key fields
func populateKeyFieldTypes(
resolver *EntityResolver,
Expand Down
14 changes: 14 additions & 0 deletions plugin/federation/fieldset/fieldset.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ func (f Field) LastIndex() int {
// parseUnnestedKeyFieldSet // handles simple case where none of the fields are nested.
func parseUnnestedKeyFieldSet(raw string, prefix []string) Set {
ret := Set{}
unionField := false

for _, s := range strings.Fields(raw) {
if s == "..." {
continue
}
if s == "on" {
unionField = true
continue
}

if unionField {
s = "... on " + s
unionField = false
}

next := append(prefix[0:len(prefix):len(prefix)], s) //nolint:gocritic // set cap=len in order to force slice reallocation
ret = append(ret, next)
}
Expand Down

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

Loading

0 comments on commit 7b2e27b

Please sign in to comment.