Skip to content

Commit

Permalink
feat: propagate operation name (#993)
Browse files Browse the repository at this point in the history
Co-authored-by: spetrunin <neyasut@gmail.com>
  • Loading branch information
Noroth and devsergiy authored Nov 25, 2024
1 parent 611d683 commit fe24f2b
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/astvisitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ type (
EnterFragmentSpreadVisitor
LeaveFragmentSpreadVisitor
}
// EnterFragmentSpreadVisitor is the callback when the walker enters an inline framgnet
// EnterInlineFragmentVisitor is the callback when the walker enters an inline fragment
EnterInlineFragmentVisitor interface {
// EnterInlineFragment gets called when the walker enters an inline fragment
// ref is the reference to the selection set on the AST
Expand Down
28 changes: 28 additions & 0 deletions v2/pkg/engine/datasource/graphql_datasource/graphql_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -398,14 +400,40 @@ func (p *Planner[T]) ConfigureSubscription() plan.SubscriptionConfiguration {
}
}

func (p *Planner[T]) buildUpstreamOperationName(ref int) string {
operationName := p.visitor.Operation.OperationDefinitionNameBytes(ref)
if len(operationName) == 0 {
return ""
}

fetchID := strconv.Itoa(p.dataSourcePlannerConfig.FetchID)

builder := strings.Builder{}
builder.Grow(len(operationName) + len(p.dataSourceConfig.Name()) + len(fetchID) + 4) // 4 is for delimiters "__"

builder.Write(operationName)
builder.WriteString("__" + p.dataSourceConfig.Name() + "__" + fetchID)

return builder.String()
}

func (p *Planner[T]) EnterOperationDefinition(ref int) {
operationType := p.visitor.Operation.OperationDefinitions[ref].OperationType
if p.dataSourcePlannerConfig.IsNested {
operationType = ast.OperationTypeQuery
}

definition := p.upstreamOperation.AddOperationDefinitionToRootNodes(ast.OperationDefinition{
OperationType: operationType,
})

if p.dataSourcePlannerConfig.Options.EnableOperationNamePropagation {
operation := p.buildUpstreamOperationName(ref)
if operation != "" {
p.upstreamOperation.OperationDefinitions[definition.Ref].Name = p.upstreamOperation.Input.AppendInputString(operation)
}
}

p.nodes = append(p.nodes, definition)
}

Expand Down
Loading

0 comments on commit fe24f2b

Please sign in to comment.