# @keystone-alpha/keystone

> The main @keystone-alpha class & CLI. This is where the magic happens.

Latest version **16.1.0** (published 2019-10-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @keystone-alpha/keystone
pnpm add @keystone-alpha/keystone
yarn add @keystone-alpha/keystone
bun add @keystone-alpha/keystone
```

Provides the command `keystone`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 16.1.0 |
| Published | 2019-10-23 |
| First published | 2019-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.4.0 |
| Dependencies | 31 |
| Unpacked size | 168 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | The KeystoneJS Development Team |
| Maintainers | dominikwilkowski, jedwatson, jesstelford, mitchellhamilton, molomby, timl |

## Links

- npm: https://www.npmjs.com/package/@keystone-alpha/keystone
- npm.io page: https://npm.io/package/@keystone-alpha/keystone

## Dependencies (31)

- [arg](https://npm.io/package/arg.md) ^4.1.1
- [ora](https://npm.io/package/ora.md) ^3.4.0
- [cors](https://npm.io/package/cors.md) ^2.8.4
- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [endent](https://npm.io/package/endent.md) ^1.3.0
- [falsey](https://npm.io/package/falsey.md) ^1.0.0
- [globby](https://npm.io/package/globby.md) ^9.1.0
- [ci-info](https://npm.io/package/ci-info.md) ^2.0.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [graphql](https://npm.io/package/graphql.md) ^14.4.2
- [dev-null](https://npm.io/package/dev-null.md) ^0.1.1
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.0
- [passport](https://npm.io/package/passport.md) ^0.4.0
- [pluralize](https://npm.io/package/pluralize.md) ^7.0.0
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.10.1
- [p-waterfall](https://npm.io/package/p-waterfall.md) ^2.1.0
- [fast-memoize](https://npm.io/package/fast-memoize.md) ^2.4.0
- [apollo-errors](https://npm.io/package/apollo-errors.md) ^1.9.0
- [terminal-link](https://npm.io/package/terminal-link.md) ^1.3.0
- [passport-twitter](https://npm.io/package/passport-twitter.md) ^1.0.4
- [graphql-type-json](https://npm.io/package/graphql-type-json.md) ^0.2.1
- [passport-facebook](https://npm.io/package/passport-facebook.md) ^3.0.0
- [lodash.flattendeep](https://npm.io/package/lodash.flattendeep.md) ^4.4.0
- [express-pino-logger](https://npm.io/package/express-pino-logger.md) ^4.0.0
- [@keystone-alpha/utils](https://npm.io/package/@keystone-alpha/utils.md) ^3.2.0
- [@keystone-alpha/fields](https://npm.io/package/@keystone-alpha/fields.md) ^15.0.0
- [@keystone-alpha/logger](https://npm.io/package/@keystone-alpha/logger.md) ^2.0.1
- [@keystone-alpha/session](https://npm.io/package/@keystone-alpha/session.md) ^3.0.2
- [@keystone-alpha/app-graphql](https://npm.io/package/@keystone-alpha/app-graphql.md) ^8.2.1
- [@keystone-alpha/access-control](https://npm.io/package/@keystone-alpha/access-control.md) ^3.1.0
- [@keystone-alpha/build-field-types](https://npm.io/package/@keystone-alpha/build-field-types.md) ^1.0.6

## Recent versions

- 16.1.0 (latest) — 2019-10-23
- 16.0.1 — 2019-10-16
- 16.0.0 — 2019-10-15
- 15.3.2 — 2019-10-09
- 15.1.3 — 2019-10-09
- 15.0.1 — 2019-10-09
- 14.0.1 — 2019-10-09
- 15.4.1 — 2019-10-09
- 15.2.2 — 2019-10-09
- 15.4.0 — 2019-10-09
- 15.3.1 — 2019-10-02
- 15.3.0 — 2019-09-27
- 15.2.1 — 2019-09-23
- 15.2.0 — 2019-09-19
- 15.1.2 — 2019-09-18
- … 34 more at https://npm.io/package/@keystone-alpha/keystone/versions

## README

<!--[meta]
section: api
title: Keystone
order: 1
[meta]-->

# keystone

## Constructor

### Usage

```javascript
const { Keystone } = require('@keystone-alpha/keystone');

const keystone = new Keystone({
  /*...config */
});
```

### Config

| Option                  | Type       | Default    | Description                                                                                                                                       |
| ----------------------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`                  | `String`   | `null`     | The name of the project. Appears in the Admin UI.                                                                                                 |
| `adapter`               | `Object`   | Required   | The database storage adapter. See the [Adapter Framework](https://keystonejs.com/keystone-alpha/keystone/lib/adapters/) page for more details. |
| `adapters`              | `Array`    | `[]`       |                                                                                                                                                   |
| `defaultAdapter`        | `Object`   | `null`     |                                                                                                                                                   |
| `defaultAccess`         | `Object`   | `{}`       |                                                                                                                                                   |
| `onConnect`             | `Function` | `null`     |                                                                                                                                                   |
| `cookieSecret`          | `String`   | `qwerty`   |                                                                                                                                                   |
| `cookieMaxAge`          | `Int`      | 30 days    |                                                                                                                                                   |
| `secureCookies`         | `Boolean`  | Variable   | Defaults to true in production mode, false otherwise.                                                                                             |
| `sessionStore`          | `Object`   | `null`     |                                                                                                                                                   |
| `schemaNames`           | `Array`    | `[public]` |                                                                                                                                                   |
| `queryLimits`           | `Object`   | `{}`       | Configures global query limits                                                                                                                    |

### `queryLimits`

Configures global query limits.

These should be used together with [list query limits](https://keystonejs.com/api/create-list#query-limits).

#### Usage

```javascript
const keystone = new Keystone({
  /* ...config */
  queryLimits: {
    maxTotalResults: 1000,
  },
});
```

- `maxTotalResults`: limit of the total results of all relationship subqueries

Note that `maxTotalResults` applies to the total results of all relationship queries separately, even if some are nested inside others.

## Methods

| Method                | Description                                                                  |
| --------------------- | ---------------------------------------------------------------------------- |
| `createList`          | Add a list to the `Keystone` schema.                                         |
| `extendGraphQLSchema` | Extend keystones generated schema with custom types, queries, and mutations. |
| `connect`             | Manually connect to Adapters.                                                |
| `prepare`             | Manually prepare `Keystone` middlewares.                                     |
| `createItems`         | Add items to a `Keystone` list.                                              |
| `disconnect`          | Disconnect from all adapters.                                                |
| `executeQuery`        | Run GraphQL queries and mutations directly against a `Keystone` instance.    |

<!--

## Super secret methods

Hello curious user. Here are some undocumented methods you _can_ use.
Please note: We use these internally but provide no support or assurance if used in your projects.

| Method                | Description                                                                  |
| --------------------- | ---------------------------------------------------------------------------- |
| `dumpSchema`          | Dump schema to a file.                                                       |
| `getTypeDefs`         | Remove from user documentation?                                              |
| `registerSchema`      | Remove from user documentation?                                              |
| `getAdminSchema`      | Remove from user documentation?                                              |
| `getAccessContext`    | Remove from user documentation?                                              |
| `createItem`          | Remove from user documentation?                                              |
| `getAdminMeta`        | Remove from user documentation?                                              |

-->

## createList(listKey, config)

### Usage

```javascript
keystone.createList('Posts', {
  /*...config */
});
```

### Config

Registers a new list with KeystoneJS and returns a `Keystone` list object.

| Option    | Type     | Default | Description                                                                                                 |
| --------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------- |
| `listKey` | `String` | `null`  | The name of the list. This should be singular, E.g. 'User' not 'Users'.                                     |
| `config`  | `Object` | `{}`    | The list config. See the [createList API](https://keystonejs.com/api/create-list) page for more details. |

## extendGraphQLSchema(config)

Extends keystones generated schema with custom types, queries, and mutations.

### Usage

```javascript
keystone.extendGraphQLSchema({
  types: ['type FooBar { foo: Int, bar: Float }'],
  queries: [
    {
      schema: 'double(x: Int): Int',
      resolver: (_, { x }) => 2 * x,
    },
  ],
  mutations: [
    {
      schema: 'double(x: Int): Int',
      resolver: (_, { x }) => 2 * x,
    },
  ],
});
```

### Config

| Option    | Type    | Description                                         |
| --------- | ------- | --------------------------------------------------- |
| types     | `array` | A list of strings defining graphQL types.           |
| queries   | `array` | A list of objects of the form { schema, resolver }. |
| mutations | `array` | A list of objects of the form { schema, resolver }. |

The `schema` for both queries and mutations should be a string defining the graphQL schema element for the query/mutation, e.g.

```javascript
{
  schema: 'getBestPosts(author: ID!): [Post]';
}
```

The `resolver` for both queries and mutations should be a resolver function with the signature `(obj, args, context, info)`. See the [Apollo docs](https://www.apollographql.com/docs/graphql-tools/resolvers/#resolver-function-signature) for more details.

## createItems(items)

Allows bulk creation of items. This method's primary use is intended for migration scripts, or initial seeding of databases.

### Usage

```javascript
keystone.createItems({
  User: [{ name: 'Ticiana' }, { name: 'Lauren' }],
  Post: [
    {
      title: 'Hello World',
      author: { where: { name: 'Ticiana' } },
    },
  ],
});
```

The `author` field of the `Post` list would have the following configuration:

```javascript
keystone.createList('Post', {
  fields: {
    author: { type: Relationship, ref: 'User' },
  },
});
```

### Config

| Option      | Type     | Description                                                                     |
| ----------- | -------- | ------------------------------------------------------------------------------- |
| `[listKey]` | `Object` | An object where keys are list keys, and values are an array of items to insert. |

_Note_: The format of the data must match the lists and fields setup with `keystone.createList()`

It is possible to create relationships at insertion using the KeystoneJS query syntax.

E.g. `author: { where: { name: 'Ticiana' } }`

Upon insertion, KeystoneJS will resolve the `{ where: { name: 'Ticiana' } }` query
against the `User` list, ultimately setting the `author` field to the ID of the
_first_ `User` that is found.

Note an error is thrown if no items match the query.

## prepare(config)

Manually prepare middlewares. Returns a promise representing the processed middlewares. They are available as an array through the `middlewares` property of the returned object.

### Usage

```javascript
const { middlewares } = await keystone.prepare({
  apps,
  dev: process.env.NODE_ENV !== 'production',
});
```

### Config

| Option    | Type      | default | Description                                          |
| --------- | --------- | ------- | ---------------------------------------------------- |
| `dev`     | `Boolean` | `false` | Sets the dev flag in KeystoneJS' express middleware. |
| `apps`    | `Array`   | `[]`    | An array of 'Apps' which are express middleware.     |
| `distDir` | `String`  | `dist`  | The build directory for keystone.                    |

## connect()

Manually connect KeystoneJS to the adapters.

### Usage

```javascript
keystone.connect();
```

_Note_: `keystone.connect()` is only required for custom servers. Most example projects use the `keystone start` command to start a server and automatically connect.

See: [Custom Server](https://keystonejs.com/guides/custom-server).

## disconnect()

Disconnect all adapters.

## executeQuery(queryString, config)

Use this method to execute queries or mutations directly against a `Keystone` instance.

**Note:** When querying or mutating via `keystone.executeQuery`, there are differences to keep in mind:

- No access control checks are run (everything is set to `() => true`)
- The `context.req` object is set to `{}` (you can override this if necessary,
  see options below)
- Attempting to authenticate will throw errors (due to `req` being mocked)

Returns a Promise representing the result of the given query or mutation.

### Usage

```javascript
keystone.executeQuery(queryString, config);
```

### queryString

A graphQL query string. For example:

```graphql
query {
  allTodos {
    id
    name
  }
}
```

Can also be a mutation:

```graphql
mutation newTodo($name: String) {
  createTodo(name: $name) {
    id
  }
}
```

### Config

| Option      | Type     | Default | Description                                                                                                               |
| ----------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| `variables` | `Object` | `{}`    | The variables passed to the graphql query for the given queryString.                                                      |
| `context`   | `Object` | `{}`    | Override the default `context` object passed to the GraphQL engine. Useful for adding a `req` or setting the `schemaName` |

---
_Source: https://npm.io/package/@keystone-alpha/keystone · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
