Skip to content

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

@denisyilmaz

Description

@denisyilmaz

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions