I assume that when you say "I don’t have active session on my custom mutations" you mean that, in your custom mutation resolver, the session
property of the context
argument is coming though as null
?
Not sure what's causing this in your case. I've created a simple Keystone app with session and a custom mutation but failed to reproduce the issue you describe. In the current release of Keystone the custom GraphQL syntax is a bit different that the version you're on (docs, examples). It's also possible whatever problem you were seeing has been fixed. Hard to know without knowing the specific version you have in your package.json
.
My example has just a User list that can be authenticated and a mutation called getRandom
that returns a random float. The entire thing is ~50 lines; here's the keystone.ts
:
import { list, config, graphql } from '@keystone-6/core';
import { statelessSessions } from '@keystone-6/core/session';
import { allowAll } from '@keystone-6/core/access';
import { text, password } from '@keystone-6/core/fields';
import { createAuth } from '@keystone-6/auth';
import { Context } from '.keystone/types';
let sessionSecret = Math.random().toString(36).substring(2, 10).repeat(4);
let sessionMaxAge = 60 * 60 * 24 * 30;
const { withAuth } = createAuth({
listKey: 'User',
identityField: 'email',
secretField: 'password',
sessionData: 'name',
});
const lists = {
User: list({
access: allowAll,
fields: {
name: text({ validation: { isRequired: true } }),
email: text({ isIndexed: 'unique', validation: { isRequired: true } }),
password: password(),
},
}),
};
const extendGraphqlSchema = graphql.extend(base => ({
mutation: {
getRandom: graphql.field({
type: graphql.Float,
args: {},
resolve(source, {}, context: Context) {
console.log('Dumping session object:', context.session);
return Math.random();
},
}),
},
}));
export default withAuth(
config({
db: {
provider: 'postgresql',
url: process.env.DATABASE_URL || 'postgres://molomby@localhost/ks6-auth',
},
lists,
session: statelessSessions({
maxAge: sessionMaxAge,
secret: sessionSecret,
}),
extendGraphqlSchema,
})
);
I'm running this out of the examples directory in a clone of the main Keystone repo so package versions are current, specifically: @keystone-6/auth@5.0.1
and @keystone-6/core@3.1.2
.
The DB was manually seeded with a single user so I could authenticate:

Then I start the app and hit the custom mutation:
mutation {
getRandom
}
If there's no current session, then context.session
is null, as you'd expect:
Dumping session object: undefined
But if I sign into the Admin UI using the seeded user record, then hit the mutation from the GraphQL Playground (in the same browser, of course), I get my GraphQL resolver dumps the session object you'd expect:
Dumping session object: {
listKey: 'User',
itemId: 'claro2yq40008lh6y5wpkh2s1',
data: [Object: null prototype] { name: 'Molomby' }
}