Skip to Page NavigationSkip to Page NavigationSkip to Content
Keystone 6 is now in General Availability!
Why KeystoneUpdatesDocumentation
Edit on GitHub

Query API

The Query API provides a programmatic API for running CRUD operations against your GraphQL API. For each list in your system the following API is available at context.query.<listName>.

{
findOne({ where: { id }, query }),
findMany({ where, take, skip, orderBy, query }),
count({ where }),
createOne({ data, query }),
createMany({ data, query }),
updateOne({ where: { id }, data, query }),
updateMany({ data, query }),
deleteOne({ where: { id }, query }),
deleteMany({ where, query }),
}

The arguments to these functions closely correspond to their equivalent GraphQL APIs, making it easy to switch between the programmatic API and the GraphQL API.

The query argument, which defaults to 'id' for all the functions, is a string which indicates which fields should be returned by the operation. Unless otherwise specified, the other arguments to all functions are required.

The functions in the API all work by directly executing queries and mutations against your GraphQL API.

findOne

const user = await context.query.User.findOne({
where: { id: '...' },
query: 'id name posts { id title }',
});

findMany

All arguments are optional.

const users = await context.query.User.findMany({
where: { name: { startsWith: 'A' } },
take: 10,
skip: 20,
orderBy: [{ name: 'asc' }],
query: 'id name posts { id title }',
});

count

All arguments are optional.

const count = await context.query.User.count({
where: { name: { startsWith: 'A' } },
});

createOne

const user = await context.query.User.createOne({
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
query: 'id name posts { id title }',
});

createMany

const users = await context.query.User.createMany({
data: [
{
name: 'Alice',
posts: { create: [{ title: 'Alices first post' }] },
},
{
name: 'Bob',
posts: { create: [{ title: 'Bobs first post' }] },
},
],
query: 'id name posts { id title }',
});

updateOne

const user = await context.query.User.updateOne({
where: { id: '...' },
data: {
name: 'Alice',
posts: { create: [{ title: 'My first post' }] },
},
query: 'id name posts { id title }',
});

updateMany

const users = await context.query.User.updateMany({
data: [
{
where: { id: '...' },
data: {
name: 'Alice',
posts: { create: [{ title: 'Alices first post' }] },
},
},
{
where: { id: '...' },
data: {
name: 'Bob',
posts: { create: [{ title: 'Bobs first post' }] },
},
},
],
query: 'id name posts { id title }',
});

deleteOne

const user = await context.query.User.deleteOne({
where: { id: '...' },
query: 'id name posts { id title }',
});

deleteMany

const users = await context.query.User.deleteMany({
where: [{ id: '...' }, { id: '...' }],
query: 'id name posts { id title }',
});

Context API Reference

The API for run-time functionality in your Keystone system. Use it to write business logic for access control, hooks, testing, GraphQL schema extensions, and more.

DB API Reference

The API for running CRUD operations against the internal GraphQL resolvers in your system. It returns internal item objects, which can be returned from GraphQL resolvers.

On this page

  • Query API
  • findOne
  • findMany
  • count
  • createOne
  • createMany
  • updateOne
  • updateMany
  • deleteOne
  • deleteMany
  • Related resources