Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singletons #7863

Merged
merged 2 commits into from
Sep 7, 2022
Merged

Singletons #7863

merged 2 commits into from
Sep 7, 2022

Conversation

emmatown
Copy link
Member

@emmatown emmatown commented Aug 30, 2022

Adds a new singleton list function for configuring lists as singletons.
update: See #7941, we now prefer isSingleton: true, not a function

See below for an example of configuring a singleton:

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

 const lists = {
  Settings: singleton({
    access: allowAll,
    fields: {
      websiteName: text(),
      copyrightText: text(),
      highlightedPosts: relationship({ ref: 'Post', many: true }),
    },
  }),
  Post: list({
    access: allowAll,
    fields: {
      title: text({ validation: { isRequired: true } }),
      content: text(),
    },
  }),
  ...
};

Setting this flag does a few things:

In Graphql

Graphql queries for singletons are all the same as those for lists, however with two major differences:

  • Graphql will error if you ever make a query that doesn't have the where: { id: '1' }, you will get an error - you can only read, create, update, or delete an item with an ID of 1 in singleton.
  • Upside, we are defaulting the where query for singletons, meaning you can simply not provide it, so you don't need to think about the above

So for example, the query for the above singleton would be:

query {
  settings {
    copyrightText
  }
}

NB: This is a query for a single settings

In the admin UI

  • Singletons do not display a count or an add button.
  • Clicking on a singleton's dashboard card or dashboard nav link will take you to the single singleton item
  • If the singleton doesn't exist when you visit its page, you get a create button, and a message {ThisSingleton} doesn't exist or you don't have access to it.

In Keystone

  • You cannot have a relationship to a singleton. This is done because, since there is only ever one, there's very little reason to ever have a relationship to a singleton, and allowing it added some edge-cases that were easier just to simply not to support

DX Design Notes

If the plural of a singleton is ambiguous, singleton lists default to the plural Many${ListName}.
This should typically be unambiguous.

We have retained the many query support for singletons to maximize compatibility and keep support for generic functions across lists that may or may not be singletons.

@changeset-bot

This comment was marked as resolved.

@vercel
Copy link

vercel bot commented Aug 30, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
keystone-next-docs ❌ Failed (Inspect) Sep 7, 2022 at 5:47AM (UTC)

@vercel vercel bot temporarily deployed to Preview August 30, 2022 05:24 Inactive
@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 30, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 7050783:

Sandbox Source
@keystone-6/sandbox Configuration

@emmatown emmatown force-pushed the singletons-as-lists branch from fd17e6b to b483800 Compare August 30, 2022 05:51
@vercel vercel bot temporarily deployed to Preview August 30, 2022 05:57 Inactive
@emmatown emmatown force-pushed the singletons-as-lists branch from b483800 to eddbb52 Compare August 30, 2022 06:15
@vercel vercel bot temporarily deployed to Preview August 30, 2022 06:17 Inactive
@emmatown emmatown force-pushed the singletons-as-lists branch from eddbb52 to 11588af Compare August 30, 2022 06:28
@vercel vercel bot temporarily deployed to Preview August 30, 2022 06:30 Inactive
@emmatown emmatown force-pushed the singletons-as-lists branch from 11588af to 4e80266 Compare August 30, 2022 06:47
@vercel vercel bot temporarily deployed to Preview August 30, 2022 06:49 Inactive
@emmatown emmatown force-pushed the singletons-as-lists branch from 4e80266 to e602af2 Compare August 30, 2022 06:55
@vercel vercel bot temporarily deployed to Preview August 30, 2022 06:58 Inactive
@emmatown emmatown force-pushed the singletons-as-lists branch from e602af2 to 075abb5 Compare August 30, 2022 07:12
@vercel vercel bot temporarily deployed to Preview August 30, 2022 07:15 Inactive
@vercel vercel bot temporarily deployed to Preview August 30, 2022 13:15 Inactive
@Noviny Noviny force-pushed the singletons-as-lists branch from 1f4aca0 to 1d77442 Compare September 6, 2022 00:27
@vercel vercel bot temporarily deployed to Preview September 6, 2022 00:30 Inactive
@vercel vercel bot temporarily deployed to Preview September 6, 2022 01:54 Inactive
@vercel vercel bot temporarily deployed to Preview September 6, 2022 06:07 Inactive
examples/testing/schema.ts Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview September 6, 2022 07:17 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 01:03 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 01:04 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 01:08 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 01:16 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 02:25 Inactive
@Noviny Noviny changed the title Singletons as lists Singletons implementation Sep 7, 2022
@Noviny Noviny marked this pull request as ready for review September 7, 2022 03:00
@Noviny Noviny mentioned this pull request Sep 7, 2022
3 tasks
@vercel vercel bot temporarily deployed to Preview September 7, 2022 03:01 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 03:14 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 03:41 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 03:46 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 03:49 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 04:00 Inactive
@dcousens dcousens force-pushed the singletons-as-lists branch from 6cf6ef1 to 60c9429 Compare September 7, 2022 04:07
@vercel vercel bot temporarily deployed to Preview September 7, 2022 04:13 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 05:16 Inactive
@vercel vercel bot temporarily deployed to Preview September 7, 2022 05:21 Inactive
@dcousens dcousens enabled auto-merge (squash) September 7, 2022 05:23
@dcousens dcousens changed the title Singletons implementation Singletons Sep 7, 2022
@dcousens dcousens disabled auto-merge September 7, 2022 05:24
@dcousens dcousens mentioned this pull request Sep 7, 2022
@dcousens dcousens force-pushed the singletons-as-lists branch from 4b21dfb to 8958469 Compare September 7, 2022 05:35
@vercel vercel bot temporarily deployed to Preview September 7, 2022 05:38 Inactive
@dcousens dcousens force-pushed the singletons-as-lists branch from 8958469 to 7050783 Compare September 7, 2022 05:41
@vercel vercel bot temporarily deployed to Preview September 7, 2022 05:47 Inactive
@dcousens dcousens merged commit 524431b into main Sep 7, 2022
@dcousens dcousens deleted the singletons-as-lists branch September 7, 2022 05:58
@keystonejs keystonejs deleted a comment from emmatown Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants