Skip to content
Closed
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
20 changes: 15 additions & 5 deletions internal/oe/federator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

func BuildProviderLevelQuery(fieldsMap *[]ProviderLevelFieldRecord) []*FederationServiceAST {
var queries []*FederationServiceAST
var addedServiceKeys []string
// Dedup by service+schema: one provider may expose multiple schemaIds.
var addedQueryKeys []string

for _, field := range *fieldsMap {

serviceKey := field.ServiceKey
queryKey := serviceKey + "\x00" + field.SchemaId

if !contains(addedServiceKeys, serviceKey) {
addedServiceKeys = append(addedServiceKeys, serviceKey)
if !contains(addedQueryKeys, queryKey) {
addedQueryKeys = append(addedQueryKeys, queryKey)
queries = append(queries, &FederationServiceAST{
ServiceKey: serviceKey,
SchemaID: field.SchemaId,
Expand All @@ -38,9 +39,12 @@ func BuildProviderLevelQuery(fieldsMap *[]ProviderLevelFieldRecord) []*Federatio
},
})
}
// find the query with the service key
// Attach fields to the query matching both service key and schema ID.
for _, q := range queries {
if q.ServiceKey == serviceKey && q.SchemaID == field.SchemaId {
if field.FieldPath == "" {
break
}
args := strings.Split(field.FieldPath, ".")
pushFieldToAst(args, q.QueryAst.Definitions[0].(*ast.OperationDefinition).SelectionSet)
break
Expand Down Expand Up @@ -133,6 +137,9 @@ func BuildArrayProviderQuery(fieldsMap []string, arrayFields []string) []*Federa
}

func pushFieldToAst(field []string, parentField *ast.SelectionSet) {
if len(field) == 0 {
return
}
// loop through selectionSets to match the field path.
current := field[0]

Expand Down Expand Up @@ -202,6 +209,9 @@ func pushFieldToAst(field []string, parentField *ast.SelectionSet) {

// pushArrayFieldToAst handles array fields specifically, ensuring proper array structure
func pushArrayFieldToAst(field []string, parentField *ast.SelectionSet) {
if len(field) == 0 {
return
}
// For array fields, we need to handle the structure differently
current := field[0]

Expand Down
54 changes: 54 additions & 0 deletions internal/oe/federator/federator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,60 @@ func TestBuildProviderLevelQuery(t *testing.T) {
}
}

func TestBuildProviderLevelQuery_SameServiceDifferentSchemas(t *testing.T) {
fieldsMap := &[]ProviderLevelFieldRecord{
{ServiceKey: "drp", SchemaId: "schema-v1", FieldPath: "person.fullName"},
{ServiceKey: "drp", SchemaId: "schema-v2", FieldPath: "person.address"},
{ServiceKey: "drp", SchemaId: "schema-v1", FieldPath: "person.nic"},
}

queries := BuildProviderLevelQuery(fieldsMap)
require.Len(t, queries, 2)

bySchema := map[string]*FederationServiceAST{}
for _, q := range queries {
assert.Equal(t, "drp", q.ServiceKey)
bySchema[q.SchemaID] = q
}
require.Contains(t, bySchema, "schema-v1")
require.Contains(t, bySchema, "schema-v2")

v1Op := bySchema["schema-v1"].QueryAst.Definitions[0].(*ast.OperationDefinition)
v2Op := bySchema["schema-v2"].QueryAst.Definitions[0].(*ast.OperationDefinition)

// v1 got fullName + nic under person; v2 got address under person
assert.ElementsMatch(t, []string{"person"}, extractFieldNames(v1Op.SelectionSet))
assert.ElementsMatch(t, []string{"person"}, extractFieldNames(v2Op.SelectionSet))

personV1 := v1Op.SelectionSet.Selections[0].(*ast.Field)
personV2 := v2Op.SelectionSet.Selections[0].(*ast.Field)
assert.ElementsMatch(t, []string{"fullName", "nic"}, extractFieldNames(personV1.SelectionSet))
assert.ElementsMatch(t, []string{"address"}, extractFieldNames(personV2.SelectionSet))
}

func TestBuildProviderLevelQuery_EmptyFieldPathSkipped(t *testing.T) {
fieldsMap := &[]ProviderLevelFieldRecord{
{ServiceKey: "drp", SchemaId: "schema1", FieldPath: ""},
{ServiceKey: "drp", SchemaId: "schema1", FieldPath: "person.fullName"},
}
queries := BuildProviderLevelQuery(fieldsMap)
require.Len(t, queries, 1)
op := queries[0].QueryAst.Definitions[0].(*ast.OperationDefinition)
assert.ElementsMatch(t, []string{"person"}, extractFieldNames(op.SelectionSet))
}

func TestBuildArrayProviderQuery_DotlessPathNoPanic(t *testing.T) {
// Path with no '.' yields empty args[1:]; must not panic.
assert.NotPanics(t, func() {
queries := BuildArrayProviderQuery([]string{"drp"}, []string{"dmt"})
require.Len(t, queries, 2)
for _, q := range queries {
op := q.QueryAst.Definitions[0].(*ast.OperationDefinition)
assert.Empty(t, op.SelectionSet.Selections)
}
})
}

// Helper function to extract field names from selection set
func extractFieldNames(selectionSet *ast.SelectionSet) []string {
var fields []string
Expand Down
Loading