payloadcms / payloadcms/payload

relationship array order corrupted in GraphQL query when multiple fields use same relationshipo

Open
#14,956 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: graphql Bug db: postgres stale status: needs-triage
Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

Describe the Bug

When querying a collection with both a single upload/relationship field and a hasMany upload/relationship array field pointing to the same collection, the array order is corrupted if the single field is queried before the array field. The order is correct if the array field comes first.

Link to the code that reproduces this issue

https://github.com/denisyilmaz/payload-relationship-bug

Reproduction Steps
  1. Create a post with:
    • A cover upload (single) = file-A
    • files = [file-A, file-B, file-C] (stored in that order)
    • A coverRelationship (single) = file-A
    • filesRelationship = [file-A, file-B, file-C] (stored in that order)
  2. Test the following queries:

✅ Correct Order (array field first):

query CorrectFilesOrder {
  Posts {
    docs {
      id
      files {
        id
      }
    }
  }
}

query HotfixFilesOrder {
  Posts {
    docs {
      id
      files {
        id
      }
      cover {
        id
      }
    }
  }
}

query CorrectFilesRelationshipOrder {
  Posts {
    docs {
      id
      filesRelationship {
        id
      }
    }
  }
}

query HotfixFilesRelationshipOrder {
  Posts {
    docs {
      id
      filesRelationship {
        id
      }
      coverRelationship {
        id
      }
    }
  }
}

❌ Wrong Order (single field first):

query WrongFilesOrder {
  Posts {
    docs {
      id
      cover {
        id
      }
      files {
        id
      }
    }
  }
}

query WrongFilesRelationshipOrder {
  Posts {
    docs {
      id
      coverRelationship {
        id
      }
      filesRelationship {
        id
      }
    }
  }
}

Result: files and filesRelationship arrays have corrupted order [file-B, file-C, file-A] (first item moved to end)

Expected Behavior

The files and filesRelationship array order should be identical regardless of:

  • Whether cover/coverRelationship is included in the query
  • The order of fields in the GraphQL query selection set

GraphQL field order should not affect the result.

Actual Behavior

When cover/coverRelationship is queried before files/filesRelationship, the array order is corrupted (first element moves to last position)

  • When files/filesRelationship is queried before cover/coverRelationship, the order is correct
  • This affects both upload type fields and relationship type fields

The stored document order is correct (verified via depth: 0 queries)

Which area(s) are affected?

db: postgres, area: graphql

Environment Info
Binaries:
  Node: 20.19.6
  npm: 10.8.2
  Yarn: N/A
  pnpm: 10.10.0
Relevant Packages:
  payload: 3.68.4
  next: 15.4.10
  @payloadcms/db-postgres: 3.68.4
  @payloadcms/drizzle: 3.68.4
  @payloadcms/email-nodemailer: 3.68.4
  @payloadcms/email-resend: 3.68.4
  @payloadcms/graphql: 3.68.4
  @payloadcms/next/utilities: 3.68.4
  @payloadcms/payload-cloud: 3.68.4
  @payloadcms/plugin-cloud-storage: 3.68.4
  @payloadcms/plugin-multi-tenant: 3.68.4
  @payloadcms/plugin-search: 3.68.4
  @payloadcms/plugin-stripe: 3.68.4
  @payloadcms/richtext-lexical: 3.68.4
  @payloadcms/storage-s3: 3.68.4
  @payloadcms/translations: 3.68.4
  @payloadcms/ui/shared: 3.68.4
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041
  Available memory (MB): 36864
  Available CPU cores: 14

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and compare the listed GraphQL queries, especially the single relationship field before the hasMany field, against the depth: 0 result. Trace the GraphQL relationship resolution path for Postgres and add coverage for both upload and relationship fields. Done means array order remains [file-A, file-B, file-C] regardless of selection order.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, postgres, typescript
Domain
api, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.