Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLoader in batch-delegate uses a non-unique cacheKey #5645

Open
4 tasks
briankrug opened this issue Oct 18, 2023 · 1 comment
Open
4 tasks

getLoader in batch-delegate uses a non-unique cacheKey #5645

briankrug opened this issue Oct 18, 2023 · 1 comment

Comments

@briankrug
Copy link

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
Copy link
Author

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);
     }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant