Skip to content

Commit

Permalink
Move session graphQL code into sessions module (#5004)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 3, 2021
1 parent 03abbab commit ceab7dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-rivers-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Moved session handling GraphQL schema code into the sessions module.
25 changes: 3 additions & 22 deletions packages-next/keystone/src/lib/createGraphQLSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { GraphQLObjectType } from 'graphql';
import { mergeSchemas } from '@graphql-tools/merge';
import { mapSchema } from '@graphql-tools/utils';
import { makeExecutableSchema } from '@graphql-tools/schema';
import type { KeystoneConfig, KeystoneContext, BaseKeystone } from '@keystone-next/types';
import type { KeystoneConfig, BaseKeystone } from '@keystone-next/types';
import { getAdminMetaSchema } from '@keystone-next/admin-ui/system';

import { gql } from '../schema';
import { sessionSchema } from '../session';

export function createGraphQLSchema(config: KeystoneConfig, keystone: BaseKeystone) {
// Start with the core keystone graphQL schema
Expand Down Expand Up @@ -41,24 +39,7 @@ export function createGraphQLSchema(config: KeystoneConfig, keystone: BaseKeysto

// Merge in session graphQL API
if (config.session) {
graphQLSchema = mergeSchemas({
schemas: [graphQLSchema],
typeDefs: gql`
type Mutation {
endSession: Boolean!
}
`,
resolvers: {
Mutation: {
async endSession(rootVal, args, context: KeystoneContext) {
if (context.endSession) {
await context.endSession();
}
return true;
},
},
},
});
graphQLSchema = sessionSchema(graphQLSchema);
}

// Merge in the admin-meta graphQL API
Expand Down
25 changes: 25 additions & 0 deletions packages-next/keystone/src/session/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mergeSchemas } from '@graphql-tools/merge';
import { GraphQLSchema } from 'graphql';
import { IncomingMessage, ServerResponse } from 'http';
import * as cookie from 'cookie';
import Iron from '@hapi/iron';
Expand All @@ -7,7 +9,9 @@ import {
SessionStoreFunction,
SessionContext,
CreateContext,
KeystoneContext,
} from '@keystone-next/types';
import { gql } from '../schema';

// uid-safe is what express-session uses so let's just use it
import { sync as uid } from 'uid-safe';
Expand Down Expand Up @@ -237,3 +241,24 @@ export async function createSessionContext<T>(
endSession: () => sessionStrategy.end({ req, res, createContext }),
};
}

export function sessionSchema(graphQLSchema: GraphQLSchema) {
return mergeSchemas({
schemas: [graphQLSchema],
typeDefs: gql`
type Mutation {
endSession: Boolean!
}
`,
resolvers: {
Mutation: {
async endSession(rootVal, args, context: KeystoneContext) {
if (context.endSession) {
await context.endSession();
}
return true;
},
},
},
});
}

1 comment on commit ceab7dc

@vercel
Copy link

@vercel vercel bot commented on ceab7dc Mar 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.