16.1.0 • Published 5 years ago

@keystone-alpha/keystone v16.1.0

Weekly downloads
43
License
MIT
Repository
-
Last release
5 years ago

keystone

Constructor

Usage

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

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

Config

OptionTypeDefaultDescription
nameStringnullThe name of the project. Appears in the Admin UI.
adapterObjectRequiredThe database storage adapter. See the Adapter Framework page for more details.
adaptersArray[]
defaultAdapterObjectnull
defaultAccessObject{}
onConnectFunctionnull
cookieSecretStringqwerty
cookieMaxAgeInt30 days
secureCookiesBooleanVariableDefaults to true in production mode, false otherwise.
sessionStoreObjectnull
schemaNamesArray[public]
queryLimitsObject{}Configures global query limits

queryLimits

Configures global query limits.

These should be used together with list query limits.

Usage

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

MethodDescription
createListAdd a list to the Keystone schema.
extendGraphQLSchemaExtend keystones generated schema with custom types, queries, and mutations.
connectManually connect to Adapters.
prepareManually prepare Keystone middlewares.
createItemsAdd items to a Keystone list.
disconnectDisconnect from all adapters.
executeQueryRun GraphQL queries and mutations directly against a Keystone instance.

createList(listKey, config)

Usage

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

Config

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

OptionTypeDefaultDescription
listKeyStringnullThe name of the list. This should be singular, E.g. 'User' not 'Users'.
configObject{}The list config. See the createList API page for more details.

extendGraphQLSchema(config)

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

Usage

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

OptionTypeDescription
typesarrayA list of strings defining graphQL types.
queriesarrayA list of objects of the form { schema, resolver }.
mutationsarrayA 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.

{
  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 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

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:

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

Config

OptionTypeDescription
[listKey]ObjectAn 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

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

Config

OptionTypedefaultDescription
devBooleanfalseSets the dev flag in KeystoneJS' express middleware.
appsArray[]An array of 'Apps' which are express middleware.
distDirStringdistThe build directory for keystone.

connect()

Manually connect KeystoneJS to the adapters.

Usage

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.

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

keystone.executeQuery(queryString, config);

queryString

A graphQL query string. For example:

query {
  allTodos {
    id
    name
  }
}

Can also be a mutation:

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

Config

OptionTypeDefaultDescription
variablesObject{}The variables passed to the graphql query for the given queryString.
contextObject{}Override the default context object passed to the GraphQL engine. Useful for adding a req or setting the schemaName
16.1.0

5 years ago

16.0.1

5 years ago

16.0.0

5 years ago

15.3.2

5 years ago

15.1.3

5 years ago

15.0.1

5 years ago

14.0.1

5 years ago

15.4.1

5 years ago

15.2.2

5 years ago

15.4.0

5 years ago

15.3.1

5 years ago

15.3.0

5 years ago

15.2.1

5 years ago

15.2.0

5 years ago

15.1.2

5 years ago

15.1.1

5 years ago

15.1.0

5 years ago

15.0.0

5 years ago

14.0.0

5 years ago

13.1.0

5 years ago

13.0.0

5 years ago

12.0.1

5 years ago

12.0.0

5 years ago

11.0.0

5 years ago

10.5.0

5 years ago

10.4.0

5 years ago

10.3.0

5 years ago

10.2.0

5 years ago

10.1.0

5 years ago

10.0.0

5 years ago

9.1.0

5 years ago

9.0.0

5 years ago

8.0.0

5 years ago

7.0.3

5 years ago

7.0.2

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.0.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.0.0

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago