Skip to content

Commit 5040763

Browse files
committed
feat: set operation name also with persistent queries
1 parent 223d43a commit 5040763

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

router/core/operation_processor.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,40 @@ func (o *OperationKit) setAndParseOperationDoc() error {
897897
return nil
898898
}
899899

900+
func (o *OperationKit) populateOperationNameFromDoc() {
901+
if o.parsedOperation.Request.OperationName != "" {
902+
return
903+
}
904+
if o.kit == nil || o.kit.doc == nil {
905+
return
906+
}
907+
908+
ref := o.operationDefinitionRef
909+
if ref < 0 || ref >= len(o.kit.doc.OperationDefinitions) {
910+
ref = ast.InvalidRef
911+
}
912+
if ref == ast.InvalidRef {
913+
for i := range o.kit.doc.RootNodes {
914+
if o.kit.doc.RootNodes[i].Kind == ast.NodeKindOperationDefinition {
915+
ref = o.kit.doc.RootNodes[i].Ref
916+
break
917+
}
918+
}
919+
}
920+
if ref == ast.InvalidRef {
921+
return
922+
}
923+
924+
name := o.kit.doc.OperationDefinitionNameString(ref)
925+
if name == "" {
926+
return
927+
}
928+
929+
o.parsedOperation.Request.OperationName = name
930+
o.operationDefinitionRef = ref
931+
o.originalOperationNameRef = o.kit.doc.OperationDefinitions[ref].Name
932+
}
933+
900934
func (o *OperationKit) normalizeVariablesCacheKey() uint64 {
901935
_, _ = o.kit.keyGen.Write(o.kit.doc.Input.Variables)
902936
_, _ = o.kit.keyGen.WriteString(o.parsedOperation.NormalizedRepresentation)
@@ -1144,6 +1178,7 @@ func (o *OperationKit) handleFoundPersistedOperationEntry(entry NormalizationCac
11441178
if err != nil {
11451179
return err
11461180
}
1181+
o.populateOperationNameFromDoc()
11471182
return nil
11481183
}
11491184

router/core/operation_processor_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ func TestOperationProcessorPersistentOperations(t *testing.T) {
7575
}
7676
}
7777

78+
func TestPersistedOperationCachePopulatesOperationName(t *testing.T) {
79+
executor := &Executor{
80+
PlanConfig: plan.Configuration{},
81+
RouterSchema: nil,
82+
Resolver: nil,
83+
RenameTypeNames: nil,
84+
}
85+
processor := NewOperationProcessor(OperationProcessorOptions{
86+
Executor: executor,
87+
MaxOperationSizeInBytes: 10 << 20,
88+
ParseKitPoolSize: 1,
89+
})
90+
91+
kit, err := processor.NewKit()
92+
require.NoError(t, err)
93+
defer kit.Free()
94+
95+
entry := NormalizationCacheEntry{
96+
normalizedRepresentation: "query TestOperation { a }",
97+
operationType: "query",
98+
}
99+
100+
err = kit.handleFoundPersistedOperationEntry(entry)
101+
require.NoError(t, err)
102+
require.Equal(t, "TestOperation", kit.parsedOperation.Request.OperationName)
103+
}
104+
78105
func TestParseOperationProcessor(t *testing.T) {
79106
executor := &Executor{
80107
PlanConfig: plan.Configuration{},

0 commit comments

Comments
 (0)