Long awaited Singleton has arrived
Singleton is one of the most popular community requested features and it is an easy way to work with lists that will only ever have one item like a website configuration or user settings.
You describe the list (Eg. WebsiteConfiguration) like you describe any other list and add the property isSingleton: true
to it.
// schema.tsexport default config({lists: ({WebsiteConfiguration: list({...isSingleton: true,fields: {websiteName: text(),copyrightText: text(),}}),}),});
In GraphQL, you can read and write from this list without needing an identifier.
query {websiteConfiguration {websiteNamecopyrightText}}mutation createNew {createWebsiteConfiguration(data: { websiteName: "cool website", copyrightText: "do not copy me" }) {websiteNamecopyrightText}}mutation updateExisting {updateWebsiteConfiguration(data: { websiteName: "cool website", copyrightText: "do not copy me" }) {websiteNamecopyrightText}}
Within the server environment, you can read and write from this list using the default identifier 1
.
// readcontext.query.WebsiteConfiguration.findOne({ where: { id: '1' } });// updatecontext.query.WebsiteConfiguration.updateOne({where: {id: '1',},data: {websiteName: 'name to update',copyrightText: 'text to update',},});
Read our API docs or refer to our Singleton example to learn more about Singleton.
If you like using Keystone, we'd appreciate a shout out in Twitter and a star in GitHub.