payload icon indicating copy to clipboard operation
payload copied to clipboard

Required array fields are nullable in GraphQL

Open reiv opened this issue 1 year ago • 1 comments

Link to reproduction

No response

Describe the Bug

Fields with required: true defined in an array aren't marked as non-nullable in the resulting GraphQL schema.

To Reproduce

Given this collection config:

import {CollectionConfig} from 'payload/types';

export const Foo: CollectionConfig = {
  slug: 'foo',
  fields: [
    {
      type: 'array',
      name: 'bar',
      required: true,
      fields: [
        {
          type: 'text',
          name: 'baz',
          required: true,
        },
      ],
    },
    {
      type: 'text',
      name: 'quux',
      required: true,
    },
  ],
};

Running payload generate:graphQLSchema results in this schema (excerpt):

type Foo {
  id: String
  bar: [Foo_Bar!]!
  quux: String!
  updatedAt: DateTime
  createdAt: DateTime
}

type Foo_Bar {
  baz: String
  id: String
}

I'd expect baz to be marked as non-nullable in the same way that quux is, unless there is a reason for arrays to behave differently?

Payload Version

2.7.0

Adapters and Plugins

No response

reiv avatar Jan 11 '24 14:01 reiv

Hey @reiv there are a variety of reasons why something might be nullable even if it's required:

  • if the field is localized (other locales might not have it)
  • if the field has access control (you might not have access to the field)
  • if the field has conditional logic applied to it (might be invisible / not required if the condition does not pass)

I don't see that any of these apply to your code snippet, so we will try and reproduce to see what we can come up with, because I agree that the code snippet you provided does make me think that the field should be non-nullable.

But thought I'd note this down in the meantime while we reproduce.

Thanks for bringing this up!

jmikrut avatar Jan 11 '24 14:01 jmikrut

This issue was automatically closed due to lack of activity. If this issue is still relevant against the latest codebase, please create a new issue.

github-actions[bot] avatar Dec 06 '24 15:12 github-actions[bot]

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Dec 08 '24 04:12 github-actions[bot]