Files
payload/packages/graphql/src/schema/fieldToWhereInputSchemaMap.ts
Sasha c08c7071ee fix(graphql): population of joins that target relationship fields that have relationTo as an array (#12289)
Fixes population of joins that target relationship fields that have
`relationTo` as an array, for example:
```ts
// Posts collection
{
  name: 'polymorphic',
  type: 'relationship',
  relationTo: ['categories', 'users'],
},
// Categories collection
{
  name: 'polymorphic',
  type: 'join',
  collection: 'posts',
  on: 'polymorphic',
}
```

Thanks @jaycetde for the integration test
https://github.com/payloadcms/payload/pull/12278!

---------

Co-authored-by: Jayce Pulsipher <jpulsipher@nav.com>
2025-05-01 14:04:42 -04:00

165 lines
4.3 KiB
TypeScript

import type {
ArrayField,
CheckboxField,
CodeField,
CollapsibleField,
DateField,
EmailField,
GroupField,
JSONField,
NumberField,
PointField,
RadioField,
RelationshipField,
RichTextField,
RowField,
SelectField,
TabsField,
TextareaField,
TextField,
UploadField,
} from 'payload'
import { GraphQLEnumType, GraphQLInputObjectType } from 'graphql'
import { GraphQLJSON } from '../packages/graphql-type-json/index.js'
import { combineParentName } from '../utilities/combineParentName.js'
import { formatName } from '../utilities/formatName.js'
import { recursivelyBuildNestedPaths } from './recursivelyBuildNestedPaths.js'
import { withOperators } from './withOperators.js'
type Args = {
collectionSlug?: string
nestedFieldName?: string
parentName: string
}
export const fieldToSchemaMap = ({ nestedFieldName, parentName }: Args): any => ({
array: (field: ArrayField) =>
recursivelyBuildNestedPaths({
field,
nestedFieldName2: nestedFieldName,
parentName,
}),
checkbox: (field: CheckboxField) => ({
type: withOperators(field, parentName),
}),
code: (field: CodeField) => ({
type: withOperators(field, parentName),
}),
collapsible: (field: CollapsibleField) =>
recursivelyBuildNestedPaths({
field,
nestedFieldName2: nestedFieldName,
parentName,
}),
date: (field: DateField) => ({
type: withOperators(field, parentName),
}),
email: (field: EmailField) => ({
type: withOperators(field, parentName),
}),
group: (field: GroupField) =>
recursivelyBuildNestedPaths({
field,
nestedFieldName2: nestedFieldName,
parentName,
}),
json: (field: JSONField) => ({
type: withOperators(field, parentName),
}),
number: (field: NumberField) => ({
type: withOperators(field, parentName),
}),
point: (field: PointField) => ({
type: withOperators(field, parentName),
}),
radio: (field: RadioField) => ({
type: withOperators(field, parentName),
}),
relationship: (field: RelationshipField) => {
if (Array.isArray(field.relationTo)) {
return {
type: new GraphQLInputObjectType({
name: `${combineParentName(parentName, field.name)}_Relation`,
fields: {
relationTo: {
type: new GraphQLEnumType({
name: `${combineParentName(parentName, field.name)}_Relation_RelationTo`,
values: field.relationTo.reduce(
(values, relation) => ({
...values,
[formatName(relation)]: {
value: relation,
},
}),
{},
),
}),
},
value: { type: GraphQLJSON },
},
}),
}
}
return {
type: withOperators(field, parentName),
}
},
richText: (field: RichTextField) => ({
type: withOperators(field, parentName),
}),
row: (field: RowField) =>
recursivelyBuildNestedPaths({
field,
nestedFieldName2: nestedFieldName,
parentName,
}),
select: (field: SelectField) => ({
type: withOperators(field, parentName),
}),
tabs: (field: TabsField) =>
recursivelyBuildNestedPaths({
field,
nestedFieldName2: nestedFieldName,
parentName,
}),
text: (field: TextField) => ({
type: withOperators(field, parentName),
}),
textarea: (field: TextareaField) => ({
type: withOperators(field, parentName),
}),
upload: (field: UploadField) => {
if (Array.isArray(field.relationTo)) {
return {
type: new GraphQLInputObjectType({
name: `${combineParentName(parentName, field.name)}_Relation`,
fields: {
relationTo: {
type: new GraphQLEnumType({
name: `${combineParentName(parentName, field.name)}_Relation_RelationTo`,
values: field.relationTo.reduce(
(values, relation) => ({
...values,
[formatName(relation)]: {
value: relation,
},
}),
{},
),
}),
},
value: { type: GraphQLJSON },
},
}),
}
}
return {
type: withOperators(field, parentName),
}
},
})