Skip to content
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
22 changes: 5 additions & 17 deletions v2/pkg/engine/resolve/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package resolve

import "errors"

const (
locationsField = "locations"
)

var (
lBrace = []byte("{")
rBrace = []byte("}")
Expand All @@ -16,7 +20,7 @@ var (
literalFalse = []byte("false")
literalErrors = []byte("errors")
literalMessage = []byte("message")
literalLocations = []byte("locations")
literalLocations = []byte(locationsField)
literalPath = []byte("path")
literalUnderscoreEntities = []byte("_entities")
literalExtensions = []byte("extensions")
Expand All @@ -35,19 +39,3 @@ var (
errHeaderPathInvalid = errors.New("invalid header path: header variables must be of this format: .request.header.{{ key }} ")
ErrUnableToResolve = errors.New("unable to resolve operation")
)

var (
Comment thread
SkArchon marked this conversation as resolved.
errorPaths = [][]string{
{"message"},
{"locations"},
{"path"},
{"extensions"},
}
)

const (
errorsMessagePathIndex = 0
errorsLocationsPathIndex = 1
errorsPathPathIndex = 2
errorsExtensionsPathIndex = 3
)
46 changes: 41 additions & 5 deletions v2/pkg/engine/resolve/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,12 +936,48 @@ func (l *Loader) optionallyOmitErrorFields(values []*astjson.Value) {

// optionallyOmitErrorLocations removes the "locations" object from all values.
func (l *Loader) optionallyOmitErrorLocations(values []*astjson.Value) {
if !l.omitSubgraphErrorLocations {
return
}
for _, value := range values {
if value.Exists("locations") {
value.Del("locations")
// If the flag is set, delete all locations
if !value.Exists(locationsField) || l.omitSubgraphErrorLocations {
value.Del(locationsField)
continue
}

locations := value.Get(locationsField)
if locations.Type() != astjson.TypeArray {
continue
}

locationsArray := locations.GetArray()
Comment thread
Noroth marked this conversation as resolved.
Outdated
locationsClone := slices.Clone(locationsArray)
Comment thread
Noroth marked this conversation as resolved.
Outdated

deletedEntries := 0
locationsArrayLength := len(locationsArray)

// We loop on a clone since we delete elements inline
for i, loc := range locationsClone {
line := loc.Get("line")
column := loc.Get("column")

// Skip invalid locations: nil values or values <= 0
if line == nil || column == nil {
locations.Del(strconv.Itoa(i - deletedEntries))
deletedEntries++
continue
}

// Keep location only if both line and column are > 0
// In case it is not an int, 0 will be returned which is invalid anyway
isValid := line.GetInt() > 0 && column.GetInt() > 0
if !isValid {
locations.Del(strconv.Itoa(i - deletedEntries))
deletedEntries++
}
Comment thread
Noroth marked this conversation as resolved.
}

// If all locations were invalid, delete the locations field
if locationsArrayLength == deletedEntries {
value.Del(locationsField)
Comment thread
Noroth marked this conversation as resolved.
Outdated
}
}
}
Expand Down
Loading