Keystone 6 js: checkbox null values

153 views Asked by At

Keystone js 6 does not allow me to add checkbox field, that looks the following way

// checkbox_field: checkbox({ label: 'Checkbox Field', access: { read: ()=> true, update: ()=> true } }),

I assume it's because I have some null values in db, since I had a the same problem with text type field, which was easily solved using db.isNullable === true. However, I can't find a similar option for checkbox field type.

I tried using defaultValue option and db.map, but it didn't help

1

There are 1 answers

2
Molomby On BEST ANSWER

In what way does Keystone "not allow" this? Do you get an error? It's difficult to help with a problem without concrete information about what's happening.

I've added the field config you supplied to my local copy of the task manager example without issue.

import { list } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields';
import { select } from '@keystone-6/core/fields';

export const lists = {
  Task: list({
    access: allowAll,
    fields: {
      label: text({ validation: { isRequired: true } }),

      // Test field ⬇️
      checkbox_field: checkbox({
        label: 'Checkbox Field',
        access: { read: () => true, update: () => true }
      }),
      
      // .. other fields
    },
  }),
  .. other lists
};

I've set db.useMigrations to true so when I restart the app, I'm prompted to create and name a migration that will add the column. The resultant SQL is below. This is for PostgreSQL but it should be for the other DB providers.

-- AlterTable
ALTER TABLE "Task" ADD COLUMN     "checkbox_field" BOOLEAN NOT NULL DEFAULT false;

This will add a non-nullable column to the DB with the default value applied to any existing records (so there shouldn't be anything "blocking" this migration from applying).

For simplicity's sake the Keystone Checkbox field type doesn't support the DB field itself being nullable though it does give you some options to control the nullability of the GraphQL types, ie: graphql.read.isNonNull and graphql.create.isNonNull. This allows for use cases where a value may be returned as null due to access control, or passed in as null for a create but saved with a value due to a default value or hook being configured.