keystonejs / keystone

The superpowered headless CMS for Node.js — built with GraphQL and React
https://keystonejs.com
MIT License
9.18k stars 1.15k forks source link

Everything is nullable in the schema #9062

Closed Wiz1991 closed 5 months ago

Wiz1991 commented 6 months ago

Pretty much most of the schema is generated in such a way that it becomes a minefield of nullable fields because no ! is aded anywhere. Lets take a simple example

Create two lists which are connected:

const Novel = list({
  access: allowAll,
  fields: {
    title: text({ validation: { isRequired: true } }),
    originalTitle: text({ validation: { isRequired: false } }),
    description: document({
      formatting: true,
      layouts: [
        [1, 1],
        [1, 1, 1],
        [2, 1],
        [1, 2],
        [1, 2, 1]
      ],
      links: true,
      dividers: true
    }),
  }
});

export const Featured = list({
  isSingleton: true,
  access: allowAll,
  ui: {
    description: 'Configure which novels are featured on the homepage. '
  },
  fields: {
    novels: relationship({
      ref: 'Novel',
      many: true
    })
  }
});

For the above, the following schema should be generated:

type Novel {
  id: ID!
  title: String!
  originalTitle: String
  description: Novel_description_Document
}

type Featured {
  id: ID!
  novels(where: NovelWhereInput! = {}, orderBy: [NovelOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: NovelWhereUniqueInput): [Novel!]!
  novelsCount(where: NovelWhereInput! = {}): Int
}

but instead the following is generated:

type Novel {
  id: ID!
  title: String
  originalTitle: String
  description: Novel_description_Document
}

type Featured {
  id: ID!
  novels(where: NovelWhereInput! = {}, orderBy: [NovelOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: NovelWhereUniqueInput): [Novel!]
  novelsCount(where: NovelWhereInput! = {}): Int
}

This is also the same for relationship fields, many relationships should create [Novel!]!, not [Novel!]. This means that the field (in this case Novel) cannot return null and that it must resolve to an array and that none of the individuals items inside that array can be null. This makes it much easier to query, as when there is no relation it just returns []

iamandrewluca commented 5 months ago

See https://keystonejs.com/docs/fields/overview#common-configuration See graphql.isNonNull.read