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

feat(exec): Use alias provided by client for duplicated fields #28

Merged
merged 1 commit into from
Nov 18, 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
2 changes: 1 addition & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *Request) execSelections(ctx context.Context, sels []selected.Selection,
if needStrCounterpart {
out.WriteByte(',')
out.WriteByte('"')
out.WriteString(f.field.Alias + types.DUPLICATION_SUFFIX)
out.WriteString(f.field.CounterpartAlias)
out.WriteByte('"')
out.WriteByte(':')
if isArray {
Expand Down
2 changes: 2 additions & 0 deletions internal/exec/selected/selected.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type SchemaField struct {
Async bool
FixedResult reflect.Value
NeedStrCounterpart bool
CounterpartAlias string
}

type TypeAssertion struct {
Expand Down Expand Up @@ -160,6 +161,7 @@ func applySelectionSet(r *Request, s *resolvable.Schema, e *resolvable.Object, s
Sels: fieldSels,
Async: fe.HasContext || fe.ArgsPacker != nil || fe.HasError || HasAsyncSel(fieldSels),
NeedStrCounterpart: field.NeedStrCounterpart,
CounterpartAlias: field.CounterpartAlias,
})
}

Expand Down
12 changes: 7 additions & 5 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,29 @@ func parseFragment(l *common.Lexer) *types.FragmentDefinition {
func parseSelectionSet(l *common.Lexer) []types.Selection {
var sels []types.Selection
originalFields := make(map[string]*types.Field, 0)
needDuplicateFields := make([]string, 0)
needDuplicateFields := make(map[string]string, 0)
l.ConsumeToken('{')
for l.Peek() != '}' {
f := parseSelection(l)
switch sel := f.(type) {
case *types.Field:
if !strings.HasSuffix(sel.Alias.Name, types.DUPLICATION_SUFFIX) {
if !strings.HasSuffix(sel.Name.Name, types.DUPLICATION_SUFFIX) {
sels = append(sels, f)
originalFields[sel.Alias.Name] = sel
originalFields[sel.Name.Name] = sel
} else {
needDuplicateFields = append(needDuplicateFields, strings.ReplaceAll(sel.Alias.Name, types.DUPLICATION_SUFFIX, ""))
origFieldName := strings.ReplaceAll(sel.Name.Name, types.DUPLICATION_SUFFIX, "")
needDuplicateFields[origFieldName] = sel.Alias.Name
}
default:
sels = append(sels, f)
}
}
l.ConsumeToken('}')
for _, fieldName := range needDuplicateFields {
for fieldName, aliasName := range needDuplicateFields {
f, ok := originalFields[fieldName]
if ok {
f.NeedStrCounterpart = true
f.CounterpartAlias = aliasName
}
}
return sels
Expand Down
1 change: 1 addition & 0 deletions types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Field struct {
SelectionSet SelectionSet
SelectionSetLoc errors.Location
NeedStrCounterpart bool
CounterpartAlias string
}

func (Field) isSelection() {}
Expand Down