I need to add some custom server-side functions to my KeystoneJS instance. Below is a simple example; the function userCount
has been added to the exports of the file that initialises Keystone, which should return a count of the number of users.
module.exports = {
keystone,
apps: [
new GraphQLApp(),
new AdminUIApp({
name: PROJECT_NAME,
enableDefaultRoute: true,
authStrategy,
}),
],
userCount: async () => {
try {
const context = keystone.createContext({ skipAccessControl: true });
console.log(context); // Output below
const userCount = await keystone.executeGraphQL({
context,
query: `query {
_allUsersMeta {
count
}
}`,
});
return userCount;
} catch (error) {
return error;
}
}
};
Context
seems to be correctly created with the schema name public
(see console log below), but no matter what I try, I get the following error:
No executable schema named 'public' is available. Have you setup '@keystonejs/app-graphql'?
This seems to contradict the log below. What am I doing wrong?
console.log(context)
output:
{
schemaName: 'public',
authedItem: undefined,
authedListKey: undefined,
getCustomAccessControlForUser: [Function: getCustomAccessControlForUser],
getListAccessControlForUser: [Function: getListAccessControlForUser],
getFieldAccessControlForUser: [Function: getFieldAccessControlForUser],
getAuthAccessControlForUser: [Function: getAuthAccessControlForUser],
totalResults: 0,
maxTotalResults: 1000,
createContext: [Function],
executeGraphQL: [Function],
gqlNames: [Function]
}
The cause of the problem seems to be that I was handling GET requests with a separate Express instance, which I'd set up in addition to the instance used by Keystone.
The solution was to add all my Express logic (e.g. custom endpoints) to the configureExpress
export in the Keystone index.js
file, as per these instructions.
A custom server can be setup for more complicated implementations.