Skip to content

@resolveTo directive fail to infer sourceSelectionSet when custom result is defined #9435

Description

@muradm

Issue workflow progress

Progress of the issue based on the
Contributor Workflow

Make sure to fork this template and run yarn generate in the terminal.

Please make sure Mesh package versions under package.json matches yours.

  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug

I'm using mesh cli to join multiple encapsulated subgraphs to form supergraph. In addition to generated schema, I add custom field using additionalTypeDefs under one subgraph referring another using @resolveTo directive.

  extend type TargetType {
    complexDataItem: SubComplexDataFieldType
      @resolveTo(
        requiredSelectionSet: "{ id }"
        sourceName: "Subgraph1"
        sourceTypeName: "Query"
        sourceFieldName: "_encapsulated_Subgraph1_complexData"
        sourceArgs: { id: "{root.id}" }
        result: "items[0]"
      )
  }

Such resolver causes TypeError: Cannot read properties of undefined (reading 'type') here.
When @resolveTo directive includes sourceSelectionSet as in the following example:

  extend type TargetType {
    complexDataItem: SubComplexDataFieldType
      @resolveTo(
        requiredSelectionSet: "{ id }"
        sourceName: "Subgraph1"
        sourceTypeName: "Query"
        sourceFieldName: "_encapsulated_Subgraph1_complexData"
        sourceSelectionSet: "{ items { key value } }"   # sourceSelectionSet provided
        sourceArgs: { id: "{root.id}" }
        result: "items[0]"
      )
  }

It works as expected. However, in real life, it is hard to specify contents of sourceSelectionSet, it should derived from type according to path in result and runtime query provided selection set. When encapsulation is not applied to subgraphs, @resolveTo able to derive selection set from result without explicitly specifying sourceSelectionSet.

To Reproduce Steps to reproduce the behavior:

$ git clone https://github.com/muradm/resolve-to-result-mangling-test.git
$ cd resolve-to-result-mangling-test
$ npm run build
$ npm run start

Navigate to http://localhost:5000/graphql. In graphiql run the query:

query {
  Subgraph2 {
    targetQuery {
      targets {
        id
        complexDataItem {
          key
          value
        }
      }
    }
  }
}

In console output of gateway you get:

[2]   message: "Cannot read properties of undefined (reading 'type')"
[2]   locations: [
[2]     {
[2]       line: 1
[2]       column: 35
[2]     }
[2]   ]
[2]   path: [
[2]     "Subgraph2"
[2]     "targetQuery"
[2]     "targets"
[2]     2
[2]     "complexDataItem"
[2]   ]

Expected behavior

Expecting the following query result:

{
  "data": {
    "Subgraph2": {
      "targetQuery": {
        "targets": [
          {
            "id": "nullish",
            "complexDataItem": null
          },
          {
            "id": "empty",
            "complexDataItem": null
          },
          {
            "id": "full",
            "complexDataItem": {
              "key": "key1",
              "value": "value1"
            }
          }
        ]
      }
    }
  }
}

Environment:

  • OS: Linux
  • "@graphql-mesh/compose-cli": "^1.5.30"
  • "@graphql-hive/gateway-runtime": "^2.8.7"
  • "@graphql-mesh/transport-http": "^1.0.23"
  • NodeJS: v24.14.1

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions