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

Fix union queries not working in @requires #3347

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 14 additions & 2 deletions plugin/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"errors"
"fmt"
"github.com/99designs/gqlgen/internal/rewrite"
rickbijkerk marked this conversation as resolved.
Show resolved Hide resolved
"sort"
"strings"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/99designs/gqlgen/internal/rewrite"
"github.com/99designs/gqlgen/plugin/federation/fieldset"
)

Expand Down 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 @@
// 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 == true {

Check failure on line 147 in plugin/federation/fieldset/fieldset.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.22)

S1002: should omit comparison to bool constant, can be simplified to `unionField` (gosimple)

Check failure on line 147 in plugin/federation/fieldset/fieldset.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.23)

S1002: should omit comparison to bool constant, can be simplified to `unionField` (gosimple)
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
16 changes: 16 additions & 0 deletions plugin/federation/testdata/explicitrequires/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ type World @key(fields: "hello { name } foo ") {
hello: Hello
}

type Person @key(fields: "name"){
name: String!
gender: Gender!
welcomeMessage: String @requires(fields:"gender { ... on Male {description} ... on Female {description}}")
}

union Gender = Male | Female

type Male {
description: String!
}

type Female {
description: String!
}

type WorldWithMultipleKeys @key(fields: "hello { name } foo ") @key(fields: "bar") {
foo: String!
bar: Int!
Expand Down
Loading