Skip to content

[BUG?]: graphql resolver depth filter depends on path collapsing #7468

@ab-pm

Description

@ab-pm

Bug Report

I notice while inspecting the code of the graphql instrumentation/plugin that the depth config option has a rather peculiar behaviour. The shouldInstrument function

function shouldInstrument (config, path) {
let depth = 0
for (const item of path) {
if (typeof item === 'string') {
depth += 1
}
}
return config.depth < 0 || config.depth >= depth
}

counts the number of non-numeric path segments, i.e. treating a selection on a field returning a list (or list of lists, etc) as a single level. However, with the collapse config option enabled (which is the default), the numbers in the path get transformed into the string '*'
.map(segment => typeof segment === 'number' ? '*' : segment)
before the path array is passed into shouldInstrument:
const path = getPath(info, this.config)
if (!shouldInstrument(this.config, path)) return

So when collapsing is turned off, less path segments are counted and more (deeper) resolver calls are instrumented. You can easily test this by

Reproduction Code

You can try this yourself by running the "graphql with a depth >=1 should only instrument up to the specified depth" test. Change

return agent.load('graphql', { depth: 2 })
to

          return agent.load('graphql', { depth: 2, collapse: false })

and the test fails because the trace now also contains spans for friends.0.name and friends.1.name.

Question

Is this considered a bug or a feature? Or an oversight? What is the desired behaviour?

I'm about to open a PR with some optimisations in this place, and would like to know if it will need to keep exact compatibility with the current behaviour.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions