Skip to content

Commit 1a028cb

Browse files
r1tsuuDeepshekhar DasdeepshekhardasJarrodMFlesch
authored
fix(graphql): port virtual fields should be nullable in GraphQL schema (#17256)
Port of #16193 to `3.x` Co-authored-by: Deepshekhar Das <[email protected]> Co-authored-by: deepshekhardas <[email protected]> Co-authored-by: Jarrod Flesch <[email protected]>
1 parent dd19aae commit 1a028cb

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎packages/graphql/src/schema/withNullableType.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { GraphQLType } from 'graphql'
22
import type { FieldAffectingData } from 'payload'
33

44
import { GraphQLNonNull } from 'graphql'
5+
import { fieldIsVirtual } from 'payload/shared'
56

67
export const withNullableType = ({
78
type,
@@ -17,6 +18,9 @@ export const withNullableType = ({
1718
const hasReadAccessControl = field.access && field.access.read
1819
const condition = field.admin && field.admin.condition
1920
const isTimestamp = field.name === 'createdAt' || field.name === 'updatedAt'
21+
// Virtual fields are computed (by hooks or from a linked relationship), so they must
22+
// not be non-null in the GraphQL schema even when marked required.
23+
const isVirtual = fieldIsVirtual(field)
2024

2125
if (
2226
!forceNullable &&
@@ -25,7 +29,8 @@ export const withNullableType = ({
2529
(!field.localized || parentIsLocalized) &&
2630
!condition &&
2731
!hasReadAccessControl &&
28-
!isTimestamp
32+
!isTimestamp &&
33+
!isVirtual
2934
) {
3035
return new GraphQLNonNull(type)
3136
}

‎test/graphql/config.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ export default buildConfigWithDefaults({
3939
},
4040
],
4141
},
42+
{
43+
slug: 'virtual-fields',
44+
fields: [
45+
{
46+
type: 'relationship',
47+
relationTo: 'posts',
48+
name: 'post',
49+
},
50+
{
51+
// A required field that is NOT virtual - stays non-null in the GraphQL schema
52+
name: 'requiredTitle',
53+
type: 'text',
54+
required: true,
55+
},
56+
{
57+
// Required + virtual (computed by a hook) - must be nullable in the GraphQL schema
58+
name: 'virtualComputed',
59+
type: 'text',
60+
required: true,
61+
virtual: true,
62+
hooks: {
63+
afterRead: [({ data }) => `computed-${data?.requiredTitle ?? ''}`],
64+
},
65+
},
66+
{
67+
// Required + virtual linked to a relationship path - must also be nullable
68+
name: 'virtualFromRelation',
69+
type: 'text',
70+
required: true,
71+
virtual: 'post.title',
72+
},
73+
],
74+
versions: false,
75+
},
4276
],
4377
globals: [
4478
{

‎test/graphql/int.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type { GraphQLInputObjectType } from 'graphql'
12
import type { Payload } from 'payload'
23

4+
import { configToSchema } from '@payloadcms/graphql'
5+
import { GraphQLNonNull } from 'graphql'
36
import path from 'path'
47
import { fileURLToPath } from 'url'
58
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'
@@ -338,5 +341,17 @@ query {
338341

339342
expect(afterDelete.errors).toBeUndefined()
340343
})
344+
345+
describe('nullable schema types', () => {
346+
it('should not mark required virtual fields as non-null in the mutation input type', () => {
347+
const { schema } = configToSchema(payload.config)
348+
const inputType = schema.getType('mutationVirtualFieldInput') as GraphQLInputObjectType
349+
const fields = inputType.getFields()
350+
351+
expect(fields.requiredTitle!.type instanceof GraphQLNonNull).toBe(true)
352+
expect(fields.virtualComputed!.type instanceof GraphQLNonNull).toBe(false)
353+
expect(fields.virtualFromRelation!.type instanceof GraphQLNonNull).toBe(false)
354+
})
355+
})
341356
})
342357
})

0 commit comments

Comments
 (0)