graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

getLoader in batch-delegate uses a non-unique cacheKey

Open briankrug opened this issue 2 years ago • 1 comments
trafficstars

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox

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

    Please make sure the GraphQL Tools 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

At https://github.com/ardatan/graphql-tools/blob/master/packages/batch-delegate/src/getLoader.ts#L88 the fieldName seems to always be "_entities" whereas the info.fieldName seems to be the field of the extended type. Thus, the cacheKey is not unique when the selection set matches on different object types! And the wrong loader is then used which results in null values for those entities. In my opinion, the cacheKey should include the Parent Type Name (info.parentType.name) and the Info Field name (info.fieldName) and retain the concatenation of the selection set.

[I'm at a loss to understand why the fieldName variables is not equal to info.fieldName despite it being seemingly initiatize to it, but my debugger shows it is different (and the resultant behavior confirms it).]

To Reproduce Steps to reproduce the behavior:

My GraphQL query looks like:

query pq {
  problemQuery {
    account {
      id
      name
    }
   topic {
      name
      environment {
        id
        name
      }
      count
    }
    total
  }
}

There are 3 sub-schemas stitched together:

Schema 1

type Problem {
  account: Account!
  topic: Topic!
  total: Int
}

type Account @key(fields : "id") @extends {
  id: ID! @external
}

type Environment @key(fields : "id") @extends {
  id: ID!
}

type Topic @key(fields : "name environment { id }") @extends {
  name: ID!
  environment: Environment!
}

Schema 2

Topic @key(fields : "name environment { id }") {
  name: ID!
  environment: Environment!
  count: Int
}

_Schema 3_

type Account @key(fields : "id") { id: ID! name: String }

type Environment @key(fields : "id") { id: ID! name: String }


**Expected behavior**

The query should return with all the entities populated

**Environment:**

- `@graphql-tools/stitch`: 9.0.3
- NodeJS: v20.5.1

**Additional context**


briankrug avatar Oct 18 '23 02:10 briankrug

Recommended patch:

diff --git a/node_modules/@graphql-tools/batch-delegate/cjs/getLoader.js b/node_modules/@graphql-tools/batch-delegate/cjs/getLoader.js
index 7af799b..040d05d 100644
--- a/node_modules/@graphql-tools/batch-delegate/cjs/getLoader.js
+++ b/node_modules/@graphql-tools/batch-delegate/cjs/getLoader.js
@@ -56,7 +56,7 @@ function defaultCacheKeyFn(key) {
 function getLoader(options) {
     const { schema, context, info, fieldName = info.fieldName, dataLoaderOptions, fieldNodes = (0, delegate_1.getActualFieldNodes)(info.fieldNodes[0]), selectionSet = fieldNodes[0].selectionSet, } = options;
     const loaders = getLoadersMap(context ?? GLOBAL_CONTEXT, schema);
-    let cacheKey = fieldName;
+    let cacheKey = info.parentType.name.concat(".", info.fieldName);
     if (selectionSet != null) {
         cacheKey += memoizedPrint(selectionSet);
     }

briankrug avatar Oct 18 '23 02:10 briankrug