payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.12k stars 1.44k forks source link

GraphQL: Internal Server Error occurs when querying with hasMany field and its data is filtered by access control #6518

Open ikenox opened 3 months ago

ikenox commented 3 months ago

Link to reproduction

https://github.com/ikenox/payload/commit/bd0674af229da5c717a5c469ade7001a2e7d9a9b

Describe the Bug

  1. Add hasMany relationship. For example, I prepared Posts collection and Tags collection (Posts has 0-to-many tags)
  2. The following query will cause 500 Error when some tags data is filtered by access control.

Query:

query {
  Posts {
    docs {
      text
      tags {
        name
      }
    }
  }
}

Result:

{
  "errors": [
    {
      "extensions": {
        "name": "Error",
        "statusCode": 500
      },
      "locations": [
        {
          "line": 6,
          "column": 17
        }
      ],
      "message": "Something went wrong.",
      "path": [
        "Posts",
        "docs",
        0,
        "tags",
        0
      ]
    }
  ],
  "data": {
    "Posts": {
      "docs": [
        {
          "text": "post1",
          "tags": null
        }
      ]
    }
  }
}

Detail of the bug

https://github.com/payloadcms/payload/blob/4c832ad/packages/payload/src/graphql/schema/buildObjectType.ts#L371-L397

At here,

So, for example, when result is null at i=0, the results array will be [null, data1, ...]. This results array is invalid because it contains null even though the graphql schema type of tags is an array of non-null tag type ([Tag!]) .

To Reproduce

I added a minimum reproducible test. pnpm test:int:postgres _community will fail. https://github.com/ikenox/payload/commit/bd0674af229da5c717a5c469ade7001a2e7d9a9b

Payload Version

2.18.3

Adapters and Plugins

db-postgres: 0.8.4

kendelljoseph commented 1 month ago

Hi @ikenox -- I'm looking into this now!

wkentdag commented 1 month ago

sounds similar to https://github.com/payloadcms/payload/issues/6800 which was fixed in 2.22.1. can you reproduce with a newer version of payload?

edit: whoops didn't read carefully, might be a similar but separate issue 😅

ikenox commented 1 month ago

@wkentdag Yes, I rebased my reproducible code on v2.24.1 but the problem still remains. https://github.com/ikenox/payload/commit/96172618e619ed3d116912a89c20da871441a1e2

kendelljoseph commented 1 month ago

Yep, we've addressed some other issues wherein arrays have null values in them. We're diggin' into it! :)